簡體   English   中英

Python / PyGame函數無法正常運行

[英]Python/PyGame functions not working as hoped

我有這個項目來創建一個帶有主菜單的游戲。 目前,我僅出於原型目的創建了一個非常基本的主菜單。 我的主菜單已加載,然后我想按“ f”鍵來玩游戲,但它沒有按預期工作。 當我啟動它時,g在添加功能之前還不錯,但是我需要這些功能。 有人有想法嗎? 謝謝。

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

window = pygame.display.set_mode(resolution)
window.fill(grey)
pygame.display.set_caption('Harrys Game')

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()

    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_f:
                break
            game()

def game():
    while False:
        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)
        window.blit(side5, (8, 327.3))
        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))
        pygame.display.update()

        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)

mainMenu()


pygame.display.update()

一些東西:

main_menu()有一段while True:循環,沒有breakraisereturn ,因此如果被調用,將無限期地繼續循環。

game()包含while False:永遠不會開始的“循環”。

這行沒有意義:

mainMenu() == False

而且:

game() == True

由於某種原因,您可以定義函數(例如def counters:然后直接調用它們(例如counters() )。

當您運行代碼時,會發生以下情況:

  1. def main():之前的所有定義def main():發生;
  2. main被定義然后立即調用;
  3. mainMenu已定義;
  4. game被定義然后立即被調用,但是什么也不做(見上文);
  5. 定義counters然后立即調用;
  6. 您調用pygame.display.update()

就是這樣 mainMenu永遠不會被調用(這是一件好事,因為它將永遠無用地循環)。

偽代碼建議:

set up variables

set up window 

main menu:
    setup on screen text
    while True:
        get user input 
        if correct user input:
            break
    call game

game:
    while True:
        play game

call main menu

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM