基于pygame实现贪吃蛇小游戏示例
程序员文章站
2022-03-18 09:57:40
目录游戏截图引入库初始化模型获取键盘事件移动贪吃蛇吃食物逻辑碰撞到自身逻辑结束游戏显示文字函数完整代码游戏截图引入库import copyimport randomimport sysimport p...
游戏截图
引入库
import copy import random import sys import 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 = false move_down = false move_left = false move_right = true # 初始分数 score=0 pygame.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=false sys.exit() if event.type == pygame.keydown: if event.key == pygame.k_down or event.key == pygame.k_s: move_up = false move_down = true move_left = false move_right = false if event.key == pygame.k_up or event.key == pygame.k_w: move_up = true move_down = false move_left = false move_right = false if event.key == pygame.k_left or event.key == pygame.k_a: move_up = false move_down = false move_left = true move_right = false if event.key == pygame.k_right or event.key == pygame.k_d: move_up = false move_down = false move_left = false move_right = true if event.key == pygame.k_escape: # esc关闭 running=false sys.exit() if event.key ==pygame.k_space and not running: running=true snake_list = [[10, 10]] score=0
移动贪吃蛇
# 身子移动 snake_len = len(snake_list) - 1 while 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] -= 10 if snake_list[snake_len][1] < 0: snake_list[snake_len][1] = 500 if move_down: snake_list[snake_len][1] += 10 if snake_list[snake_len][1] > 500: snake_list[snake_len][1] = 0 if move_left: snake_list[snake_len][0] -= 10 if snake_list[snake_len][0] < 0: snake_list[snake_len][0] = 500 if move_right: snake_list[snake_len][0] += 10 if 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+=1 break
碰撞到自身逻辑
# 吃到自己 结束游戏 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 copy import random import sys import pygame 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) # 蛇模型 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 = false move_down = false move_left = false move_right = true pygame.init() screen = pygame.display.set_mode((500, 500)) # 画布大小 title = pygame.display.set_caption('贪吃蛇') # 名字 clock = pygame.time.clock() # 游戏时钟 running=true # 游戏运行标志 score=0 while true: clock.tick(20) # 20fps screen.fill([255, 255, 255]) # 背景填充 for event in pygame.event.get(): # 获取键盘事件 if event.type == pygame.quit: running=false sys.exit() if event.type == pygame.keydown: if event.key == pygame.k_down or event.key == pygame.k_s: move_up = false move_down = true move_left = false move_right = false if event.key == pygame.k_up or event.key == pygame.k_w: move_up = true move_down = false move_left = false move_right = false if event.key == pygame.k_left or event.key == pygame.k_a: move_up = false move_down = false move_left = true move_right = false if event.key == pygame.k_right or event.key == pygame.k_d: move_up = false move_down = false move_left = false move_right = true if event.key == pygame.k_escape: # esc关闭 running=false sys.exit() if event.key ==pygame.k_space and not running: running=true snake_list = [[10, 10]] score=0 # 身子移动 snake_len = len(snake_list) - 1 while 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] -= 10 if snake_list[snake_len][1] < 0: snake_list[snake_len][1] = 500 if move_down: snake_list[snake_len][1] += 10 if snake_list[snake_len][1] > 500: snake_list[snake_len][1] = 0 if move_left: snake_list[snake_len][0] -= 10 if snake_list[snake_len][0] < 0: snake_list[snake_len][0] = 500 if move_right: snake_list[snake_len][0] += 10 if snake_list[snake_len][0] > 500: snake_list[snake_len][0] = 0 if 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+=1 break # 吃到自己 结束游戏 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) pygame.display.update() # 绘制
到此这篇关于基于pygame实现贪吃蛇小游戏示例的文章就介绍到这了,更多相关pygame贪吃蛇小游戏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 西南财经大学211排多少名?在全国算顶尖211名校吗?
下一篇: Python线程之多线程展示详解