用pygame贪吃蛇小游戏的过程及代码是什么
Admin 2022-05-28 群英技术资讯 297 次浏览
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() # 绘制
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了Python游戏推箱子的实现,推箱子游戏是一款可玩性极高的策略解谜手游,游戏中玩家将扮演一名可爱Q萌的角色,下面我们就看看看具体的实现过程吧,需要的小伙伴可以参考一下
这篇文章主要为大家介绍了pytest官方文档解读fixtures的autouse,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要为大家介绍了C语言中的结构体在Python中实现转换示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要为大家介绍了python密码学一次性密码的实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
python中none与null有啥区别?很多新手在python时,对于none与null容易混淆,因此,这篇文章就主要给大家讲讲none与null用法的不同,感兴趣的朋友就继续往下看吧,希望大家阅读完这篇文章能有所收获。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008