站立时我能跳。站= =真,但不能跳

时间:2014-12-19 15:28:43

标签: python pygame

我有一个包含跳跃的游戏。当变量stand为True时,他可以正常跳跃,当我击中此块时,我设置为True。我通过输入来测试:

if stand == True:
    print("stand is true")

打印出来。虽然他不能跳。

我的代码是:

import pygame, random, time, os, sys, tkinter

###################################################################################
#                   This is a small 10 level project of a minimalistic platformer.
#               It isn't too special, but fun to make!
#                   It uses images instead of drawing rectangles in pygame
#               I found it a bit quicker honestly.
#                   Copyright Hunter Kepley 2014
#
#
###################################################################################


pygame.init()

disw = 720
dish = 680

black = (  0,  0,  0)
white = (255,255,255)
red   = (255, 50, 50)
green = ( 50,255, 50)
blue  = ( 50, 50,255)

flag = False
stand = False

gameDisplay = pygame.display.set_mode((disw,dish))
pygame.display.set_caption("Rec")
clock = pygame.time.Clock()

class player:
    def __init__(self, x, y, image, w, h, xc, yc): # Initialization
        self.x = x
        self.y = y
        self.image = image
        self.w = w
        self.h = h
        self.xc = xc
        self.yc = yc

    def update(self): # Update class display
        gameDisplay.blit(self.image, (self.x, self.y))

class level:
    def __init__(self, spawnx, spawny, var, plcl, prev):
      self.spawnx = spawnx
      self.spawny = spawny
      self.var = var
      self.plcl = plcl

    def spawn(self):
        self.plcl.x = self.spawnx
        self.plcl.y = self.spawny

    def set(self):
        self.var = True
        self.prev = False

class block: # Class for blocks, namely collisions
    def __init__(self, x, y, w, h, plcl, img, stand):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.plcl = plcl
        self.img = img
        self.stand = stand

        if plcl.x + plcl.w >= self.x and plcl.y + plcl.h >= self.y and plcl.x <= self.x + self.w and plcl.y <= self.y + self.h:
            if plcl.x + plcl.w >= self.x and plcl.x <= self.x + self.w and plcl.y <= self.y + 20 and plcl.y + plcl.h >= self.y:
                plcl.y = self.y - plcl.h
                stand = True

    def update(self):
        gameDisplay.blit(self.img, (self.x, self.y))

player1 = player(10, 10, pygame.image.load("images/player.png"), 30, 40, 0 ,6) # Defining player1 as a player class

start = True
lone = False
ltwo = False
lthree = False
lfour = False
lfive = False
lsix = False
lseven = False
leight = False
lnine = False
lten = False

lives = 3

def gameloop():
    global flag, stand, start, lone, ltwo, lthree, lfour, lfive, lsix, lseven, leight, lnine, lten, lives
    gameExit = False

    starttime = pygame.time.get_ticks()

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

            elif event.type == pygame.KEYDOWN:
                if flag == False and event.key == pygame.K_SPACE and stand == True: # For jumping
                    starttime = pygame.time.get_ticks()
                    player1.yc = -6
                    flag = True
                    stand = False

                if event.key == pygame.K_d: # Right
                    player1.xc = 4

                if event.key == pygame.K_a: # Left
                    player1.xc = -4

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE: # For jumping
                    player1.yc = 6

                if event.key == pygame.K_d:
                    player1.xc = 0

                if event.key == pygame.K_a:
                    player1.xc = 0

        if flag == True and pygame.time.get_ticks() - starttime >= 300: # For jumping
            player1.yc = 6
            flag = False

        gameDisplay.fill(white) # Fill

        player1.update() # Update player display

        player1.x += player1.xc # update player movements
        player1.y += player1.yc

        if player1.y >= dish - player1.h: # Bottom collisions
            player1.y = dish - player1.h
            stand = True
            if lives > 0:
                lives -= 1
            elif lives <= 0: # Reset the game if you die
                start = True
                lone = False
                ltwo = False
                lthree = False
                lfour = False
                lfive = False
                lsix = False
                lseven = False
                leight = False
                lnine = False
                lten = False
                lives = 3
                player1.x = 10
                player1.y = 10

        if player1.x <= 0: # Left wall collisions
            player1.x = 0

        if player1.x >= disw - player1.w: # Right wall collisions
            player1.x = disw - player1.w


        # Level one class

        levelone = level(10, 200, lone, player1, start)

        # Start Collisions

        if player1.x >= disw - player1.w:
            levelone.spawn()
            levelone.set()

        # Blocks in Start

        block1start = block(5, dish - 50, 250, 50, player1, pygame.image.load("images/block1.png"), stand) # Here is the 1st block defined
        block1start.update()

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

if __name__ == "__main__":
    gameloop()

pygame.quit()
quit()

如您所见,当您触摸该块时,stand设置为True,但他无法执行跳转。

重述:

问题:Stand为True且flag为False但由于某种原因无法跳转。

尝试:不使用类,如果stand为True则打印等

需要:如何解决问题的答案。我不需要批评我的编码,就像大多数人发布时一样,只是如何解决它。

1 个答案:

答案 0 :(得分:4)

您正在True中将本地变量设置为block.__init__

stand = True

该变量在其他任何地方都不可见。您也不能简单地将其设为全局,因为您还使用stand作为方法的参数。

您必须使用:

global stand

在该方法中,使名称充当全局,但也将参数重命名为方法:

def __init__(self, x, y, w, h, plcl, img, block_stand):
    # ...
    self.stand = block_stand