[英]Python Pygame Shape Rotation Issue
我對Pygame相當陌生,我正在嘗試制作一個簡單的小游戲,我目前在旋轉矩形時遇到問題。 它自身的形狀會旋轉,但是由於某種原因,當形狀旋轉時(例如說90度),它會從地板上浮出幾個像素,這是一個圖像,說明發生了什么:
#Player Class
class Player(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the player
controls. """
# List of sprites we can bump against
level = None
# -- Methods
def __init__(self):
""" Constructor function """
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.width = 40
self.height = 60
self.image = pygame.Surface([self.width, self.height])
self.image.fill(RED)
self.baseImage = self.image
# Set a referance to the image rect.
self.rect = self.image.get_rect()
def update(self):
""" Move the player. """
# Move left/right
self.rect.x += self.change_x
# See if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for block in block_hit_list:
# If we are moving right,
# set our right side to the left side of the item we hit
if self.change_x > 0:
self.rect.right = block.rect.left
elif self.change_x < 0:
# Otherwise if we are moving left, do the opposite.
self.rect.left = block.rect.right
# Player-controlled movement:
def go_left(self):
""" Called when the user hits the left arrow. """
self.change_x = -10
def go_right(self):
""" Called when the user hits the right arrow. """
self.change_x = 10
def stop(self):
""" Called when the user lets off the keyboard. """
self.change_x = 0
def turn_r(self):
self.image = pygame.transform.rotate(self.image, angle)
self.change_x = 0
def turn_l(self):
count = -90
self.image = pygame.transform.rotate(self.image, angle)
self.change_x = -0
#--------主程序循環-----------
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.go_left()
if event.key == pygame.K_RIGHT:
player.go_right()
if event.key == pygame.K_UP:
player.jump()
if event.key == pygame.K_d:
player.turn_r()
count += 90
if event.key == pygame.K_a:
player.turn_l()
count -= 90
您的問題似乎在形狀的頂部和左側,您可能需要重新對齊中心,例如:
def turn_r(self):
center_x = self.rect.x + (self.rect.width / 2)
center_y = self.rect.y + (self.rect.height / 2)
self.image = pygame.transform.rotate(self.image, angle)
new_center_x = self.rect.x + (self.rect.width / 2)
new_center_y = self.rect.y + (self.rect.height / 2)
self.change_x = center_x - new_center_x
self.change_y = center_y - new_center_y
或者,您可以將形狀的新Y設置為返回到底部:
def turn_r(self):
bottom = self.rect.y + self.rect.height
self.image = pygame.transform.rotate(self.image, angle)
new_bottom = self.rect.y + self.rect.height
self.change_y = bottom - new_bottom
我對PyGame不太了解,代碼可能會有一些問題,但這是這樣的。
在將其旋轉90°之前,寬度為40,高度為60。將其交換后,這兩個值被交換,因此現在高度僅為40,而您的代碼仍將其以參考原始60像素的高度旋轉。
我想說的最簡單的方法是使程序旋轉圖像並重置為另一個高度級別,該高度級別比以前降低了20個像素。
只是為了幫助別人,我才弄清楚代碼出了什么問題。 問題是,形狀旋轉后,矩形保持不變,我通過在turn_r和turn_l函數中簡單地更新了矩形來解決了這個問題。 隨時尋求幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.