第一次写python,尝试做一个简易版的贪吃蛇游戏,但是由于未系统学过python,遇到一些无法解决的问题,最主要是整个程序在添加主菜单函数之前都可以按照预期效果正常运行
之前在MU上运行很正常,但是在增加了主菜单函数之后运行发现snakebody列表无法弹出方块,长度不断增加无法实现蛇在行走的效果,是为什么呢? 新增加的主菜单函数中的按钮无法正常跳转到对应的函数,而游戏结束页面的按钮则可以正常使用,大大的不理解?
import pygame, sys, random
import pygame, sys, time
import pygame, sys
from pygame.locals import *
from pygame import mixer
pygame.init()
snakeColor = pygame.Color(0, 0, 255) # 蛇颜色
backColor = pygame.Color(154, 255, 154) # 背景颜色
whiteColor = pygame.Color(255, 255, 255) # 方块的颜色
buttonColor = pygame.Color(255, 250, 240)
violent = (159, 121, 238) # 字体颜色
pygame.mixer.init()
pygame.mixer.music.load("for you.mp3.mp3")
playsurface = pygame.display.set_mode((640, 480)) # 设置窗体大小
pygame.display.set_caption("贪吃蛇")
fontBigObj = pygame.font.Font("Bembo Bold.ttf", 48) # 通过字体文件获得字体对象
fontMinObj = pygame.font.Font("Bembo Bold.ttf", 38)
# 抽象一个函数显示文本数据
def showText(fontObj, text, x, y):
textSurfaceObj = fontObj.render(text, True, violent, None) # 配置要显示的文字
textRectObj = textSurfaceObj.get_rect() # 获得要显示的对象的rect
textRectObj.center = (x, y) # 设置显示对象的坐标
playsurface.blit(textSurfaceObj, textRectObj) # 绘制字体
# 设置跳转按钮
def button(text, x, y, w, h, action=None):
mouse = pygame.mouse.get_pos()
# 获取鼠标点击情况
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(playsurface, buttonColor, Rect(x, y, w, h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(playsurface, buttonColor, Rect(x, y, w, h))
showText(fontMinObj, text, (x + w / 2), (y + h / 2))
def gameover():
playsurface.fill(backColor)
showText(fontBigObj, "Game Over!", 300, 100)
showText(fontMinObj, "Score:" + str(score), 300, 150)
button("Play again", 200, 200, 190, 30, Start)
button("Exit", 250, 250, 100, 30, Exit)
pygame.display.update()
def Exit():
pygame.quit()
sys.exit()
# 页面主菜单
def main_Menu():
pygame.mixer.music.play()
playsurface.fill(backColor)
showText(fontBigObj, "Gluttonous Snake", 300, 100)
button("Start", 230, 180, 120, 30, Start)
button("Exit", 240, 250, 100, 30, Exit)
pygame.display.update()
# 游戏主类
def Start():
playsurface.fill(backColor) # 设置背景颜色
fpsClock = pygame.time.Clock() # 控制动画的帧率
snakeposition = [100, 100] # 设置蛇的起始位置
snakebody = [[100, 100], [80, 100], [60, 100]] # 设置蛇的身体段数
snakelength = 3 # 设置蛇的起始长度为3
global score
score = 0
t_list = [[300, 300]]
for i in range(1, 4):
t_list.append(
[int(random.randrange(1, 32) * 20), int(random.randrange(1, 24) * 20)]
)
t_flag = 1 # 标记该方块是否被吃掉 1:没有 0:被吃
direction = "right" # 起始时向右运动
change_direction = direction
while True:
# 构建蛇的实态和方块
for position in snakebody:
pygame.draw.rect(playsurface, snakeColor, Rect(position[0], position[1], 20, 20))
for item in t_list:
pygame.draw.rect(playsurface, whiteColor, Rect(item[0], item[1], 20, 20))
pygame.display.update() # 将待显示的surface全部显示到桌面
# 从队列中获取事件
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# KEYDOWN是pygame用于获取键盘按键信息的事件
elif event.type == KEYDOWN:
if event.key == K_RIGHT:
change_direction = "right"
if event.key == K_LEFT:
change_direction = "left"
if event.key == K_UP:
change_direction = "up"
if event.key == K_DOWN:
change_direction = "down"
if event.key == K_ESCAPE: # 按下退出键时退出
pygame.event.post(pygame.event.Event(QUIT))
# 确定方向
if change_direction == "left" and not direction == "right":
direction = change_direction
if change_direction == "right" and not direction == "left":
direction = change_direction
if change_direction == "up" and not direction == "down":
direction = change_direction
if change_direction == "down" and not direction == "up":
direction = change_direction
# 移动蛇的方向
if direction == "right":
snakeposition[0] += 20
if direction == "left":
snakeposition[0] -= 20
if direction == "up":
snakeposition[1] -= 20
if direction == "down":
snakeposition[1] += 20
for item in t_list:
if snakeposition[0] == item[0] and snakeposition[1] == item[1]:
t_flag = 0
t_list.pop(t_list.index(item))
break
if t_flag == 0:
score += 1
t_list.append(
[int(random.randrange(1, 32) * 20), int(random.randrange(1, 24) * 20)]
)
t_flag = 1
else:
snakebody.pop(-1)
snakebody.insert(0, list(snakeposition))
if snakeposition[0] > 620 or snakeposition[0] < 0:
gameover()
elif snakeposition[1] > 460 or snakeposition[1] < 0:
gameover()
fpsClock.tick(7) # 数值越大,移动速度越快
if __name__ == "__main__":
main_Menu()