gpt4 book ai didi

python - Pygame Sprite 二段跳

转载 作者:行者123 更新时间:2023-12-04 10:37:55 25 4
gpt4 key购买 nike

所以我对 pygame 比较陌生,并且已经开始从基础教程分支出来。我希望 Sprite 能够进行二段跳并尝试添加变量等,因此如果他们最终获得加电或某种形式,我可以将其更改为三段跳,并且我可以轻松更改它,但我挣扎。 P.S:由于某些代码已被删除,某些评论可能没有意义,但对我仍然有用。

x = 200
y = 450
width = 64
height = 66
vel = 5
screenwidth = 500
isjump = True #whether our character is jumping or not
jumpcount = 10
maxjump = 2 #double jump
maxjumpcount = 0

#we must keep track of direction, are they moving and how many steps for frames

left = False
right = False
walkcount = 0


def redrawGameWindow():
global walkcount

window.blit(background,(0,0)) #loads bg at 0,0

if walkcount +1 >= 15: #15 as we have 5 sprites, which will be displayed 3 times per second
walkcount = 0

if left:
window.blit(walkleft[walkcount//3],(x,y))#excludes remainders
walkcount+=1
elif right:
window.blit(walkright[walkcount//3],(x,y))
walkcount+=1
elif isjump:
window.blit(jumpp,(x,y))
walkcount+=1
else:
window.blit(char,(x,y))

pygame.display.update()


#now to write main loop which checks for collisions, mouse events etc
run = True
while run: #as soon as we exit this, the game ends
#main loop, gonna check for collision

clock.tick(15) #frame rate, how many pics per second, games r just a ton of pictures running very quickly
#now we check for events

for event in pygame.event.get(): #gets a list of all events that happened
print(event)
#can go through these events& check if they've happened
if event.type == pygame.QUIT: #if we try to exit the window
run = False

through the use of events. If a key has been pressed, we change the x & y of our shape
#if we want it to continuely move, we need to get a list



keys = pygame.key.get_pressed() #if these are pressed or held down, execute whatever is under these.


if keys[pygame.K_LEFT] and x > vel-vel: #if pressed we move our character by the velocity in whatever direction via grid which works from the TOP LEFT of screen
#0,0 is top left #vel-vel to equal 0, makes border better
x -= vel
left = True
right = False
elif keys[pygame.K_RIGHT] and x < screenwidth - width: #as the square co ord is top left of it
x+= vel
right = True
left = False
else:
right = False
left = False
walkcount = 0

#JUMP CODE
if not(isjump): # still lets them move left and right
if keys[pygame.K_SPACE]:
isjump = True #quadratic function for jump
right = False
left = False
walkcount = 0
maxjumpcount+=1
if maxjumpcount > 2:
isjump = False
else:
while jumpcount >= -10 and maxjumpcount < 2: #moving slower, then faster, hang, then go down
pygame.time.delay(12)
y -= (jumpcount*abs(jumpcount)) / 2#1.35? #squared, 10 ^2, /2, jumpcount = jump height
jumpcount -= 1


else: #jump has concluded if it reaches here
isjump = False
jumpcount = 10
maxjumpcount = 0 #double jump resets once jump ends

redrawGameWindow()

为相当可悲的尝试道歉。任何其他建议将不胜感激,因为我还有很多东西要学。谢谢。

最佳答案

你有一个游戏循环,使用它。跳跃不需要额外的循环。使用选择 ( if ) 而不是内部 while循环,解决问题:

run = True
while run: #as soon as we exit this, the game ends
# [...]

if not(isjump): # still lets them move left and right
if keys[pygame.K_SPACE]:
# [...]
else:

# instead of: while jumpcount >= -10 and maxjumpcount < 2:
if jumpcount >= -10 and maxjumpcount < 2: # <---------------

y -= (jumpcount*abs(jumpcount)) / 2
jumpcount -= 1

请注意,此代码块在主应用程序循环中不断调用。

关于python - Pygame Sprite 二段跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60079714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com