直接GameOver# b变量为了防止这个情况发生b=True# 蛇snake=init_snake()# 食物food=Creat_Food(snake)...
#2.撞自己 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, ...
Let's make a Snake game in Python(in less than 100 lines code)! For those who don't know, the white thing is the snake. It can be controlled by the player to go up, down, left and right. Every time the snake eats one of those blue things(let's call it food), it gets bigge...
import pygame import sys import random # 定义颜色 WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) GREY = (211, 211, 211) # 淡灰色 def init(): global screen, screen_size global snake_pos, food_pos, snake_speed # 初始化pygame pygame.init() # 设置屏幕大小 ...
game_over = False while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block_size y1_change = 0 ...
whileTrue:foreventinpygame.event.get():ifevent.type== QUIT: sys.exit()elifevent.type== KEYDOWN:ifevent.key == K_RETURN:ifgame_over: start =Truegame_over =Falseb =Truesnake = init_snake() food = create_food(snake) food_style = get_food_style() ...
if snake_pos[1] < 0 or snake_pos[1] > screen_height-10: # 撞上下墙 game_over() 检查撞自己身体 从列表第二个元素开始检查(索引为1),因为第一个是刚加进去的头 for block in snake_body[1:]: if snake_pos[0] == block[0] and snake_pos[1] == block[1]: ...
二、GameOver 之前提到,所有游戏最重要的部分是循环。而GameOver函数就是跳出这个循环的条件。这里给出当蛇吃到自己身体或者碰到边界时显示的界面(判断死亡的代码会在之后展示) 三、贪吃蛇与树莓 接下来介绍游戏的主题部分,即贪吃蛇与蛇莓的显示以及运动。
running_game(screen, snake_speed_clock)是游戏主要功能,在这里给大家慢慢讲解。先贴代码: #游戏运行主体defrunning_game(screen,snake_speed_clock):startx=random.randint(3,map_width-8)#开始位置starty=random.randint(3,map_height-8)snake_coords=[{'x':startx,'y':starty},#初始贪吃蛇{'x':startx...
#游戏运行主体def running_game(screen,snake_speed_clock):startx = random.randint(3, map_width - 8) #开始位置starty = random.randint(3, map_height - 8)snake_coords = [{'x': startx, 'y': starty}, #初始贪吃蛇{'x': startx - 1, 'y': starty},{'x': startx - 2, 'y': st...