更改变量无效

时间:2017-05-27 13:37:46

标签: python pygame

我正在写一个小躲避游戏,我的想法是创造一种加电,将对象(在这种情况下是Spikeball)重置为起点并使它们两倍大。当玩家击中块时它打印出'hit',但不知何故值不会改变!我使用的是python 2.7。

这是我的代码:

import pygame
import os
import shutil
import time
from PIL import Image,ImageDraw
import random

    pygame.init()

    display_hight = 1100
    display_width = 1100

    h=0
    g=0
    r=0
    t=0
    j=0
    k=0
    m=0
    l=0
    q=0
    w=0
    f=0
    s=0

    black = (0,0,0)
    white = (255,255,255)
    red = (255,0,0)

    gameDisplay = pygame.display.set_mode((display_width,display_hight))
    pygame.display.set_caption("ZOMPS")
    clock = pygame.time.Clock()

    mydir = os.path.dirname('/home/arne/Desktop/Python-project/Game/Demonsave.png')
    playerImg = pygame.image.load(os.path.join(mydir,'Demonsave.png'))
    playerImg = pygame.transform.scale(playerImg,(180,200))

    mydir1 = os.path.dirname('/home/arne/Desktop/Python-project/Block.png')
    blockImg = pygame.image.load(os.path.join(mydir1,'Block.png'))


    def spikeball(m,l,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[m,l,spike_width,spike_hight])

    def spikeball2(j,k,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[j,k,spike_width,spike_hight])

    def spikeball3(q,w,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[q,w,spike_width,spike_hight])

    def spikeball1(f,s,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[f,s,spike_width,spike_hight])

    def spikeball4(r,t,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[r,t,spike_width,spike_hight])

    def power():
            p=1
            if p == 1:
                    spike_width=160
                    spike_hight=160
                    q=-100
            w=random.randrange(90,display_width-90)
            r=random.randrange(90,display_width-90)
                t=-200
                f=random.randrange(90,display_width-90)
                s=-400
                start_m=random.randrange(90,display_width-90)
                start_l =-800

            else:
                    print('fail') 


    def lost():
        message_display('You lost!')

    def 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_hight/2)
        gameDisplay.blit(TextSurf,TextRect)
        pygame.display.update()
        time.sleep(2)
        game_loop()

    def player(x,y):
        gameDisplay.blit(playerImg,(x,y))

    def block(g,h):
        gameDisplay.blit(blockImg,(g,h))

    def game_loop():
        j=random.randrange(0,display_width)
        k=-300
        x = display_width*0.4
        y = display_hight*0.75
        x_change=0
        f=random.randrange(90,display_width-90)
        s=-400
        start_m=random.randrange(90,display_width-90)
        start_l =-800
        spike_speed=random.randrange(5,10)
        spike_speed3=random.randrange(5,10)
        spike_speed2=random.randrange(5,10)
        spike_speed1=random.randrange(5,10)
        spike_speed4=random.randrange(5,10)
        spike_width=80
        spike_hight=80
        w=-100
        q=random.randrange(0,display_width)
        r=random.randrange(90,display_width-90)
        t=-200
        g=random.randrange(90,display_width-90)
        h=-1300
        block_speed=random.randrange(3,5)
        GameExit = False

        while not GameExit:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    GameExit =True
                elif event.type == pygame.KEYDOWN: 
                    if event.key == pygame.K_d:
                         x_change+=4
                    elif event.key == pygame.K_a:
                         x_change-=4
                    elif event.key == pygame.K_ESCAPE:
                        quit()
                elif event.type == pygame.KEYUP:
                    if event.key== pygame.K_a or pygame.K_d:
                        x_change=0
            if y+50 < h+40 and y+160 >h:
                        if x+60 >g and x+60 < g+40 or x+101 > g and x + 101 < g+40:
                            power()
                            print('hit')

            x+=x_change
            k+=spike_speed2
            s+=spike_speed3
            q+=spike_speed1
            t+=spike_speed4
            gameDisplay.fill(red)
            player(x,y)
            block(g,h)
            spikeball(start_m,start_l,spike_width,spike_hight,black)
            spikeball1(w,q,spike_width,spike_hight,black)
            spikeball2(j,k,spike_width,spike_hight,black)
            spikeball3(f,s,spike_width,spike_hight,black)
            spikeball3(r,t,spike_width,spike_hight,black)
            start_l+=spike_speed
            h+=block_speed

            if x > display_width or x < 0:
                x = display_width*0.4
                y = display_hight*0.75

            elif k > display_hight:
                j=random.randrange(90,display_width-90) 
                k=-100
                spike_speed2=random.randrange(6,12) 

            elif h > display_hight:
                g=random.randrange(90,display_width-90) 
                h=-900
                block_speed=random.randrange(1,6) 

            elif t > display_hight:
                r=random.randrange(90,display_width-90) 
                t=-100
                spike_speed4=random.randrange(6,12) 

            elif start_l > display_hight:
                start_l =-100
                start_m=random.randrange(90,display_width-90) 
                spike_speed=random.randrange(5,10) 

            elif s > display_hight:
                s=-100
                f=random.randrange(90,display_width-90)
                spike_speed3=random.randrange(6,12)

            elif q > display_hight:
                q=-100
                w=random.randrange(90,display_width-90)
                spike_speed1=random.randrange(6,12)

            elif y+50 < start_l+spike_hight and y+160 >start_l:
                if x+60 >start_m and x+60 < start_m+spike_width or x+101 > start_m and x + 101 < start_m+spike_width:
                    lost()
            elif y+50 < k+spike_hight and y+160 >k:
                if x+60 >j and x+60 < j+spike_width or x+101 > j and x + 101 <j+spike_width:
                    lost()
            elif y+50 < s+spike_hight and y+160 >s:
                if x+60 >f and x+60 < f+spike_width or x+101 > f and x + 101 < f+spike_width:
                    lost()
            elif y+50 < q+spike_hight and y+160 >q:
                if x+60 >w and x+60 < w+spike_width or x+101 > w and x + 101 < w+spike_width:
                    lost()
            elif y+50 < t+spike_hight and y+160 >t:
                if x+60 >r and x+60 < r+spike_width or x+101 > r and x + 101 < r+spike_width:
                    lost()

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


    game_loop()
    pygame.quit()
    quit()

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您的缩进看起来不正确,但看起来您正试图在函数内设置全局变量。

here所示,你可以使用它:

globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print(globvar)     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

但是使用全局变量是一种不好的做法,你可能更善于使用类。 Python是一种面向对象的语言,它非常易于使用。您可以找到文档here