[英]How to rotate image while moving in pygame
我是 pygame 的新手,並試圖開發一個游戲,玩家用箭頭鍵移動並用鼠標的 position(如迷你 miltia)旋轉。 但是我可以旋轉播放器但不能移動它。 它只顯示旋轉的玩家,但它沒有移動。
def rot_center(image, rect, angle):
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image, rot_rect
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image_orig = load_image('player.png')
self.image = self.image_orig
self.rect = self.image.get_rect()
self.rect_orig = self.rect
self.mask = pygame.mask.from_surface(self.image)
self.x, self.y = int(pygame.display.Info().current_w / 2), int(pygame.display.Info().current_h / 2)
self.rect.topleft = self.x, self.y
self.health = 100
self.damage_done = 0
self.chspeed_x = 10
self.chspeed_y = 10
self.dir = 0
def rot_aim(self, tx, ty):
self.dir = (math.atan2(self.y - ty, self.x - tx) * 180 / PI)
self.image, self.rect = rot_center(self.image_orig, self.rect_orig, self.dir)
def move(self, dx, dy):
self.chspeed_x = dx
self.chspeed_y = dy
self.x = self.x + self.chspeed_x * math.cos(math.radians(270 - self.dir))
self.y = self.y + self.chspeed_y * math.cos(math.radians(270 - self.dir))
def main():
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
FPS = 30
paused = False
player = Player()
player_s = pygame.sprite.Group()
player_s.add(player)
while not paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEMOTION:
mousepos = pygame.mouse.get_pos()
mouseX = mousepos[0]
mouseY = mousepos[1]
player.rot_aim(mousepos[1], mousepos[0])
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_UP:
player.move(0, 10)
if event.type == pygame.K_DOWN:
player.move(0, -10)
if event.type == pygame.K_RIGHT:
player.move(10, 0)
if event.type == pygame.K_LEFT:
player.move(-10, 0)
player_s.draw(screen)
clock.tick(FPS)
pygame.display.flip()
您在旋轉或移動播放器后錯過了更新self.rect
。 實際上播放器( self.x
, self.y
)的 position 已更改。 但是由於self.rect
是用來繪制玩家的,所以這個屬性必須由 position 更新。 position 必須是round
,因為pygame.Rect
object 存儲整數值:
class Player(pygame.sprite.Sprite):
# [...]
def rot_aim(self, tx, ty):
self.dir = (math.atan2(self.y - ty, self.x - tx) * 180 / PI)
self.image, self.rect = rot_center(self.image_orig, self.rect_orig, self.dir)
self.rect.center = round(self.x), round(self.y) # <--- this is missing
def move(self, dx, dy):
self.chspeed_x = dx
self.chspeed_y = dy
self.x = self.x + self.chspeed_x * math.cos(math.radians(270 - self.dir))
self.y = self.y + self.chspeed_y * math.cos(math.radians(270 - self.dir))
self.rect.center= round(self.x), round(self.y) # <--- this is missing
此外還有一個錯字。 您必須將event.key
與按鈕進行比較,而不是event.type
例如:
if event.type == pygame.K_UP:
if event.key == pygame.K_UP:
# [...]
無論如何,我建議使用pygame.key.get_pressed()
而不是按鈕事件,以實現連續平穩的移動。
最后在繪制場景之前通過screen.fill(0)
清除背景:
def main():
# [...]
while not paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEMOTION:
mousepos = pygame.mouse.get_pos()
mouseX = mousepos[0]
mouseY = mousepos[1]
player.rot_aim(mousepos[1], mousepos[0])
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player.move(0, -10)
if keys[pygame.K_DOWN]:
player.move(0, 10)
if keys[pygame.K_RIGHT]:
player.move(10, 0)
if keys[pygame.K_LEFT]:
player.move(-10, 0)
screen.fill(0)
player_s.draw(screen)
clock.tick(FPS)
pygame.display.flip()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.