[英]why is my jump not working in python 3 / pygame
i get no error its just when i try to press the up arrow key it does nothing. 我没有错误,只是当我尝试按向上箭头键时,它什么也没做。
is there something wrong with my jump(self) in my player class? 我的球员班上的跳楼(自己)有什么问题吗? or do i need to add an update for it?
还是我需要为其添加更新? im not sure
我不确定
here is my code 这是我的代码
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("use arows")
movex = 0
class player:
def __init__(self ,x, y):
self.x = x
self.y = y
self.width = 32
self.height = 32
self.velocity = 0
self.falling = False
self.onGround = False
def jump(self):
if(self.onGround == False):
return
self.velocity = 8
self.onGround = False
def detectCollisions(self,x1,y1,w1,h1,x2,y2,w2,h2):
if (x2+w2>=x1>=x2 and y2+h2>=y1>=y2):
return True
elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):
return True
elif (x2+w2>=x1>=x2 and y2+h2>=y1+h1>=y2):
return True
elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):
return True
else:
return False
def update(self, gravity, blockList):
if (self.velocity < 0):
self.falling = True
collision = False
blockX,blockY = 0,0
for block in blockList:
collision = self.detectCollisions(self.x, self.y, self.width, self.height, block.x, block.y, block.width, block.height )
if collision == True:
blockx = block.x
blocky = block.y
break
if(collision == True):
if (self.falling == True):
self.falling == False
self.onGround== True
self.velocity = 0
self.y = blocky - self.height
if (self.onGround == False):
self.velocity += gravity
self.y -= self.velocity
def render(self,screen):
pygame.draw.rect(screen,(0,0,0),(self.x, self.y, self.width, self.height))
class Block:
def __init__ (self, x, y):
self.x = x
self.y = y
self.width = 32
self.height = 32
def render(self,screen):
pygame.draw.rect(screen,(0,0,0),(self.x, self.y, self.width, self.height))
gravity = -0.5
black = (0,0,0)
white = (255,255,255)
blue = (50,60,200)
clock = pygame.time.Clock()
player = player(0,0)
# 25 colums and 19 rows
level1 = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
]
blockList = []
for y in range (0,len(level1)):
for x in range (0,len(level1[y])):
if (level1[y][x] == 1):
blockList.append(Block(x*32, y*32))
gameloop = True
while gameloop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameloop = False
if(event.type == pygame.KEYDOWN):
if (event.key == pygame.K_RIGHT):
movex = 5
elif(event.key == pygame.K_LEFT):
movex = -5
elif (event.key == pygame.K_UP):
player.jump()
if(event.type == pygame.KEYUP):
if (event.key == pygame.K_RIGHT):
movex = 0
elif(event.key == pygame.K_LEFT):
movex = 0
screen.fill(blue)
for block in blockList:
block.render(screen)
player.x += movex
player.update(gravity, blockList)
player.render(screen)
clock.tick(60)
pygame.display.update()
pygame.quit()
You have a typo in your update() method: 您的update()方法中有一个错字:
if(self.falling == True):
self.falling == False
self.onGround == True
These are just statements that server no purpose. 这些只是服务器无用的陈述。 You need to change them to single equals signs so that it actually changes the values of these variables.
您需要将它们更改为单等号,以便它实际上更改这些变量的值。 Should be:
应该:
if self.falling == True:
self.falling = False
self.onGround = True
By the way, parenthesis around if statement conditions are not required in Python so I removed them from my answer. 顺便说一下,Python中不需要if语句条件的括号,因此我从答案中删除了它们。 I recommend you remove them too.
我建议您也删除它们。
Edit - Further explanation: 编辑-进一步说明:
var1 == var2 always returns True or False, depending on if they are equal. var1 == var2始终返回True或False,这取决于它们是否相等。 It does not change the values of var1 or var2.
它不会更改var1或var2的值。
var1 = var2 sets var1 equal to var2. var1 = var2将var1设置为等于var2。
Full example: 完整示例:
var1 = 1
var2 = 2
var1 == var2 # Nothing happens
var1 = var2 # var1 now equals 2
var3 = var1 == var2 # var3 now equals True. You might want some parenthesis around this one just for clarity.
print(var1,var2,var3) # Prints "2 2 True"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.