Python 如何修复老虎机错误

Python 如何修复老虎机错误,python,pygame,Python,Pygame,我不得不为一个班级项目设计一个吃角子老虎机,由于某种原因,这个程序一直在崩溃。我不确定我输入的代码是否有问题,或者我是否遗漏了什么。我们被告知只使用红皮书第3、5、9章中inventwithpython手册中的信息。非常感谢您对本代码的任何帮助 import random, sys, pygame from pygame.locals import * pygame.init() WINDOWWIDTH = 1280 WINDOWHEIGHT = 720 BLACK = 0, 0, 0 BU

我不得不为一个班级项目设计一个吃角子老虎机,由于某种原因,这个程序一直在崩溃。我不确定我输入的代码是否有问题,或者我是否遗漏了什么。我们被告知只使用红皮书第3、5、9章中inventwithpython手册中的信息。非常感谢您对本代码的任何帮助

import random, sys, pygame
from pygame.locals import *
pygame.init()


WINDOWWIDTH = 1280
WINDOWHEIGHT = 720
BLACK = 0, 0, 0
BUTTONSIZE = 100
BUTTONGAPSIZE = 20
#Testing for Response with color:
GREEN = (255,0,0)
WHITE = (255,255,255)
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
clickedButton = ''
BASICFONT = pygame.font.SysFont('freesandbold.ttf',22)
FPSCLOCK = pygame.time.Clock()

XMARGIN = int((WINDOWWIDTH - (2 * BUTTONSIZE) - BUTTONGAPSIZE) / 2)
YMARGIN = int((WINDOWHEIGHT - (2 * BUTTONSIZE) - BUTTONGAPSIZE) / 2)

# Rect objects for each of the four buttons
BLANKRECT = pygame.Rect(XMARGIN, YMARGIN, BUTTONSIZE, BUTTONSIZE)
MINUSRECT   = pygame.Rect(XMARGIN + BUTTONSIZE + BUTTONGAPSIZE, YMARGIN, BUTTONSIZE, BUTTONSIZE)
PLUSRECT    = pygame.Rect(XMARGIN, YMARGIN + BUTTONSIZE + BUTTONGAPSIZE, BUTTONSIZE, BUTTONSIZE)
SPINBRECT  = pygame.Rect(XMARGIN + BUTTONSIZE + BUTTONGAPSIZE, YMARGIN + BUTTONSIZE + BUTTONGAPSIZE, BUTTONSIZE, BUTTONSIZE)

WHEEL1 = ['Tree', 'Reindeer', 'Reindeer', 'Frosty', 'Frosty', 'Frosty', 'Santa', 'Santa', 'Santa', 'Santa']
WHEEL2 = ['Tree', 'Reindeer', 'Reindeer', 'Frosty', 'Frosty', 'Frosty', 'Santa', 'Santa', 'Santa', 'Santa']
WHEEL3 = ['Tree', 'Reindeer', 'Reindeer', 'Frosty', 'Frosty', 'Frosty', 'Santa', 'Santa', 'Santa', 'Santa']


IMAGELIST1 = [pygame.image.load('Tree.png'),
              pygame.image.load('Reindeer.png'),
              pygame.image.load('Reindeer.png'),
              pygame.image.load('Frosty.png'),
              pygame.image.load('Frosty.png'),
              pygame.image.load('Frosty.png'),
              pygame.image.load('Santa.png'),
              pygame.image.load('Santa.png'),
              pygame.image.load('Santa.png'),
              pygame.image.load('Santa.png')]


IMAGELIST2 = [pygame.image.load('Tree.png'),
              pygame.image.load('Reindeer.png'),
              pygame.image.load('Reindeer.png'),
              pygame.image.load('Frosty.png'),
              pygame.image.load('Frosty.png'),
              pygame.image.load('Frosty.png'),
              pygame.image.load('Santa.png'),
              pygame.image.load('Santa.png'),
              pygame.image.load('Santa.png'),
              pygame.image.load('Santa.png')]


IMAGELIST3 = [pygame.image.load('tree.png'),
              pygame.image.load('reindeer.png'),
              pygame.image.load('reindeer.png'),
              pygame.image.load('frosty.png'),
              pygame.image.load('frosty.png'),
              pygame.image.load('frosty.png'),
              pygame.image.load('santa.png'),
              pygame.image.load('santa.png'),
              pygame.image.load('santa.png'),
              pygame.image.load('santa.png')]

blank = pygame.image.load('blank.png')
minus = pygame.image.load('minus.png')
plus = pygame.image.load('plus.png')
spinB = pygame.image.load('spin.png')

topcoord = 50
leftcoord = 50
startTop = 50
startLeft = 50
w1 = 1
w2 = 1
w3 = 1
n = 1
funds = 500
winnings = 0
bid = 5
choice = 'yes'
TEXTCOLOR = WHITE

# load the sound files
TREE = pygame.mixer.Sound('Tree.ogg')
REINDEER = pygame.mixer.Sound('Reindeer.ogg')
FROSTY = pygame.mixer.Sound('Frosty.ogg')
SANTA = pygame.mixer.Sound('Santa.ogg')    

def intro(funds):
    print('would you like to spin?')
    funds = 500
    print('Funds: 500')
    return funds

def PlaceBid(bet, funds):
    while True:
        bet = input('How much would you like to bet? (1)5 (2)10 (3)15 (4)20')
        if bet == '1':
            bet = 5
            funds -= bet
            return bet, funds
        elif bet == '2':
            bet  = 10
            funds -= bet
            return bet, funds
        elif bet == '3':
            bet  = 15
            funds -= bet
            return bet, funds
        elif bet == '4':
            bet = 20
            funds -= bet
            return bet, funds
        else:
            print('That was an incorrect option.')
            continue

def drawButtons():
    pygame.draw.rect(DISPLAYSURF, BLANK, BLANKRECT)
    pygame.draw.rect(DISPLAYSURF, MINUS, MINUSRECT)
    pygame.draw.rect(DISPLAYSURF, PLUS,  PLUSRECT)
    pygame.draw.rect(DISPLAYSURF, SPINB, SPINBRECT)

def PlusBet(funds, bid):
    bid += 5
    if bid > funds:
        bid = funds
        return bid
    elif bid > 20:
        bid = 20
        return bid

def MinusBet(funds, bid):
    bid -= 5
    if bid < 5:
        bid = 5
        return bid

def Spin(word1, word2, word3):
    n = random.randint(0,9)
    word1 = WHEEL1 [n]
    image1 = IMAGELIST1[n]
    n = random.randint(0,9)
    word2 = WHEEL2 [n]
    image2 = IMAGELIST2[n]
    n = random.randint(0,9)
    word3 = WHEEL3 [n]
    image3 = IMAGELIST2[n]

    print(word1 + ' ' + word2 + ' ' + word3)
    return word1, word2, word3, image1, image2, image3

def DisplayImage(DISPLAYSURF, image, topCoord, leftCoord):
    imageRect = image.get_rect()
    imageRect.top = topCoord
    imageRect.left = leftCoord
    DISPLAYSURF.blit(image, imageRect)
    leftCoord = imageRect.right + 10
    return leftCoord

def DisplayMinus (DISPLAYSURF, minus):
    minusRect = minus.get_rect()
    minusRect.top = 450
    minusRect.left = 50
    DISPLAYSURF.blit(minus, minusRect)
    pygame.display.update()
    minusButton = minusRect
    return minusButton

def DisplayPlus (DISPLAYSURF, plus):
    plusRect = plus.get_rect()
    plusRect.top = 450
    plusRect.left = 250
    DISPLAYSURF.blit(plus, plusRect)
    pygame.display.update()
    plusButton = plusRect
    return plusButton

def DisplaySpin (DISPLAYSURF, spinB):
    spinRect = spinB.get_rect()
    spinRect.top = 450
    spinRect.left = 500
    DISPLAYSURF.blit(spinB, spinRect)
    pygame.display.update()
    spinButton = spinRect
    return spinButton

def DisplayFunds(DISPLAYSURF, funds, WHITE, BASICFONT):
    fundsSurf = BASICFONT.render('Funds: ' +str(funds),1,WHITE)
    fundsRect = fundsSurf.get_rect()
    fundsRect.top = 100
    fundsRect.left = 675
    DISPLAYSURF.blit(fundsSurf, fundsRect)
    pygame.display.update()

def DisplayWinnings(DISPLAYSURF, winnings, WHITE, BASICFONT):
    winningsSurf = BASICFONT.render('Winnings: ' +str(winnings),1,WHITE)
    winningsRect = winningsSurf.get_rect()
    winningsRect.top = 150
    winningsRect.left = 675
    DISPLAYSURF.blit(winningsSurf, winningsRect)
    pygame.display.update()

def DisplayBet(DISPLAYSURF, bet, WHITE, BASICFONT):
    betSurf = BASICFONT.render('Bet: ' +str(winnings),1,WHITE)
    betRect = betSurf.get_rect()
    betRect.top = 475
    betRect.left = 165
    DISPLAYSURF.blit(betSurf, betRect)
    pygame.display.update()



**def GetButtonClicked(x,y):
    if minusButton.collidepoint((x,y)):
        return 'minusBet'
    if plusButton.collidepoint((x,y)):
        return 'plusBet'
    if spinButton.collidepoint((x,y)):
        return 'spin'**

def Terminate():
    pygame.quit()
    sys.exit()

def ReturnWinnings(word1, word2, word3, bank, bid):
    if word1 == word2 and word1 == word3:
        if word1 == 'Tree':
            sound = TREE
            winnings = bid * 5
            bank += winnings
            return bank
        elif word1 == 'Reindeer':
            sound = REINDEER
            winnings = bid * 4
            bank += winnings
            return bank
        elif word1 == 'Frosty':
            sound = FROSTY
            winnings = bid * 3
            bank += winnings
            return bank
        elif word1 == 'Santa':
            sound = SANTA
            winnings = bid * 2
            bank += winnings
            return bank
    else:
        return bank


funds = intro(funds)
DISPLAYSURF.fill(GREEN)
leftcoord = DisplayImage(DISPLAYSURF, blank, topcoord, leftcoord)
leftcoord = DisplayImage(DISPLAYSURF, blank, topcoord, leftcoord)
leftcoord = DisplayImage(DISPLAYSURF, blank, topcoord, leftcoord)
minusButton = DisplayMinus(DISPLAYSURF, minus)
plusButton = DisplayPlus(DISPLAYSURF, plus)
spinButton = DisplaySpin(DISPLAYSURF, spinB)
DisplayFunds(DISPLAYSURF, funds, WHITE, BASICFONT)
DisplayWinnings(DISPLAYSURF, winnings, WHITE, BASICFONT)
DisplayBet(DISPLAYSURF, bid, WHITE, BASICFONT)

while True:
    bid, funds = PlaceBid(bid, funds)
    w1, w2, w3, i1, i2, i3 = Spin(w1, w2, w3)
    topcoord = startTop
    leftcoord = startLeft
    funds = ReturnWinnings(w1, w2, w3, funds, bid)
    leftcoord = DisplayImage(DISPLAYSURF, i1, topcoord, leftcoord)
    leftcoord = DisplayImage(DISPLAYSURF, i2, topcoord, leftcoord)
    leftcoord = DisplayImage(DISPLAYSURF, i3, topcoord, leftcoord)
    pygame.display.update()
    FPSCLOCK.tick(30)
    print(funds)
    choice = input('Would you like to play again?')
    if choice.lower().startswith('y'):
        continue
    else:


break

我不确定这是否是有意的,但看起来IMAGELIST1和2中的文件名以大写字母“Santa.png”开头,而IMAGELIST3中的文件名以小写字母“Santa.png”开头


当您的程序进入ReturnWinning函数时,似乎没有任何小写字母。那可能是你的问题。试试看它是否有效。

定义崩溃。这会使你的电脑崩溃吗?它是否抛出python错误?什么错误?由于程序正在崩溃,您应该调查诊断和调试设施,以找出程序中发生崩溃的位置。当你知道崩溃发生在哪一行时,你应该能够找出它崩溃的原因。你能给出失败的Highlight代码吗?游戏崩溃的原因是什么?python代码本身不会导致错误,而是在它启动pygame时,pygame程序冻结,在我尝试按下旋转按钮的那一刻没有响应。我认为错误来自于我用**粗体显示的部分。这是整个代码还是有其他部分?因为你有星号的部分在程序中没有被调用,所以我不知道这是否真的是问题所在。