Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的代码在打开时总是退出*在游戏中*_Python_Pygame - Fatal编程技术网

Python 为什么我的代码在打开时总是退出*在游戏中*

Python 为什么我的代码在打开时总是退出*在游戏中*,python,pygame,Python,Pygame,下面是我试图在pygame中制作的一个游戏的代码。我试图运行它来测试它,但每当我按下“玩游戏”按钮时,它就会退出。这很奇怪,我试着检查也许我在游戏功能中做错了什么,但我似乎找不到任何东西。我打开了规则,规则似乎运行得很好,退出游戏按钮也是如此。为什么“玩游戏”按钮会出现这种情况,我是否遗漏了什么 # the following code will always put the screen in the top corner import os os.environ['SDL_VIDEO_WIN

下面是我试图在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()
#以下代码将始终将屏幕置于上角
导入操作系统
操作系统环境['SDL\u视频窗口位置]=%d,%d%(20,20)
随机输入
从pygame导入*
init()
尺寸=宽度,高度=1000,700
屏幕=显示。设置模式(大小)
#定义颜色
黑色=(0,0,0)
白色=(255,255,255)
蓝色=(0,0255)
绿色=(0,255,0)
红色=(255,0,0)
粉红色=(255192203)
浅粉色=(255183193)
灯光_pink2=(25519203)
火红=(255、105、180)
桃泡芙=(255、218、185)
紫罗兰色=(19921133)
记分牌=(128,71,3)
#定义字体
menuFont=font.SysFont(“新罗马时代”,60)
#游戏中的国家
状态菜单=0
STATE_GAME=1
州规则=2
STATE_QUIT=3
玩家=[]
playerMove=2
def绘图菜单(屏幕、按钮、mx、我的、状态):
全球玩家
块宽度=宽度//3
块高=高度//7
rectList=[Rect(blockWidth,blockHeight,blockWidth,blockHeight),#游戏选择
Rect(blockWidth,3*blockHeight,blockWidth,blockHeight),#帮助选择
Rect(块宽,5*blockHeight,blockWidth,blockHeight)#非常好的选择
stateList=[状态\游戏,状态\规则,状态\退出]
标题列表=[“玩游戏”、“规则”、“退出游戏”]
draw.rect(屏幕、桃花(0、0、宽度、高度))
对于范围内的i(len(rectList)):
rect=rectList[i]#获取当前rect
绘制矩形(屏幕、紫色、矩形)#绘制矩形
text=menuFont.render(标题列表[i],1,黑色)#制作字体`
textWidth,textHeight=menuFont.size(标题列表[i])#获取字体大小
useW=(blockWidth-textWidth)//2#用于居中
useH=(blockHeight-textHeight)//2
#获取中心矩形
textRect=Rect(Rect[0]+useW,Rect[1]+useH,textWidth,textHeight)
screen.blit(text,textRect)#绘制到屏幕
如果矩形碰撞点(mx,my):
draw.rect(屏幕,黑色,rect,2)
如果按钮==1:
状态=状态列表[i]
如果是国家队比赛:
player=[random.randint(10,宽度+10),random.randint(高度//5-10,高度+10)]
返回状态
def绘图记分板(屏幕、mx、my、按钮):
#画记分板
记分板高度,记分板宽度=高度/5,宽度/6
draw.rect(屏幕、记分板,(0、0、宽度、记分板高度))
draw.rect(屏幕,黑色,(0,0,宽度,记分板高度),3)
string=“欢迎您成为垃圾收集器的第一天!”
text=menuFont.render(字符串,0,红色)
blit(文本,Rect(1050500500))
backRect=Rect((0,0,宽度//15,记分板高度//3))
draw.rect(屏幕、绿色、后退)
#如果球与后退按钮相撞,它将自身加粗
#否则它的周围只有一个1像素的矩形
如果backRect.collidepoint(mx,my):
draw.rect(屏幕、黑色、背面、3)
如果按钮==1:
返回状态菜单
其他:
draw.rect(屏幕、黑色、backRect、1)
def drawGame(屏幕、按钮、mx、我的、状态):
全球玩家
记分板高度=高度//5
draw.rect(屏幕,蓝色,(0,0,宽度,高度))
draw.rect(屏幕,黑色,(0,0,宽度,高度),2)
状态=绘图记分板(屏幕、mx、我的、按钮)
#字画
画圈(屏幕,灯光,播放器,10)
画圈(屏幕,黑色,玩家,12,1)
如果按钮==3:
状态=状态菜单
如果mx=10+玩家移动:
玩家[0]-=玩家移动
如果我的<玩家[1]和我的>=记分板高度+10:
玩家[1]-=玩家移动
如果drawGame中的mx>player[0]和mx player[1]以及mx:

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的结果中

在记分板上:

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)
如果backRect.collidepoint(mx,my):
draw.rect(屏幕、黑色、背面、3)
如果按钮==1:

返回状态_菜单#在函数drawGame中,您有:

state = drawScoreboard(screen, mx, my, button)
您不小心用记分板重新定义了状态。为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)
    drawScoreboard(screen, mx, my, button)
    #character drawing
    draw.circle(screen, light_pink2, player, 10) 
    draw.circle(screen, black, player, 12, 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)
    drawScoreboard(screen, mx, my, button)
    #character drawing
    draw.circle(screen, light_pink2, player, 10) 
    draw.circle(screen, black, player, 12, 1)