def draw(): "Advance players and draw game." p1xy.move(p1aim) #moving the first snake by p1aim which we set earlier p1head = p1xy.copy() #copying the x,y coordinates to p1head p2xy.move(p2aim) #moving the second snake by p2aim which we set earlier p2head = p2xy.copy() #copying the x,y coordinates to p2head if not inside( p1head ) or p1head in p2body: #if red snake touches the screen or blue snake then blue wins print('Player blue wins!') return if not inside( p2head ) or p2head in p1body: #if blue snake touches the screen or red snake then red wins print('Player red wins!') return p1body.add(p1head) #storing p1head(which we copied) to p1body p2body.add(p2head) #storing p2head(which we copied) to p2body square( p1xy.x, p1xy.y, 3, 'red' ) #drawing body of first snake from all the stored coordinates, 3 is pensize square( p2xy.x, p2xy.y, 3, 'blue' ) #drawing body of second snake from all the stored coordinates, 3 is pensize update() ontimer(draw, 50) #drawing delay by 50ms
def draw(): global a "Advance players and draw game." p1xy.move(p1aim) p1head = p1xy.copy() p2xy.move(p2aim) p2head = p2xy.copy() Test() if not inside(p1head) or p1head in p2body: print('Player blue wins!') return if not inside(p2head) or p2head in p1body: print('Player red wins!') return p1body.add(p1head) p2body.add(p2head) square(p1xy.x, p1xy.y, 3, 'red') square(p2xy.x, p2xy.y, 3, 'blue') update() ontimer(draw, 50)
def move(): # "Move snake forward one segment." head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') update() return snake.append(head) if head == food: print('Snake:', len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 # bad_food.append() else: snake.pop(0) if head == bad_food: square(head.x, head.y, 9, 'red') update() return clear() for body in snake: square(body.x, body.y, 9, 'black') square(food.x, food.y, 9, 'green') square(bad_food.x, bad_food.y, 9, 'red') update() ontimer(move, 100)
def move(): head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: sqauare(head.x, head.y, 9, 'read') update() return snake.append(head) if head == food: print('Snake:', len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, 'black') square(food.x, food.y, 9,"red") update() ontimer(move, 100)
def flash(tile): glow, dark = tiles[tile] square(tile.x, tile.y, 200, glow) update() sleep(0.5) square(tile.x, tile.y, 200, dark) update() sleep(0.5)
def flash(tile): "Flash tile in grid." glow, dark = tiles[tile] square(tile.x, tile.y, 200, glow) update() sleep(0.25) square(tile.x, tile.y, 200, dark) update() sleep(0.25)
def flash(tile): "Flash tile in grid." glow, dark = tiles[tile] square(tile.x, tile.y, 200, glow) update() sleep(show_time) square(tile.x, tile.y, 200, dark) update() sleep(wait_time)
def flash(tile): "Flash tile in grid." glow, dark = tiles[tile] square(tile.x, tile.y, 200, glow) update() sleep(0.5) square(tile.x, tile.y, 200, dark) update() sleep(0.5)
def draw(): "Draw all the squares." step() clear() for (x, y), alive in cells.items(): color = 'green' if alive else 'black' square(x, y, 10, color) update() ontimer(draw, 100)
def loadingBar(): snakex = -50 snakey = -100 hideturtle() square(snakex, snakey, 15, 'green') square(snakex + 17, snakey, 15, 'green') square(snakex + 34, snakey, 15, 'green') square(snakex + 51, snakey, 15, 'green') square(snakex + 68, snakey, 15, 'green')
def flash(self, tile): "Flash tile in grid." glow, dark = self.tiles[tile] square(tile.x, tile.y, 200, glow) update() sleep(0.3) square(tile.x, tile.y, 200, dark) update() sleep(0.3)
def draw(): step() clear() for (x, y), alive in cells.items(): color = "green" if alive else "black" square(x, y, 10, color) update() ontimer(draw, 100)
def flash(tile): #for blinking the tiles "Flash tile in grid." glow, dark = tiles[tile] # taking the tapped tile in glow and dark variables square(tile.x, tile.y, 200, glow) #draw the respective tile by glow variable attributes update() #updating the screen sleep(0.5) #delay time for blinking square(tile.x, tile.y, 200, dark) #draw the respective tile by dark variable attributes update() sleep(0.5)
def desenhaTela(): passo() clear() for (x, y), vivo in matriz.items(): cor = 'green' if vivo else 'black' square(x, y, 10, cor) update( ) # Perform a TurtleScreen update. To be used when tracer is turned off. ontimer(desenhaTela, 1) # Install a timer that calls fun after t milliseconds.
def flash(tile): "Flash tile in grid." #print("Tile: "+str(tile)) glow, dark = tiles[tile] square(tile.x, tile.y, 200, glow) update() sleep(0.5) square(tile.x, tile.y, 200, dark) update() sleep(0.5)
def draw(): global sleepTime global subTime global stringPen # 6 players の動作を描画し判定する "Advance players and draw game." for player in players: player.xy.move(player.aim) player.head = player.xy.copy() # 記憶用の座標を3の倍数に成形 player.head = vector( int( player.head.x ) - ( int( player.head.x ) % 3) ,\ int( player.head.y ) - ( int( player.head.y ) % 3) ) # change to p1 , hit check on p1 self. for player in players: if player.previousHead != player.head: player.previousHead = player.head if not inside(player.head) or hitcheck(player): stringPen.clear() stringPen.write('Player ' + player.color + ' lose!', False, 'center', ('Alial', 14, 'normal')) print('Player ' + player.color + ' lose!') square(player.xy.x, player.xy.y, 5, 'Black') update() time.sleep(5) sys.exit(0) else: print("same previous") # hit したらそのPlayerのライフフラグを折る。 (game over) TODO # return for player in players: player.body.add(player.head) # square(player.xy.x, player.xy.y, 3, player.color ) square(player.head.x, player.head.y, 5, player.color) update() # include players speed. Change game barance here. ontimer(draw, sleepTime) # ontimer(draw, 100) # Todo change level up time and curve. subTime += sleepTime # if subTime > (sleepTime): if subTime > (10000): subTime = 0 sleepTime -= 10 print("Speed up" + str(sleepTime))
def grid(): "Draw grid of tiles." square(0, 0, 200, 'dark red') square(0, -200, 200, 'dark blue') square(-200, 0, 200, 'dark green') square(-200, -200, 200, 'khaki') update()
def move(): "Move snake forward one segment." global obstacles head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake or head in obstacles: # head in obstacles 用于判断是否撞到了障碍物 square(head.x, head.y, s - 1, 'red') update() return snake.append(head) if head == food: print('Snake:', len(snake)) food.x = randrange(-15, 15) * s food.y = randrange(-15, 15) * s obstacles = get_obstacles() # 吃食物后,随机重排列障碍物箱子(注释掉这行取消该功能) # times.x = 200 # 注释掉这行代码, 蛇吃食物后速度不再还原 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, s - 1, 'black') for obstacle in obstacles: square(obstacle.x, obstacle.y, s - 1, 'red') square(food.x, food.y, s - 1, 'green') update() # ontimer(move,200- times.x) ontimer(move, times.x)
def move(): "Move snake forward one segment." head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') update() return snake.append(head) if head == food1 or head == food2: print('Food1Storage:', len(snake)) food1.x = randrange(-15, 15) * 10 food1.y = randrange(-15, 15) * 10 update() else: #head == food2: print('food2Storage:', len(snake)) food2.x = randrange(-15, 15) * 10 food2.y = randrange(-15, 15) * 10 update() #print("total" , len(snake) + len(snake)+2) #else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, 'blue') square(food1.x, food1.y, 9, 'red') square(food2.x, food2.y, 9, 'purple') update() ontimer(move, 100)
def grid(): #function to draw the tiles "Draw grid of tiles." square(0, 0, 200, 'dark red') square(0, -200, 200, 'dark blue') square(-200, 0, 200, 'dark green') square(-200, -200, 200, 'khaki') update()
def move(): "Move pacman and all ghosts." writer.undo() writer.write(state['score']) clear() if valid(pacman + aim): pacman.move(aim) index = offset(pacman) if tiles[index] == 1: tiles[index] = 2 state['score'] += 1 x = (index % 20) * 20 - 200 y = 180 - (index // 20) * 20 square(x, y) up() goto(pacman.x + 10, pacman.y + 10) # розмір пекмена та його колір dot(20, 'red') for point, course in ghosts: if valid(point + course): point.move(course) else: options = [ vector(5, 0), vector(-5, 0), vector(0, 5), vector(0, -5), ] plan = choice(options) course.x = plan.x course.y = plan.y up() goto(point.x + 10, point.y + 10) # розмір ворогів та їх колір dot(20, 'white') update() for point, course in ghosts: if abs(pacman - point) < 20: return ontimer(move, 100)
def move(): head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') update() return snake.append() if head == food: print('snake', len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10
def move(self): "Move snake forward one segment." head = self.snake[-1].copy() head.move(self.aim) # if SnakeGameEnvironment.i >= 500: # square(head.x, head.y, 9, 'red') # update() # bye() # SnakeGameEnvironment.i = 0 # return if not self.inside( head) or head in self.snake or head in self.obstacles: square(head.x, head.y, 9, 'red') update() bye() SnakeGameEnvironment.i = 0 self.reward = -100 action = self.agent.Act(self.getState(head, self.food), self.MOVABLE_DIRECTION, self.reward, True) return else: self.snake.append(head) if head == self.food: print('Snake:', len(self.snake)) i = 1 while (True): self.food.x = randrange(-19, 19) * 10 self.food.y = randrange(-19, 19) * 10 if self.food not in self.snake or self.food not in self.obstacles: break self.reward = 500 SnakeGameEnvironment.i = 0 else: self.snake.pop(0) self.reward = -10 action = self.agent.Act(self.getState(head, self.food), self.MOVABLE_DIRECTION, self.reward, False) self.direction = self.movableDirections(action, self.direction) self.aim = SnakeGameEnvironment.DIRECTIONS[self.direction] clear() for body in self.snake: square(body.x, body.y, 9, 'black') for obstacle in self.obstacles: square(obstacle.x, obstacle.y, 9, 'yellow') square(self.food.x, self.food.y, 9, 'green') # print(np.nonzero(self.rewardMatrix)) # print(np.unique(self.rewardMatrix)) # print(np.argwhere(self.rewardMatrix == SnakeGameEnvironment.BLACK).flatten()) update() ontimer(self.move, 1) SnakeGameEnvironment.i = SnakeGameEnvironment.i + 1
def world(): "Draw world using path." bgcolor('black') path.color('blue') for index in range(len(tiles)): tile = tiles[index] if tile > 0: x = (index % 20) * 20 - 200 y = 180 - (index // 20) * 20 square(x, y) if tile == 1: path.up() path.goto(x + 10, y + 10) # розмір їжі та її колір path.dot(2, 'yellow')
def draw(): global sleepTime global subTime "Advance players and draw game." p1xy.move(p1aim) p1head = p1xy.copy() p2xy.move(p2aim) p2head = p2xy.copy() # change to p1 , hit check on p1 self. if not inside(p1head) or p1head in p2body or p1head in p1body: print('Player blue wins!') time.sleep(5) sys.exit(0) # return if not inside(p2head) or p2head in p1body or p2head in p2body: print('Player red wins!') time.sleep(5) sys.exit(0) # return p1body.add(p1head) p2body.add(p2head) square(p1xy.x, p1xy.y, 3, 'red') square(p2xy.x, p2xy.y, 3, 'blue') update() # include players speed. Change game barance here. ontimer(draw, sleepTime) # ontimer(draw, 100) # Todo change level up time and curve. subTime += sleepTime # if subTime > (sleepTime): if subTime > (10000): subTime = 0 sleepTime -= 10 print ("Speed up" + str ( sleepTime ) )
def draw(): global sleepTime global subTime # 6 players の動作を描画し判定する "Advance players and draw game." for player in players: player.xy.move(player.aim) player.head = player.xy.copy() # change to p1 , hit check on p1 self. for player in players: if not inside(player.head) or hitcheck(player): print('Player ' + player.color + ' lose!') square(player.xy.x, player.xy.y, 3, 'Black') update() time.sleep(5) sys.exit(0) # hit したらそのPlayerのライフフラグを折る。 (game over) TODO # return for player in players: player.body.add(player.head) square(player.xy.x, player.xy.y, 3, player.color) # square(player.xy.x, player.xy.y, 4, player.color ) update() # include players speed. Change game barance here. ontimer(draw, sleepTime) # ontimer(draw, 100) # Todo change level up time and curve. subTime += sleepTime # if subTime > (sleepTime): if subTime > (10000): subTime = 0 sleepTime -= 10 print("Speed up" + str(sleepTime))
def move(): "Move snake forward one segment." #Mova a cobra um segmento para frente head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: # caso a cobra encoste nas bordas square(head.x, head.y, 9, 'red')# desenha um dradrado vermelho update() return snake.append(head) #adiciona um quadrado na direção aim no vetor snake if head == food: print('Snake:', len(snake)) food.x = randrange(-15, 15) * 10 #novo X da comida no intervalo determinado food.y = randrange(-15, 15) * 10 #novo Y da comida no intervalo determinado else: snake.pop(0) # remove o quadrado da posição anterior do vetor snake clear() #tamanho do corpo da cobra em relação ao vetor snake for body in snake: square(body.x, body.y, 9, 'black') #tamanho da comida square(food.x, food.y, 9, 'green') update() ontimer(move, 100)
def move(): "Move snake forward one segment." head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') update() return snake.append(head) if head == food: print('Snake:', len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, color3[color1]) square(food.x, food.y, 9, color4[color2]) update() ontimer(move, 100)
def game_start(): clear() print_title() square(-140, -80, 80, 'gray') up() goto(-100, -50) down() color('black') write("Easy", move=True, align="center", font=("Arial", 15, "bold")) square(-40, -80, 80, 'gray') up() goto(0, -50) down() color('black') write("Normal", move=True, align="center", font=("맑은고딕", 15, "bold")) square(60, -80, 80, 'gray') up() goto(100, -50) down() color('black') write("Hard", move=True, align="center", font=("맑은고딕", 15, "bold")) up() goto(0, 100) down() color('black') write("Please select the difficulty level", move=True, align="center", font=("맑은고딕", 18, "bold")) onscreenclick(tap_button)
def move(): head = snake[-1].copy() head.move(aim) ##arguments which end game, either hitting the boundary or hitting itself ##prints final score upon collision ##deleting quit line will make snake head red upon collison if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') update() print('Game Over! Final Score:', len(snake)-1) return snake.append(head) ##argument for eating grapes and randomizing next grape location if head == grape: grape.x = randrange(-15, 15) * 10 grape.y = randrange(-15, 15) * 10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, 'green') square(grape.x, grape.y, 9, 'purple') update() ontimer(move, 100)
def move(): "Move snake forward one segment." head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') update() return snake.append(head) #to print the points you get if head == food: print('your points:', len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, 'black') square(food.x, food.y, 9, 'green') update() ontimer(move, 100)
def move(): "Move snake forward one segment." head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') update() return snake.append(head) if head == food: print('Snake:', len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, 'black') square(food.x, food.y, 9, 'green') update() ontimer(move, 100)
def stamp(x, y, text): "Display `text` at coordinates `x` and `y`." square(x, y, 50, 'white') color('black') write(text, font=('Arial', 50, 'normal'))