python - 为什么我的代码在打开时一直退出?*在游戏中*
问题描述
下面是我试图在 pygame 中制作的游戏的代码。我正在尝试运行它来测试它,但是每当我按下玩游戏按钮时,它就会退出我。这很奇怪,我试着检查我在游戏功能中可能做错了什么,但我似乎找不到任何东西。我打开了规则,规则似乎运行良好,退出游戏按钮也是如此。为什么玩游戏按钮会发生这种情况,我错过了什么吗?
# the following code will always put the screen in the top corner
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(20, 20)
import random
from pygame import *
init()
size = width, height = 1000, 700
screen = display.set_mode(size)
#define colours
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
green = (0, 255, 0)
red = (255, 0, 0)
pink = (255, 192, 203)
light_pink = (255, 183, 193)
light_pink2 = (255, 192, 203)
hot_pink = (255, 105, 180)
peachpuff = (255, 218, 185)
violet = (199, 21, 133)
scoreboard = (128, 71, 3)
# define fonts
menuFont = font.SysFont("Times New Roman",60)
#states in the Game
STATE_MENU = 0
STATE_GAME = 1
STATE_RULES = 2
STATE_QUIT = 3
player = []
playerMove = 2
def drawMenu(screen, button, mx, my, state):
global player
blockWidth = width//3
blockHeight = height//7
rectList = [Rect(blockWidth, blockHeight, blockWidth, blockHeight), # game choice
Rect(blockWidth, 3*blockHeight, blockWidth, blockHeight), #help choice
Rect(blockWidth, 5*blockHeight, blockWidth, blockHeight)] # quite choice
stateList = [STATE_GAME, STATE_RULES, STATE_QUIT]
titleList = ["Play Game", "Rules", "Quit Game"]
draw.rect(screen, peachpuff, (0, 0, width, height))
for i in range(len(rectList)):
rect = rectList[i] # get the current Rect
draw.rect(screen, violet, rect) # draw the Rect
text = menuFont.render(titleList[i] , 1, black) # make the font`
textWidth, textHeight = menuFont.size(titleList[i]) # get the font size
useW = (blockWidth - textWidth)//2 #use for centering
useH = (blockHeight - textHeight)//2
# getting a centered Rectangle
textRect = Rect(rect[0] + useW, rect[1] + useH, textWidth, textHeight)
screen.blit(text, textRect) # draw to screen
if rect.collidepoint(mx, my):
draw.rect(screen, black, rect, 2)
if button == 1:
state = stateList[i]
if STATE_GAME:
player = [random.randint(10, width + 10), random.randint(height//5 - 10, height + 10)]
return state
def drawScoreboard(screen, mx, my, button):
#drawing the score board
scoreboardHeight, scoreboardWidth = height/5, width/6
draw.rect(screen, scoreboard, (0, 0, width, scoreboardHeight))
draw.rect(screen, black, (0, 0, width, scoreboardHeight), 3)
string = "Welcome to your first day as a garbage collector!"
text = menuFont.render(string, 0, red)
screen.blit(text, Rect(10, 500, 500, 500))
backRect = Rect((0, 0, width//15, scoreboardHeight//3))
draw.rect(screen, green, backRect)
#if the ball collides with the back button it will either bold itself
#or else it will just have a 1 pixel rect around
if backRect.collidepoint(mx, my):
draw.rect(screen, black, backRect, 3)
if button == 1:
return STATE_MENU
else:
draw.rect(screen, black, backRect, 1)
def drawGame(screen, button, mx, my, state):
global player
scoreboardHeight = height //5
draw.rect(screen, blue, (0, 0, width, height))
draw.rect(screen, black, (0, 0, width, height), 2)
state = drawScoreboard(screen, mx, my, button)
#character drawing
draw.circle(screen, light_pink2, player, 10)
draw.circle(screen, black, player, 12, 1)
if button == 3:
state = STATE_MENU
if mx < player[0] and mx >= 10 + playerMove:
player[0] -= playerMove
if my < player[1] and my >= scoreboardHeight + 10:
player[1] -= playerMove
if mx > player[0] and mx <= width - 10:
player[0] += playerMove
if my > player[1] and mx <= height + 10:
player[1] += playerMove
return state
def drawRules(screen, button, mx, my, state):
draw.rect(screen, pink, (0, 0, width, height))
string = "Hi there!"
text = menuFont.render(string, 0, red)
screen.blit(text, Rect(430, 100, 500, 500))
if button == 3:
state = STATE_MENU
return state
running = True
#myClock = time.Clock()
# initializing variables
state = STATE_MENU
mx = my = 0
# Game Loop
while running:
button = 0
for e in event.get(): # checks all events that happen
if e.type == QUIT:
running = False
if e.type == MOUSEBUTTONDOWN:
mx, my = e.pos
button = e.button
elif e.type == MOUSEMOTION:
mx, my = e.pos
#button = e.button
if state == STATE_MENU:
state = drawMenu(screen, button, mx, my, state)
elif state == STATE_GAME:
state = drawGame(screen, button, mx, my, state)
elif state == STATE_RULES:
state = drawRules(screen, button, mx, my, state)
else:
running = False
display.flip()
#myClock.tick(60) # waits long enough to have 60 fps
event.get()
quit()
非常感谢您的帮助!
解决方案
在drawGame中:
def drawGame(screen, button, mx, my, state):
global player
scoreboardHeight = height // 5
draw.rect(screen, blue, (0, 0, width, height))
draw.rect(screen, black, (0, 0, width, height), 2)
state = drawScoreboard(screen, mx, my, button) # reassignment
# character drawing
draw.circle(screen, light_pink2, player, 10)
draw.circle(screen, black, player, 12, 1)
此标记的“状态”重新分配将函数参数“状态”隐藏到 drawScoreboard 的结果中。
在drawScoreboard中:
if backRect.collidepoint(mx, my):
draw.rect(screen, black, backRect, 3)
if button == 1:
return STATE_MENU # << only returns here
else:
draw.rect(screen, black, backRect, 1)
在这个块上,它只在一定的分歧中返回值。因此,在其他情况下您会得到 None,这会逃脱 Game 循环块的 if-elif 链并转到 else,将“running”设置为 false。
在游戏循环块上:
if state == STATE_MENU:
state = drawMenu(screen, button, mx, my, state)
elif state == STATE_GAME:
state = drawGame(screen, button, mx, my, state)
elif state == STATE_RULES:
state = drawRules(screen, button, mx, my, state)
else:
running = False # << state = None goes here.
如果您将 print(state) 放在 'if state == STATE_MENU:' 之前,您会得到以下结果,这可能有助于您调试游戏。
很多零...
0
0
0
0
1
无进程以退出代码 -1 结束
推荐阅读
- c++ - 在 vs2013 项目中包装一个库以在 vs2017 中使用?
- java - ABBYY 实时识别(cordova 插件)错误
- powershell - PowerShell FileSystemWatcher 退出 while ($true)
- hyperledger-composer - Hyperledger Composer - Angular 应用程序编译
- promise - Promise.all 仅在使用超过 4 个 Promise 时才获得“Access-Control-Allow-Origin”
- javascript - 使用 JQuery 更改 XHR 请求中的输入值
- amazon-web-services - AWS Lambda:监控由 SNS 触发的 lambda 超时。
- python - 将点投影到相机深度的平行网格上(maya)
- javascript - Angular - 为什么 Number.isNaN 为非空字符串返回 false?
- python - ImportError:无法导入名称'main'