如何Python实现完整功能的贪吃蛇小游戏
Admin 2022-05-21 群英技术资讯 630 次浏览
文章利用Python pygame做一个贪吃蛇的小游戏而且讲清楚每一段代码是用来干嘛的。
据说是贪吃蛇游戏是1976
年,Gremlin
公司推出的经典街机游戏,那我们今天用Python
制作的这个贪吃蛇小游戏是一个像素版的,虽然简陋,但还是可以玩起来的
我们主要要做的内容:
贪吃蛇的棋盘模型:
现在就开始我们的代码,首先,还是导入模块:
import pygame import random import copy
pygame.init() clock = pygame.time.Clock() # 设置游戏时钟 pygame.display.set_caption("贪吃蛇-解答、源码、相关资料可私信我") # 初始化标题 screen = pygame.display.set_mode((500, 500)) # 初始化窗口 窗体的大小为 500 500
snake_list = [[10, 10]]
首先设置蛇的一个运行方向 接下来判断键盘事件在决定蛇的运行方向
蛇可以运行起来了,那么接下来就是,吃食物增加自己的长度和不吃食物在不同的位置显示
初始小蛇方向:
move_up = False move_down = False move_left = False move_right = True
x = random.randint(10, 490) y = random.randint(10, 490) food_point = [x, y]
running = True while running: # 游戏时钟 刷新频率 clock.tick(20)
screen.fill([255, 255, 255])
for x in range(0, 501, 10): pygame.draw.line(screen, (195, 197, 199), (x, 0), (x, 500), 1) pygame.draw.line(screen, (195, 197, 199), (0, x), (500, x), 1) food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0)
snake_rect = [] for pos in snake_list: # 1.7.1 绘制蛇的身子 snake_rect.append(pygame.draw.circle(screen, [255, 0, 0], pos, 5, 0))
pos = len(snake_list) - 1 while pos > 0: snake_list[pos] = copy.deepcopy(snake_list[pos - 1]) pos -= 1
if move_up: snake_list[pos][1] -= 10 if snake_list[pos][1] < 0: snake_list[pos][1] = 500 if move_down: snake_list[pos][1] += 10 if snake_list[pos][1] > 500: snake_list[pos][1] = 0 if move_left: snake_list[pos][0] -= 10 if snake_list[pos][0] < 0: snake_list[pos][0] = 500 if move_right: snake_list[pos][0] += 10 if snake_list[pos][0] > 500: snake_list[pos][0] = 0
for event in pygame.event.get(): # print(event) # 判断按下的按键 if event.type == pygame.KEYDOWN: # 上键 if event.key == pygame.K_UP: move_up = True move_down = False move_left = False move_right = False # 下键 if event.key == pygame.K_DOWN: move_up = False move_down = True move_left = False move_right = False # 左键 if event.key == pygame.K_LEFT: move_up = False move_down = False move_left = True move_right = False # 右键 if event.key == pygame.K_RIGHT: move_up = False move_down = False move_left = False move_right = True
pos = len(snake_list) - 1 while pos > 0: snake_list[pos] = copy.deepcopy(snake_list[pos - 1]) pos -= 1
if food_rect.collidepoint(pos): # 贪吃蛇吃掉食物 snake_list.append(food_point) # 重置食物位置 food_point = [random.randint(10, 490), random.randint(10, 490)] food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0) break
head_rect = snake_rect[0] count = len(snake_rect) while count > 1: if head_rect.colliderect(snake_rect[count - 1]): running = False count -= 1 pygame.display.update()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要给大家分享得事提高 Python 开发效率的3个小工具,它们能够帮助我们提高工作效率。本文将介绍笔者在近一周发现的三个小工具,写文章以做记录,希望能对大家有所帮助
python语言的3 x完全不向前兼容,导致我们在python2 x中可以正常使用的库,到了python3就用不了了 比如说mysqldb目前MySQLdb并不支持python3
常用的配置文件格式有ini、json、yaml等,下面简单给大家介绍下,Python如何读写这几种格式的文件,对Python读写ini、json、yaml配置文件相关知识感兴趣的朋友一起看看吧
Python模块对Redis数据库的连接与使用讲解,下文的讲解详细,步骤过程清晰,对大家进一步学习和理解有一定的帮助。有这方面学习需要的朋友就继续往下看吧!
本文主要介绍了numpy中的converters和usecols用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008