资源简介
pygame实现贪吃蛇
代码片段和文件信息
import pygame
from pygame import locals
import random
# 初始化pygame,为使用硬件做准备
pygame.init()
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 创建一个窗口
screen = pygame.display.set_mode((660 480) 0 32)
# 背景
background = pygame.image.load(‘img/bg.png‘)
right = pygame.image.load(‘img/right.png‘)
food = pygame.image.load(‘img/apple.png‘)
body = pygame.image.load(‘img/body.png‘)
left = pygame.image.load(‘img/left.png‘)
up = pygame.image.load(‘img/up.png‘)
down = pygame.image.load(‘img/down.png‘)
basic_font = pygame.font.Font(‘img/neuropol.ttf‘ 18) # 字体
# 贪吃蛇的初始坐标
x y = 240 120
position = [(180 90) (180 120) (210 120) (x y)]
apple_x = 360 # 苹果横坐标
apple_y = 300 # 苹果纵坐标
# 贪吃蛇的朝向
setheading = “right“
snake_head = right
# 分数初始值
score = 0
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
elif event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != “left“:
setheading = ‘right‘
snake_head = right
elif event.key == locals.K_LEFT and setheading != “right“:
setheading = ‘left‘
snake_head = left
elif event.key == locals.K_UP and setheading != “down“:
setheading = ‘up‘
snake_head = up
elif event.key == locals.K_DOWN and setheading != “up“:
setheading = ‘down‘
snake_head = down
# 设置贪吃蛇的头部坐标
if setheading == “right“:
x += 30 #
elif setheading == “left“:
x -= 30
elif setheading == “up“:
y -= 30
else:
y += 30
position.append((x y))
if x == apple_x and y == apple_y:
# 随机生成一个格子的左上角坐标作为苹果(食物)的坐标
num1 = random.randint(1 22)
num2 = random.randint(1 16)
apple_x = 30 * num1 - 30 # 苹果的x坐标
apple_y = 30 * num2 - 30 # 苹果的y坐标
score += 10
else:
position.pop(0)
if x < 0 or x > 630 or y < 0 or y > 450:
exit()
# 将背景图画上去
screen.blit(background (0 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head position[-1]) #
# 将贪吃蛇的身体画上去
for i in range(len(position)-1):
screen.blit(body position[i])
# 将果实画上去
screen.blit(food (apple_x apple_y))
# 绘制分数
scoreSurf = basic_font.render(‘Score: ‘+str(score) True (0 0 0))
scoreRect = scoreSurf.get_rect()
scoreRect.topleft = (660 - 120 10)
screen.blit(scoreSurf scoreRect)
# 刷新画面
pygame.display.update()
FPSCLOCK.tick(3)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2020-11-28 20:04 snake\
目录 0 2020-11-28 20:04 snake\img\
文件 2150 2020-11-28 20:01 snake\img\apple.png
文件 23029 2020-07-13 15:03 snake\img\bg.png
文件 2019 2020-11-28 20:02 snake\img\body.png
文件 3009 2020-11-28 20:02 snake\img\down.png
文件 3026 2020-11-28 20:02 snake\img\left.png
文件 149752 2018-06-20 09:23 snake\img\neuropol.ttf
文件 3032 2020-11-28 20:02 snake\img\right.png
文件 3020 2020-11-28 20:03 snake\img\up.png
文件 2978 2021-01-21 14:35 snake\snake_pygame.py
- 上一篇:机器学习numpy和pandas基础
- 下一篇:Python爬虫实战入门教程
相关资源
- 贪吃蛇游戏.py
- python pygame 自制飞机大战游戏(加入
- pygame之《飞机大战》
- PythonTank
- Python和Pygame游戏开发
- 一个猥琐的贪吃蛇Python pygame
- python,pygame开发的太空大战源代码
- pygame小游戏-全民打飞机(关卡选择飞
- Python 飞机大战 增强版本完整代码实现
- Python小游戏我自己写的
- pygame for python 3.6 64位
- Python贪吃蛇源码+背景音乐+中文字体
- pygame-1.9.6-cp36-cp36m-win_amd64.whl
- python之外星人入侵完整版程序源码
- python之外星人入侵
- Python PyGame and Raspberry Pi Game Developmen
- pygame-1.9.6-cp37-cp37m-win_amd64.zip
- Python开发五子棋小游戏
- pygame-1.9.3-cp37-cp37m-win32.whl
- python pygame实现的简单的网游服务器端
- python飞机大战项目.zip
- pygame.whl -1.9.3-cp37-cp37m-win_amd64+win32两个
- python贪吃蛇进阶版完整代码
- python3.6模块集合-piprequestssetuptoolspyg
- aircraft battle.zip
- python-pygame消消乐
- pygame-1.9.3-cp37-cp37m-win_amd64.whl
- 飞机大战源码(python+pygame)
- pygame1.9.3-python3.7-windows-whl
- pygame64位for Python3.7
评论
共有 条评论