我已经加载了图像和所有内容,但是现在,当我调用它时,它太大了,我无法更改大小,它也不会停留在我调用它的位置,也看不到哪里出错了,为什么它会继续下降...图像会发白并显示出来,但是在错误的位置却刚刚下降,我可以移动这只猫和所有的猫,但是它正在下降并且不应该。这有点像游戏中的对象从屏幕顶部掉落,然后在顶部重新加载,但随后不重新加载。猫需要先流血并在地上,但不是。
import pygame
import os
import sys
from pygame.locals import*
pygame.init()
W = 800
H = 447
win = pygame.display.set_mode((W,H))
pygame.display.set_caption('A journey')
bg = pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/background.png')).convert()
walkRight = [pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R1.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R2.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R3.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R4.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R5.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R6.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R7.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R8.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R9.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R10.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R11.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/R12.png'))]
walkLeft = [pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L1.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L2.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L3.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L4.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L5.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L6.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L7.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L8.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L9.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L10.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L11.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/L12.png'))]
runRight =[pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR1.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR2.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR3.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR4.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR5.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR6.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR7.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR8.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR9.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR10.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR11.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR12.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RR13.png'))]
runLeft = [pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR1.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR2.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR3.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR4.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR5.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR6.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR7.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR8.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR9.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR10.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR11.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR12.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LR13.png'))]
jumpRight = [pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ1.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ2.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ3.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ4.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ5.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ6.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ7.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ8.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ9.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ10.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ11.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LJ12.png'))]
jumpLeft = [pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ1.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ2.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ3.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ4.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ5.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ6.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ7.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ8.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ9.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ10.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ11.png')),
pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/RJ12.png'))]
sit = pygame.image.load(os.path.join('D:/Python/Buttons/Programs/Pygame/game/LS6.png'))
clock = pygame.time.Clock()
x = 50
y = 150
width = 94
height = 3.9
vel = 5
isJump = False
jumpCount = 10
left = False
right = False
walkCount = 0
def redrawWindow():
global walkCount
win.blit(bg, (0,0))
if walkCount + 1 >= 27:
walkCount = 0
if left:
win.blit(walkLeft[walkCount//3], (x, y))
walkCount += 1
elif right:
win.blit(walkRight[walkCount//3], (x, y))
walkCount += 1
else:
win.blit(sit, (x, y))
pygame.display.flip()
pygame.display.update()
# main loop
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
elif keys[pygame.K_RIGHT] and x < W - width - vel:
x += vel
right = True
left = False
else:
right = False
left = False
walkCount = 0
if not(isJump):
y += vel
if keys[pygame.K_SPACE]:
isJump = True
right = False
left = False
walkCount = 0
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
redrawWindow()
pygame.quit()
答案 0 :(得分:0)
该代码似乎正在处理“跳转”代码,而没有实际接收到该事件。阅读事件循环:
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
# do left
elif keys[pygame.K_RIGHT] and x < W - width - vel:
#do right
else:
# neither left nor right
if not(isJump):
y += vel # <--- THIS PART
if keys[pygame.K_SPACE]:
# start a new jump
如果未进行跳转,则y
每帧增加vel
(5)。