基于pygame实现贪吃蛇小游戏示例

目录

  • 游戏截图
  • 引入库
  • 初始化模型
  • 获取键盘事件
  • 移动贪吃蛇
  • 吃食物逻辑
  • 碰撞到自身逻辑
  • 结束游戏
  • 显示文字函数
  • 完整代码
【基于pygame实现贪吃蛇小游戏示例】
游戏截图 基于pygame实现贪吃蛇小游戏示例
文章图片

基于pygame实现贪吃蛇小游戏示例
文章图片


引入库
import copyimport randomimport sysimport pygame


初始化模型
# 蛇模型snake_list = [[10, 10]]# 食物的模型x = random.randint(10, 490)y = random.randint(10, 490)food_point = [x, y]food_r, food_g, food_b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)food_color = pygame.Color(food_r, food_g, food_b)# 初始方向move_up = Falsemove_down = Falsemove_left = Falsemove_right = True# 初始分数score=0pygame.init()screen = pygame.display.set_mode((500, 500))# 画布大小title = pygame.display.set_caption('贪吃蛇')# 名字clock = pygame.time.Clock() # 游戏时钟


获取键盘事件
for event in pygame.event.get():# 获取键盘事件if event.type == pygame.QUIT:running=Falsesys.exit()if event.type == pygame.KEYDOWN:if event.key == pygame.K_DOWN or event.key == pygame.K_s:move_up = Falsemove_down = Truemove_left = Falsemove_right = Falseif event.key == pygame.K_UP or event.key == pygame.K_w:move_up = Truemove_down = Falsemove_left = Falsemove_right = Falseif event.key == pygame.K_LEFT or event.key == pygame.K_a:move_up = Falsemove_down = Falsemove_left = Truemove_right = Falseif event.key == pygame.K_RIGHT or event.key == pygame.K_d:move_up = Falsemove_down = Falsemove_left = Falsemove_right = Trueif event.key == pygame.K_ESCAPE:# esc关闭running=Falsesys.exit()if event.key ==pygame.K_SPACE and not running:running=Truesnake_list = [[10, 10]]score=0


移动贪吃蛇
# 身子移动snake_len = len(snake_list) - 1while snake_len > 0:snake_list[snake_len] = copy.deepcopy(snake_list[snake_len - 1])snake_len -= 1# 蛇头移动if move_up:snake_list[snake_len][1] -= 10if snake_list[snake_len][1] < 0:snake_list[snake_len][1] = 500if move_down:snake_list[snake_len][1] += 10if snake_list[snake_len][1] > 500:snake_list[snake_len][1] = 0if move_left:snake_list[snake_len][0] -= 10if snake_list[snake_len][0] < 0:snake_list[snake_len][0] = 500if move_right:snake_list[snake_len][0] += 10if snake_list[snake_len][0] > 500:snake_list[snake_len][0] = 0


吃食物逻辑
# 蛇与食物碰撞检测if food_rect.collidepoint(snake_pos):snake_list.append(food_point)food_point = [random.randint(10, 490), random.randint(10, 490)] # 重新生成食物food_r, food_g, food_b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)food_color = pygame.Color(food_r, food_g, food_b)score+=1break


碰撞到自身逻辑
# 吃到自己 结束游戏snake_head_rect = snake_rect[0] # 蛇头count = len(snake_rect)while count > 1: if snake_head_rect.colliderect(snake_rect[count - 1]): # 检测蛇头与身子的所有点running=False count -= 1


结束游戏
if not running:show_text(screen, (20,200), 'GAME OVER!', (227, 29, 18), False, 100)show_text(screen, (120,300), 'press space to try again', (223, 223, 223), False, 30)


显示文字函数
def show_text(screen, pos, text, color, font_bold=False, font_size=60, font_italic=False):cur_font = pygame.font.SysFont('宋体', font_size)cur_font.set_bold(font_bold)cur_font.set_italic(font_italic)text_fmt = cur_font.render(text, 1, color)screen.blit(text_fmt, pos)


完整代码
import copyimport randomimport sysimport pygamedef show_text(screen, pos, text, color, font_bold=False, font_size=60, font_italic=False):cur_font = pygame.font.SysFont('宋体', font_size)cur_font.set_bold(font_bold)cur_font.set_italic(font_italic)text_fmt = cur_font.render(text, 1, color)screen.blit(text_fmt, pos)# 蛇模型snake_list = [[10, 10]]# 食物的模型x = random.randint(10, 490)y = random.randint(10, 490)food_point = [x, y]food_r, food_g, food_b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)food_color = pygame.Color(food_r, food_g, food_b)# 初始方向move_up = Falsemove_down = Falsemove_left = Falsemove_right = Truepygame.init()screen = pygame.display.set_mode((500, 500))# 画布大小title = pygame.display.set_caption('贪吃蛇')# 名字clock = pygame.time.Clock() # 游戏时钟running=True# 游戏运行标志score=0while True:clock.tick(20)# 20fpsscreen.fill([255, 255, 255])# 背景填充for event in pygame.event.get():# 获取键盘事件if event.type == pygame.QUIT:running=Falsesys.exit()if event.type == pygame.KEYDOWN:if event.key == pygame.K_DOWN or event.key == pygame.K_s:move_up = Falsemove_down = Truemove_left = Falsemove_right = Falseif event.key == pygame.K_UP or event.key == pygame.K_w:move_up = Truemove_down = Falsemove_left = Falsemove_right = Falseif event.key == pygame.K_LEFT or event.key == pygame.K_a:move_up = Falsemove_down = Falsemove_left = Truemove_right = Falseif event.key == pygame.K_RIGHT or event.key == pygame.K_d:move_up = Falsemove_down = Falsemove_left = Falsemove_right = Trueif event.key == pygame.K_ESCAPE:# esc关闭running=Falsesys.exit()if event.key ==pygame.K_SPACE and not running:running=Truesnake_list = [[10, 10]]score=0# 身子移动snake_len = len(snake_list) - 1while snake_len > 0:snake_list[snake_len] = copy.deepcopy(snake_list[snake_len - 1])snake_len -= 1# 蛇头移动if move_up:snake_list[snake_len][1] -= 10if snake_list[snake_len][1] < 0:snake_list[snake_len][1] = 500if move_down:snake_list[snake_len][1] += 10if snake_list[snake_len][1] > 500:snake_list[snake_len][1] = 0if move_left:snake_list[snake_len][0] -= 10if snake_list[snake_len][0] < 0:snake_list[snake_len][0] = 500if move_right:snake_list[snake_len][0] += 10if snake_list[snake_len][0] > 500:snake_list[snake_len][0] = 0if running:# 绘制得分show_text(screen, (200,20), f'score: {score}', (0, 0, 0), False, 30)# 绘制食物food_rect = pygame.draw.circle(screen, food_color, food_point, 15)# 绘制蛇snake_rect = []for snake_pos in snake_list:snake_rect.append(pygame.draw.circle(screen, food_color, snake_pos, 5))# 蛇与食物碰撞检测if food_rect.collidepoint(snake_pos):snake_list.append(food_point)food_point = [random.randint(10, 490), random.randint(10, 490)] # 重新生成食物food_r, food_g, food_b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)food_color = pygame.Color(food_r, food_g, food_b)score+=1break# 吃到自己 结束游戏snake_head_rect = snake_rect[0] # 蛇头count = len(snake_rect)while count > 1:if snake_head_rect.colliderect(snake_rect[count - 1]): # 检测蛇头与身子的所有点running=Falsecount -= 1if not running:show_text(screen, (20,200), 'GAME OVER!', (227, 29, 18), False, 100)show_text(screen, (120,300), 'press space to try again', (223, 223, 223), False, 30)pygame.display.update()# 绘制

到此这篇关于基于pygame实现贪吃蛇小游戏示例的文章就介绍到这了,更多相关pygame贪吃蛇小游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读