Python贪吃蛇简单的代码
在⾃学Python的过程中在⽹上查询资料时发现了⼀些好玩的东西,python的游戏库模块,它可以⾃⼰弄⼀个⼩游戏来玩玩,然后我在⽹上了⼀些游戏的代码,,⾃⼰改了⼀些,弄出了⼀个简单贪吃蛇,代码也是照着敲的只是稍微的改了⼀下
import pygame, sys, random, time
from pygame.locals import *  # 从pygame模块导⼊常⽤的函数和常量
#定义颜⾊变量
black_colour = pygame.Color(28, 56, 20)
white_colour = pygame.Color(255, 144, 20)
red_colour = pygame.Color(255,34 , 20)
grey_colour = pygame.Color(150, 150, 150)
#定义游戏结束函数
def GameOver(gamesurface):
# 设置提⽰字体的格式
GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)
# 设置提⽰字体的颜⾊
GameOver_colour = der('GameOver', True, grey_colour)#只能英⽂
# 设置提⽰位置
GameOver_location = _rect()
GameOver_location.midtop = (310, 200)
# 绑定以上设置到句柄
gamesurface.blit(GameOver_colour, GameOver_location)
# 提⽰运⾏信息
pygame.display.flip()
# 休眠5秒
time.sleep(5)
# 退出游戏
pygame.quit()
# 退出程序
#定义主函数
def main():
# 初始化pygame,为使⽤硬件做准备
pygame.init()
pygame.time.Clock()
ftpsClock = pygame.time.Clock()
# 创建⼀个窗⼝
gamesurface = pygame.display.set_mode((640, 480))
# 设置窗⼝的标题
pygame.display.set_caption('tanchishe snake')
# 初始化变量
# 初始化贪吃蛇的起始位置
snakeposition = [100, 100]
# 初始化贪吃蛇的长度
snakelength = [[100, 100], [80, 100], [60, 100]]
# 初始化⽬标⽅块的位置
square_purpose = [300, 300]
# 初始化⼀个数来判断⽬标⽅块是否存在
square_position = 1
# 初始化⽅向,⽤来使贪吃蛇移动
derection = "right"
change_derection = derection
# 进⾏游戏主循环
while True:
# 检测按键等pygame事件
for event in ():
pe == QUIT:
# 接收到退出事件后,退出程序
pygame.quit()
pe == KEYDOWN:
# 判断键盘事件,⽤w,s,a,d来表⽰上下左右
if event.key == K_RIGHT or event.key == ord('d'):
change_derection = "right"
if event.key == K_LEFT or event.key == ord('a'):
change_derection = "left"
if event.key == K_UP or event.key == ord('w'):
change_derection = "up"
if event.key == K_DOWN or event.key == ord('s'):
change_derection = "down"
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
# 判断移动的⽅向是否相反
if change_derection == 'left' and not derection == 'right':
derection = change_derection
if change_derection == 'right' and not derection == 'left':
贪吃蛇的编程代码derection = change_derection
if change_derection == 'up' and not derection == 'down':
derection = change_derection
if change_derection == 'down' and not derection == 'up':
derection = change_derection
# 根据⽅向,改变坐标
if derection == 'left':
snakeposition[0] -= 20
if derection == 'right':
snakeposition[0] += 20
if derection == 'up':
snakeposition[1] -= 20
if derection == 'down':
snakeposition[1] += 20
# 增加蛇的长度
snakelength.insert(0, list(snakeposition))
# 判断是否吃掉⽬标⽅块
if snakeposition[0] == square_purpose[0] and snakeposition[1] == square_purpose[1]:
square_position = 0
else:
snakelength.pop()
# 重新⽣成⽬标⽅块
if square_position == 0:
# 随机⽣成x,y,扩⼤⼆⼗倍,在窗⼝范围内
x = random.randrange(1, 32)
y = random.randrange(1, 24)
square_purpose = [int(x * 20), int(y * 20)]
square_position = 1
# 绘制pygame显⽰层
gamesurface.fill(black_colour)
for position in snakelength:
(gamesurface, white_colour, Rect(position[0], position[1], 20, 20))
(gamesurface, red_colour, Rect(square_purpose[0], square_purpose[1], 20, 20))        # 刷新pygame显⽰层
pygame.display.flip()
# 判断是否死亡
if snakeposition[0] < 0 or snakeposition[0] > 620:
GameOver(gamesurface)
if snakeposition[1] < 0 or snakeposition[1] > 460:
GameOver(gamesurface)
for snakebody in snakelength[1:]:
if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:
GameOver(gamesurface)
# 控制游戏速度
ftpsClock.tick(8)
if __name__ == "__main__":
main()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。