首页 > Python资料 博客日记
Python——贪吃蛇
2024-09-27 23:00:06Python资料围观38次
本篇文章分享Python——贪吃蛇,对你有帮助的话记得收藏一下,看Python资料网收获更多编程知识
以下是一个简单的贪吃蛇游戏的Python代码示例:
import pygame
import time
import random
# 初始化 Pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 设置屏幕尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("贪吃蛇游戏")
# 游戏帧率
clock = pygame.time.Clock()
snake_block_size = 20
# 字体设置
font_style = pygame.font.SysFont(None, 50)
score_font = pygame.font.SysFont(None, 35)
def your_score(score):
value = score_font.render("Your Score: " + str(score), True, WHITE)
screen.blit(value, [0, 0])
def our_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block_size, snake_block_size])
# 游戏主循环
def game_loop():
game_over = False
game_quit = False
x1 = screen_width / 2
y1 = screen_height / 2
x1_change = 0
y1_change = 0
snake_list = []
snake_length = 1
foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
while not game_quit:
while game_over == True:
screen.fill(BLACK)
message = font_style.render("Game Over! Press Q-Quit or C-Play Again", True, WHITE)
screen.blit(message, [screen_width / 6, screen_height / 3])
your_score(snake_length - 1)
pygame.display.update()
# 游戏结束等待用户输入
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_quit = True
game_over = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_quit = True
game_over = False
if event.key == pygame.K_c:
game_loop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block_size
x1_change = 0
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_over = True
x1 += x1_change
y1 += y1_change
screen.fill(BLACK)
pygame.draw.rect(screen, RED, [foodx, foody, snake_block_size, snake_block_size])
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
for x in snake_list[:-1]:
if x == snake_head:
game_over = True
our_snake(snake_block_size, snake_list)
your_score(snake_length - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
snake_length += 1
clock.tick(10)
pygame.quit()
quit()
game_loop()
这个代码使用Pygame库来创建一个简单的贪吃蛇游戏。在游戏中,玩家通过键盘控制贪吃蛇的移动方向,目标是吃到食物并尽可能多地增长长度。如果贪吃蛇碰到边界或自己的身体,游戏结束。游戏过程中会显示当前的分数。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- 【Python】selenium安装+Microsoft Edge驱动器下载配置流程
- Python 中自动打开网页并点击[自动化脚本],Selenium
- Anaconda基础使用
- 【Python】成功解决 TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’
- manim边学边做--三维的点和线
- CPython是最常用的Python解释器之一,也是Python官方实现。它是用C语言编写的,旨在提供一个高效且易于使用的Python解释器。
- Anaconda安装配置Jupyter(2024最新版)
- Python中读取Excel最快的几种方法!
- Python某城市美食商家爬虫数据可视化分析和推荐查询系统毕业设计论文开题报告
- 如何使用 Python 批量检测和转换 JSONL 文件编码为 UTF-8
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Python与PyTorch的版本对应
- Anaconda版本和Python版本对应关系(持续更新...)
- Python pyinstaller打包exe最完整教程
- Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based proj