snake.move() # 判断贪吃蛇是否吃到食物 if snake.body[0][0] == food.x and snake.body[0][1] == food.y: food = Food() snake.body.append(snake.body[-1]) score += 10 # 判断贪吃蛇是否撞墙或者自己的身体 if snake.body[0][0] < 0 or snake.body[0][0] >= SIZE[0] or snake.b...
for snake in snakes: if head.col==snake.col and head.row==snake.row: dead=True break if dead: print('死了') quit=False #画背景 pygame.draw.rect(window, bg_color, (0,0,W,H)) #蛇头 for snake in snakes: rect(snake, snake_color) rect(head, head_color) rect(food, food_color)...
self.update_snake(self.last_key)) else: self.begin = False self.display_gameover(...
draw.line(screen, Black, (0, y), (Screen_Width, y), Line_Width) #蛇的爬行过程 if not game_over: curTime=time.time() if curTime-last_move_time>speed: ### if not pause: b=True last_move_time=curTime next_s = (snake[0][0] + pos[0], snake[0][1] + pos[1]) #如果吃到...
If we run the game, we can now see the snake moving to the right all the time. Note: obviously we don't see all the magic yet since the snake only consists of one element. Since we want to be able to move it with theW,S,AandDkeys, we will have to check if those were press...
snake_body.pop()ifnotfood_spawn:food_pos=[random.randrange(1,width//10)*10,random.randrange(1,height//10)*10]food_spawn=Truescreen.fill((0,0,0))draw_snake(snake_body)draw_food(food_pos)show_score()ifsnake_pos[0]<0orsnake_pos[0]>width-10:game_over()ifsnake_pos[1]<0orsnake_...
# 定义游戏结束def game_over(): global run dead=False # 定义一个状态dead # 游戏结束方式1、撞墙 if head.col<0 or head.row<0 or head.col>=COL or head.row>=ROW: # 如果蛇头的行和列在屏幕外 dead = True # 游戏结束方式2、撞自己 for snake in snakes: if head.col==snake.col and hea...
()# 食物food = create_food(snake)food_style = get_food_style()# 方向pos = (1, 0)game_over = Truestart = False # 是否开始,当start = True,game_over = True 时,才显示 GAME OVERscore = 0 # 得分orispeed = 0.5 # 原始速度speed = orispeedlast_move_time = Nonepause = False # 暂停...
# 定义游戏结束def game_over(): global run dead=False # 定义一个状态dead # 游戏结束方式1、撞墙 if head.col<0 or head.row<0 or head.col>=COL or head.row>=ROW: # 如果蛇头的行和列在屏幕外 dead = True # 游戏结束方式2、撞自己 for snake in snakes: if head.col==snake.col and hea...
pygame.display.update()# 定义一个函数来检查游戏是否结束defcheck_game_over():globalsnake_x, snake_y, snake_direction, snake_bodyif(snake_x <0orsnake_x > screen_width - snake_sizeorsnake_y <0orsnake_y > screen_height - snake_sizeor[snake_x, snake_y]insnake_body[1:]):returnTrueretu...