我不得不为一个类项目设计一个老虎机,由于某种原因,程序一直在崩溃。如果我输入的代码或者我错过了什么,我不确定是否有什么问题。我们被告知只使用第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
答案 0 :(得分:2)
我不确定这是否是故意的,但看起来你的IMAGELIST1和2中的文件名以大写字母(即'Santa.png')开头,而IMAGELIST3中的文件名以小写字母开头(即'santa.png')。
当你的程序进入ReturnWinning函数时,似乎没有任何小写字母。那可能是你的问题。尝试一下,看看它是否有效。