[英]Why doesn't my sprite move correctly in pygame?
我的第二個游戲是平台游戲。 我畫了所有的怨恨,畫了背景和平台,但我不能以一種方式移動我的角色超過 45 像素,即使它應該移動 5 像素。 我還從這里和那里復制了一些代碼,所以我不能只是搜索它。 這非常令人費解,請幫助。 這是代碼:
import sys
import pygame
screen = pygame.display.set_mode((1300, 700), pygame.RESIZABLE)
global bg
bg = pygame.image.load('Pictures/badground.png')
w = (0, 0, 0)
screen.fill(w)
screen.blit(bg, (0, 0))
level = 0
walking_l = (pygame.image.load('Pictures/walk_l.png'))
walking_r = (pygame.image.load('Pictures/walk_r.png'))
walking_l_2 = (pygame.image.load('Pictures/_stand.png'))
walking_r_2 = (pygame.image.load('Pictures/stand.png'))
stand = (pygame.image.load('Pictures/rstand.png'))
pf = (pygame.image.load('Pictures/platform.png'))
pygame.display.set_caption('Platformed')
class player(object):
def __init__(self, speed, width, height):
self.speed = speed
self.x = 650
self.y = 350
self.width = width
self.height = height
self.walk_l = False
self.walk_r = False
self.stand = True
self.jump = False
self.walk_count = 0
def reprint_win(self):
screen.fill(w)
screen.blit(bg, (0, 0))
def walk(self):
k = pygame.key.get_pressed()
if k[pygame.K_a]:
self.x -= self.speed
self.stand = False
self.walk_l = True
self.walk_r = False
self.jump = False
print(self.x)
screen.fill(w)
screen.blit(bg, (0, 0))
elif k[pygame.K_d]:
self.x += self.speed
self.stand = False
self.walk_r = True
self.walk_l = False
self.jump = False
print(self.y)
screen.fill(w)
screen.blit(bg, (0, 0))
elif k[pygame.K_w] and self.walk_r and not self.jump:
self.stand = False
self.walk_r = True
self.walk_l = False
self.jump = True
screen.fill(w)
screen.blit(bg, (0, 0))
else:
self.stand = True
self.walk_r = False
self.walk_l = False
self.jump = False
screen.fill(w)
screen.blit(bg, (0, 0))
while True:
for e in pygame.event.get():
man = player(5, 64, 64)
if e.type == pygame.QUIT:
sys.exit()
if e.type == pygame.KEYDOWN:
man.walk()
screen.blit(stand, (man.x, man.y))
pygame.display.update()
elif e.type == pygame.KEYDOWN:
man.walk()
screen.blit(stand, (man.x, man.y))
pygame.display.update()
class player
的方法walk()
更改了實例屬性,但 id 沒有繪制任何內容。
添加方法draw
,根據實例屬性繪制玩家:
class player(object):
# [...]
def walk(self):
self.stand, self.walk_l, self.walk_r, self.jump = False, False, False, False
k = pygame.key.get_pressed()
if k[pygame.K_a]:
self.x -= self.speed
self.walk_l = True
elif k[pygame.K_d]:
self.x += self.speed
self.walk_r = True
elif k[pygame.K_w] and self.walk_r and not self.jump:
self.jump = True
else:
self.stand = True
def draw(self, surf):
if self.walk_l:
surf.blit(walking_l, (self.x, self.y))
elif self.walk_r:
surf.blit(walking_r, (self.x, self.y))
else:
surf.blit(stand, (self.x, self.y))
您必須在每一幀中繪制整個場景。 繪制應用程序循環而不是事件循環。
此外,必須在應用程序循環之前創建palyer
的實例,並在每一幀中調用man.mvoe()
。
使用pygame.time.Clock()
.tick()
控制每秒幀數:
man = player(5, 64, 64)
clock = pygame.time.Clock()
FPS = 60
while True:
clock.tick(FPS)
for e in pygame.event.get():
if e.type == pygame.QUIT:
sys.exit()
man.walk()
screen.blit(bg, (0, 0))
man.draw(screen)
pygame.display.update()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.