此要点(https://gist.github.com/tobias76/8dc2e1af90f1916a2106)中的代码完全被破坏,并且没有任何事情发生。一个黑屏和几秒后关闭,我会更具体的代码,但绝对没有错误信息。就该计划而言,它起作用了。根据要求,这是我的代码(由于某种原因,堆栈溢出并不像所有正在格式化的代码一样);
============================================== < / p>
import pygame
import sys
import random
from pygame.locals import *
pygame.init()
FPS = 60
FPSClock = pygame.time.Clock()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Graphical Slot Machine")
reelgroupone = pygame.sprite.Group()
reelgrouptwo = pygame.sprite.Group()
reelgroupthree = pygame.sprite.Group()
reelone = (reelgroupone, 1)
reeltwo = (reelgrouptwo, 2)
reelthree = (reelgroupthree, 3)
image = "assets/apple.png"
image2 = "assets/bar.png"
image3 = "assets/cherry.png"
image4 = "assets/orange.png"
background = "background.jpg"
class Reels(pygame.sprite.Sprite):
def __init__(self, reelgroup, reelnumbers):
self.reelgroup = reelgroup
self.reelnumber = reelnumbers
self.reellist = [0, 1, 2, 3, 4, 5]
self.reelmove = 1
self.reelnudge = 0
self.stoptime = 60
def start(self):
self.reelmove = 1
self.stoptime = 60
def stop(self):
self.reelmove = 0
def update(self):
if self.stoptime > 0:
self.stoptime -= 1
self.reelgroup.update()
else:
self.stop()
if self.reelnudge == 1:
self.reelmove = 0
def draw(self):
self.reelgroup.draw(screen)
class Fruit(pygame.sprite.Sprite):
def __init__(self, reelgroup, reel, FruitID):
pygame.sprite.Sprite.__init__(self)
self.reelgroup = reelgroup
self.FruitID = FruitID
self.machineReel = reel
# Depending on Reel ID use a specific image, place the file path in the string
if self.FruitID == 1:
self.reelImage = "assets/apple.png"
if self.FruitID == 2:
self.reelImage = "assets/bar.png"
if self.FruitID == 3:
self.reelImage = "assets/cherry.png"
if self.FruitID == 4:
self.picture = "assets/orange.png"
self.pic = pygame.image.load(self.reelImage).convert_alpha()
self.where = ((self.machineReel * 155) - 30, 490)
self.rectangle = self.pic.get_rect()
self.rectangle.TopLeft = self.where
# Make reels faster / slower here.
self.reelSpeed = 8
self.reelgroup.add(self)
def fruitupdate(self):
self.rectangle.y -= self.reelSpeed
if self.rectangle.y < 110:
self.kill()
class main():
def __init__(self):
self.money = 10
self.counter = 5
self.fruitlist = [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]]
self.finished = 0
self.message = ""
def splash(self):
pass
def machine(self):
while self.credits > 0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.keys = pygame.key.get_pressed()
# Change this key to whatever you want
if self.keys[K_p] and reelone.reelmove == reeltwo.reelmove == reelthree.reelmove == 0:
self.money -= 1
self.counter = 5
self.finished = 0
reelone.start()
reeltwo.start()
reelthree.start()
self.message = ""
if self.keys[K_ESCAPE]:
pygame.quit()
sys.exit()
# Put your image and font blitting here.
screen.blit(background,(0,0))
if self.money >= 10:
self.counter = 1
if reelone.reelMove == 1 and reelone.stopTime % 10:
fruit = Fruit(reelgroupone, 1, random.randint(1,4))
del self.fruitlist[0][0]
self.fruitlist[0].append(fruit.ID)
if reeltwo.reelMove == 1 and reeltwo.stopTime % 10:
fruit = Fruit(reelgrouptwo, 2, random.randint(1,4))
del self.fruitlist[1][0]
self.fruitlist[1].append(fruit.ID)
if reelthree.reelMove == 1 and reelthree.stopTime % 10:
fruit = Fruit(reelgroupthree, 3, random.randint(1,4))
del self.fruitlist[2][0]
self.fruitlist[2].append(fruit.ID)
else:
self.counter += 1
if reelone.reelmove == 1:
reelone.update()
if reeltwo.reelmove == 1:
reeltwo.update()
if reelthree.reelmove == 1:
reelthree.update()
# If all the reels are moving, check if the player has won and add the credits to their
# account if so.
if reelone.reelmove == reeltwo.reelmove == reelthree.reelmove == 0 and self.finished == 0:
if self.fruitlist[0][2] == self.fruitlist[1][2] == self.fruitlist[2][2]:
self.message = "Winner, want to play again?"
if self.fruitlist[0][2] == 1:
self.credits += 10
if self.fruitlist[0][2] == 1:
self.credits += 100
if self.fruitlist[0][2] == 3:
self.credits += 1000
if self.fruitlist[0][2] == 4:
self.credits += 10000
# Now this sets the state to finished
self.finished = 1
else:
self.message = "Sorry, no win this time!"
self.end = 1
reelgroupone.draw(screen)
reelgrouptwo.draw(screen)
reelgroupthree.draw(screen)
FPSClock.tick(FPS)
pygame.display.update()
fruit = main()
答案 0 :(得分:1)
你实际上并没有在你的代码中运行main()作为函数,而是main是一个类。将main更改为函数,而是将其写在程序的底部:
If __name__ == "__main__":
main()
这样,代码实际运行。另外,摆脱fruit = main()
。它只是让它变得更加混乱,无论如何你都会用它来运行它,因此它说为什么它完成了。您可能还想在main函数中移动主类中的内容。例如,将类中的函数(例如machine()函数)放在代码之外的单独函数中,并使用machine()在main函数中调用它。