Python 站着的时候我能跳。Stand==正确,但可以';不要跳
我有一个包含跳跃的游戏。当可变站姿为真时,他可以正常跳跃,当我击中这个方块时,我将站姿设置为真。我通过打字测试:Python 站着的时候我能跳。Stand==正确,但可以';不要跳,python,pygame,Python,Pygame,我有一个包含跳跃的游戏。当可变站姿为真时,他可以正常跳跃,当我击中这个方块时,我将站姿设置为真。我通过打字测试: if stand == True: print("stand is true") 它打印出来了。虽然他不能跳 我的代码是: import pygame, random, time, os, sys, tkinter ###############################################################################
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()
导入pygame、random、time、os、sys、tkinter
###################################################################################
#这是一个10层的简约平台小项目。
#它不是很特别,但制作起来很有趣!
#它在pygame中使用图像而不是绘制矩形
#老实说,我觉得速度快了一点。
#版权所有Hunter Kepley 2014
#
#
###################################################################################
pygame.init()
disw=720
碟数=680
黑色=(0,0,0)
白色=(255255)
红色=(255,50,50)
绿色=(50255,50)
蓝色=(50255)
flag=False
立场=错误
gameDisplay=pygame.display.set_模式((disw,dish))
pygame.display.set_标题(“Rec”)
clock=pygame.time.clock()
职业球员:
定义初始化(self,x,y,image,w,h,xc,yc):#初始化
self.x=x
self.y=y
self.image=image
self.w=w
self.h=h
self.xc=xc
self.yc=yc
def更新(自我):#更新类显示
blit(self.image,(self.x,self.y))
班级级别:
定义初始值(自我、繁殖、繁殖、变量、plcl、上一个):
self.spawnx=spawnx
self.spawny=spawny
self.var=var
self.plcl=plcl
def繁殖(自我):
self.plcl.x=self.x
self.plcl.y=self.spawny
def设置(自):
self.var=True
self.prev=False
类块:#类块,即碰撞
定义初始(自身、x、y、w、h、plcl、img、支架):
self.x=x
self.y=y
self.w=w
self.h=h
self.plcl=plcl
self.img=img
自立
如果plcl.x+plcl.w>=self.x和plcl.y+plcl.h>=self.y和plcl.x=dish-player1.h:#底部碰撞
player1.y=dish-player1.h
正确
如果寿命>0:
寿命-=1
elif lifes=disw-player1.w:
levelone.spawn()
levelone.set()
#起跑障碍
block1start=block(5,dish-50250,50,player1,pygame.image.load(“images/block1.png”),stand)#这里定义了第一个块
block1start.update()
pygame.display.update()
时钟滴答(60)
如果名称=“\uuuuu main\uuuuuuuu”:
gameloop()
pygame.quit()
退出
如你所见,当你触摸方块时,stand设置为True,但他无法进行跳跃
重述:
问题:立场是正确的,旗帜是错误的,但由于某种原因不能跳跃
尝试:不使用类,如果stand为真,则打印,等等
需要:关于如何修复它的答案。我不需要像大多数人在发帖时那样对我的编码提出批评,只需要知道如何修复它 您正在块中将局部变量设置为True
。\uuuu init\uuuu
:
stand = True
该变量在其他任何地方都不可见。您也不能简单地将其设为全局变量,因为您还使用stand
作为该方法的参数
您必须使用:
global stand
在该方法中,使名称充当全局名称,但也将参数重命名为该方法:
def __init__(self, x, y, w, h, plcl, img, block_stand):
# ...
self.stand = block_stand
您正在块中将局部变量设置为True
。\uuuu init\uuuu
:
stand = True
该变量在其他任何地方都不可见。您也不能简单地将其设为全局变量,因为您还使用stand
作为该方法的参数
您必须使用:
global stand
在该方法中,使名称充当全局名称,但也将参数重命名为该方法:
def __init__(self, x, y, w, h, plcl, img, block_stand):
# ...
self.stand = block_stand
您至少有三个变量称为
stand
:全局stand
,block.stand
,以及参数stand
。必须使用self.stand
或global stand
明确使用哪一个。至少有三个变量称为stand
:globalstand
,block.stand
,以及参数stand
。您必须使用self.stand
或global stand
明确使用哪个。谢谢!我真不敢相信我忘了,哈哈。你应该调试你的代码。一些指纹会被抓到的this@keyser:OP已经在尝试使用打印语句。@MartijnPieters这是一个很好的观点:)但问题不是在哪里发生的?似乎是一个反思的好机会谢谢!我真不敢相信我忘了,哈哈。你应该调试你的代码。一些指纹会被抓到的this@keyser:OP已经在尝试使用打印语句。@MartijnPieters这是一个很好的观点:)但问题不是在哪里发生的?这似乎是一个反思的好机会