2014-01-07 38 views
0

所以,我正在開發一個學校項目的遊戲。我對Python/Pygame很陌生,所以還有一些我不明白的東西。我做了整個項目,沒有定義函數,然後我瞭解了它們並加入了它們。自從我添加它們之後,Pygame窗口甚至沒有出現。怎麼了?爲什麼Python/Pygame程序不能運行?

我是新來的StackOverflow,不知道如何將其添加到代碼示例。有人可以爲我做這個嗎?謝謝。

import pygame, math, sys, random 
pygame.init() 

#Variables 
darkYellow = 204,204,0 
grey = 102, 102, 102 
black = 0, 0, 0 
white = 255, 255, 255 
red = 255,0,0 
marroon = 120, 0, 0 
green = 0,255,0 
blue = 0,0,255 
darkBlue = 0,0,128 

resolution = 650, 600 
myFont = pygame.font.SysFont("Times New Roman", 30) 
myFont2 = pygame.font.SysFont("Times New Roman", 15) 
myFont3 = pygame.font.SysFont("Times New Roman", 60) 

redLeft = 150, 575 
redRight = 280, 575 
blackLeft = 370, 575 
blackRight = 500, 575 
radius = 20 

def main(): 
    global window 
    window = pygame.display.set_mode(resolution) 
    pygame.display.set_caption('Harrys Game') 
    window.fill(grey) 
    pygame.display.update() 

#Main menu 
def mainMenu(): 

    rectWidth = 150 
    rectHeight = 40 

    #Draw buttons 
    pygame.draw.rect(window, (white),(250, 150, rectWidth, rectHeight),0) 
    pygame.draw.rect(window, (white),(250, 225, rectWidth, rectHeight),0) 
    pygame.draw.rect(window, (white),(250, 300, rectWidth, rectHeight),0) 
    highlight = pygame.draw.rect(window, (darkYellow),(250, 150, rectWidth,    rectHeight),0) 
    pygame.display.update() 

    playGameText = myFont.render("Play Game", 1, red) 
    window.blit(playGameText, (260, 150)) 
    optionsText = myFont.render("Options", 1, red) 
    window.blit(optionsText, (275, 225)) 
    exitText = myFont.render("Exit", 1, red) 
    window.blit(exitText, (300, 300)) 
    pygame.display.update() 

def grid(): 
    startBar = pygame.draw.lines(window, black, False, [(0,545.46),(541.7,545.46)], 5) 
    bar2 = pygame.draw.lines(window, black, False, [(0,490.92),(541.7,490.92)], 5) 
    bar3 = pygame.draw.lines(window, black, False, [(0,436.38),(541.7,436.38)], 5) 
    bar4 = pygame.draw.lines(window, black, False, [(0,381.84),(541.7,381.84)], 5) 
    bar5 = pygame.draw.lines(window, black, False, [(0,327.3),(541.7,327.3)], 5) 
    bar6 = pygame.draw.lines(window, black, False, [(0,272.76),(541.7,272.76)], 5) 
    bar7 = pygame.draw.lines(window, black, False, [(0,218.22),(541.7,218.22)], 5) 
    bar8 = pygame.draw.lines(window, black, False, [(0,163.68),(541.7,163.68)], 5) 
    bar9 = pygame.draw.lines(window, black, False, [(0,109.14),(541.7,109.14)], 5) 
    bar10 = pygame.draw.lines(window, black, False, [(0,54.6),(541.7,54.6)], 5) 

    #Draw side columns 
    rightBar = pygame.draw.lines(window, black, False, [(541.7,600),(541.7,0)], 5) 
    leftBar = pygame.draw.lines(window, black, False, [(108.3,600),(108.3,0)], 5) 
    centreBar = pygame.draw.lines(window, black, False, [(325,0),(325,600)], 5) 

    #Right column text 
    label1 = myFont2.render("You rolled a:", 1, black) 
    window.blit(label1, (545, 20)) 
    rollLabel = myFont2.render("Hit space", 1, black) 
    window.blit(rollLabel, (545, 100)) 
    rollLabel2 = myFont2.render("to roll again", 1, black) 
    window.blit(rollLabel2, (545, 117)) 


    #Display column numbers 
    start1 = myFont.render("START", 1, black) 
    window.blit(start1, (8, 545.46)) 
    side2 = myFont.render("2", 1, black) 
    window.blit(side2, (54.15, 490.92)) 
    side3 = myFont.render("3", 1, black) 
    window.blit(side3, (54.15, 436.38)) 
    side4 = myFont.render("4", 1, black) 
    window.blit(side4, (54.15, 381.84)) 
    side5 = myFont.render("SAFE 5", 1, black) 
    side6 = myFont.render("6", 1, black) 
    window.blit(side6, (54.15, 272.76)) 
    side7 = myFont.render("7", 1, black) 
    window.blit(side7, (54.15, 218.22)) 
    side8 = myFont.render("8", 1, black) 
    window.blit(side8, (54.15, 163.68)) 
    side9 = myFont.render("9", 1, black) 
    window.blit(side9, (54.15, 109.14)) 
    side10 = myFont.render("10", 1, black) 
    window.blit(side10, (54.15, 54.6)) 
    finish11 = myFont.render("FINISH", 1, black) 
    window.blit(finish11, (8, 0)) 

def counters(): 
    redCountLeft = pygame.draw.circle(window, marroon, redLeft, radius) 
    redCountRight = pygame.draw.circle(window, marroon, redRight, radius) 
    blackCountLeft = pygame.draw.circle(window, black, blackLeft, radius) 
    blackCountRight = pygame.draw.circle(window, black, blackRight, radius) 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit(); sys.exit(); 
     if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: 
      #Dice roll 
      diceRoll = random.randint(1, 4) 
      diceRollLabel = myFont3.render(str(diceRoll), 1, black) 
      window.blit(diceRollLabel, (580, 35)) 
      print("Dice roll test", diceRoll) 
      pygame.display.update() 
+0

一般來說,回答「可能有些請爲我做到這一點?」沒有。請說明您遇到的具體錯誤/警告/問題。 – CoryKramer

+0

OP的「有人可以爲我做這個」是關於格式化的。 – That1Guy

+0

它沒有給出任何錯誤。只是不會跑。沒有語法等 –

回答

2

你需要打電話給你的功能應該是grid()功能的一部分行。當定義一個函數時,解釋器不會執行該代碼。以後需要調用它,當你想要的代碼塊的運行,例如:

>>> def main(): 
...  print('hi') 
... 
>>> # nothing printed 
... 
>>> main() 
hi 

通常在Python中,你將有一個像聲明:

if __name__ == '__main__': 
    main() 

這意味着,如果該文件是稱爲主python模塊(傳遞給python可執行文件python blah.py),然後運行main()函數。 main()函數將包含完整的程序邏輯,根據需要調用其他函數。

對你的代碼,你可能要像(未經測試)

if __name__ == '__main__': 
    main() # initialise the window 
    while True: 
     mainMenu() # draw the mainMenu 
     grid()  # draw the grid 
     counters() # draw the counters 

     # process events in this loop 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit(); sys.exit(); 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: 
       #Dice roll 
       diceRoll = random.randint(1, 4) 
       diceRollLabel = myFont3.render(str(diceRoll), 1, black) 
       window.blit(diceRollLabel, (580, 35)) 
       print("Dice roll test", diceRoll) 
       pygame.display.update() 
+0

輝煌的伴侶。非常感謝(順便說一句,我沒有複製你的代碼,我只是調整了我自己的函數,在每個函數之後都將這個函數稱爲對象。):D –

1

def grid()不應該被縮進或需要縮進4個空格

+0

對不起,那是我的縮進StackOverflow上,這是使用代碼塊的東西我第一次:P –

相關問題