为什么If语句不能使用Python和Pygame

问题描述 投票:-2回答:1

所以我创造了我的第一个游戏,我想用搁置跟踪高分。我的逻辑是我有一个名为highscore的全局变量。在game_menu()游戏开始时,它被赋予高分文件中的值。逻辑是,如果当前得分(躲闪)大于高分,那么高分数文件将被分配躲闪值,并显示在游戏菜单中。这在大多数情况下都有效,但即使躲闪变量小于高分,它仍然会被分配给文件。这就像游戏没有看到If语句。以下是游戏代码的重要部分。

d = shelve.open("highscore.txt")
highscore = d["highscore"]
d.close()

best_score(highscore)

上面,我们已经为文件内部的值分配了全局变量,并通过best_score显示

if dodged > highscore:
    d = shelve.open("highscore.txt")
    d["highscore"] = dodged
    d.close

这是我认为问题所在。它清楚地表明如果躲闪更大,但即使它相等或更小,文件也会被躲闪变量替换。我甚至尝试过插入ifesle,但没有任何作用。

以下是所要求的完整游戏代码

import pygame
import time
import random
import shelve

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
red = (255,0,0)
dark_red = (200,0,0)
green = (0,255,0)
dark_green = (0,200,0)

awing_width = 95

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Lonely ship")
clock = pygame.time.Clock()

background = pygame.image.load("space.jpg")

awingImg = pygame.image.load("A-wing.png")
awingImg = pygame.transform.scale(awingImg, (95,119))

asteroidImg  = pygame.image.load("AsteroidPNG.png")
asteroidImg = pygame.transform.scale(asteroidImg, (100,100))

def score(score):
    scoreFont = pygame.font.Font("freesansbold.ttf", 30)
    text = scoreFont.render("Dodged: "+str(score), True, red)
    gameDisplay.blit(text, (10,10))

def best_score(best_score):
    scoreFont = pygame.font.Font("freesansbold.ttf", 30)
    text = scoreFont.render("Highscore: "+str(best_score), True, red)
    gameDisplay.blit(text, (10,10))

def asteroid(asteroidx, asteroidy, asteroidw, asteroidh):
    gameDisplay.blit(asteroidImg, (asteroidx, asteroidy, asteroidw, asteroidh))

def awing(x,y):
    gameDisplay.blit(awingImg, (x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, red)
    return textSurface, textSurface.get_rect()

def button_text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font("freesansbold.ttf", 50)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)
    pygame.event.clear()
    game_menu()

def outOfBounds():
    message_display("You flew out of bounds!")

def crash():
    message_display("You crashed!")

def win():
    message_display("YOU WIN!!!")

def button(message,x,y,w,h,dc,bc,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x+w > mouse[0] > x and y+h > mouse [1] > y:
        pygame.draw.rect(gameDisplay, bc, (x,y,w,h))
        if click[0] == 1 and action != None:
            if action == "play":
                game_loop()
            elif action == "quit":
                pygame.quit()
                quit()
    else:     
        pygame.draw.rect(gameDisplay, dc, (x,y,w,h))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = button_text_objects(message, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)

global highscore

def game_menu():

    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()


        gameDisplay.blit(background, (0,0))


        d = shelve.open("highscore.txt")
        highscore = d["highscore"]
        d.close()

        best_score(highscore)

        largeText = pygame.font.Font("freesansbold.ttf", 50)
        TextSurf, TextRect = text_objects("Lonely Ship", largeText)
        TextRect.center = ((display_width/2), (display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Play", 200, 400, 100, 50, dark_green,green, "play")
        button("Quit", 500, 400, 100, 50, dark_red,red, "quit")


        pygame.display.update()
        clock.tick(15)

def game_loop():
    x = (display_width * 0.425)
    y = (display_height * 0.75)

    x_change = 0

    asteroid_startx = random.randrange(0, display_width)
    asteroid_starty = -600
    asteroid_speed = 5
    asteroid_width = 100
    asteroid_height = 100

    dodged = 0
    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change += -10
                elif event.key == pygame.K_RIGHT:
                    x_change += 10

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:   
                    x_change += 10
                elif event.key == pygame.K_RIGHT:
                    x_change += -10

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    game_menu()

        x += x_change


        gameDisplay.blit(background, (0,0))

        asteroid(asteroid_startx, asteroid_starty, asteroid_width, asteroid_height)
        asteroid_starty += asteroid_speed
        awing(x,y)
        score(dodged)

        if x > display_width - awing_width or x < 0:
            outOfBounds()

        if asteroid_starty > display_height:
           asteroid_starty = 0 - asteroid_height
           asteroid_startx = random.randrange(0, display_width)
           dodged += 1
           asteroid_speed += 0.5

        if dodged > highscore:
           d = shelve.open("highscore.txt")
           d["highscore"] = dodged
           d.close



        if y < asteroid_starty + asteroid_height:
            print("y crossover")

            if x > asteroid_startx and x < asteroid_startx + asteroid_width or x + awing_width > asteroid_startx and x + awing_width < asteroid_startx + asteroid_width:
                print("x crossover")
                crash()

            if dodged >= 50:
               d = shelve.open("highscore.txt")
               d["highscore"] = 0
               d.close
               win()

        pygame.display.update()
        clock.tick(60)

game_menu()
game_loop()
pygame.quit()
quit()
python if-statement pygame logic
1个回答
1
投票

我认为发生的事情是你将数字与字符串进行比较。如果您使用的是python 2.x,则here描述的数字总是更小。如果您使用的是3.x,则会引发错误。

0 > '0' // false

因此,您需要做的是将highscore投射到int

highscore = int(d["highscore"])

或者,相反 - dodged是一个字符串,因为在highscore是一个字符串的情况下,你永远不会进入if

© www.soinside.com 2019 - 2024. All rights reserved.