首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >站着的时候我能跳。站在==的真但不能跳

站着的时候我能跳。站在==的真但不能跳
EN

Stack Overflow用户
提问于 2014-12-19 15:28:42
回答 1查看 66关注 0票数 0

我有一场包括跳跃在内的比赛。当变量位为真时,他可以正常跳,当我碰到这个块时,我将站设为True。我通过打字进行测试:

代码语言:javascript
复制
if stand == True:
    print("stand is true")

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

我的代码是:

代码语言:javascript
复制
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()

正如你所看到的,当你触摸地块时,站立被设置为真,但他无法执行跳跃。

重报:

问题:站立是真,旗子是假的,但由于某种原因不能跳下去。

试过:不使用类,打印如果是真实的,等等。

需要:一个关于如何修复它的答案。我不需要批评我的编码在一般情况下,就像大多数人做的时候,我张贴,只是如何修复它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-19 15:30:28

您正在将局部变量设置为True,在block.__init__中。

代码语言:javascript
复制
stand = True

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

你必须使用:

代码语言:javascript
复制
global stand

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

代码语言:javascript
复制
def __init__(self, x, y, w, h, plcl, img, block_stand):
    # ...
    self.stand = block_stand
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27569058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档