Pygame 在曲面内旋转矩形
Pygame Rotate Rectangles within Surface
在我旋转该表面后,我无法跟踪我 blit 到该表面上的两个矩形。
表面上绘制了三个矩形,代表玩家。当按下左右键时,矩形都正确地围绕中心点旋转。
当按下 space 栏时,"lights" 应该会打开和关闭,但它们总是在表面底部重新绘制。
你能告诉我做错了什么吗?
import pygame
from pygame.locals import *
class Player(pygame.sprite.Sprite):
height = 48
width = 48
colour = (30,144,255)
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((self.width, self.height))
self.surf.fill((0, 0, 0))
self.rect = self.surf.get_rect(center = (screen_width / 2, screen_height / 2))
self.rotation = 0
self.PlayerBody = pygame.draw.rect(self.surf, self.colour, Rect(15, 15, 20, 35))
self.PlayerLightLeft = pygame.draw.rect(self.surf, (125, 0, 0), Rect(19, 45, 4, 4))
self.PlayerLightRight = pygame.draw.rect(self.surf, (125, 0, 0), Rect(27, 45, 4, 4))
self.lights = False
self.lightsOnColour = (255, 0, 0)
self.lightsOffColour = (125, 0, 0)
self.lightsColour = self.lightsOffColour
def update(self, pressedKey):
if pressedKey == pygame.K_RIGHT:
self.surf = pygame.transform.rotate(self.surf, -90)
if pressedKey == pygame.K_LEFT:
self.surf = pygame.transform.rotate(self.surf, 90)
if pressedKey == pygame.K_SPACE:
if self.lights:
self.lightsColour = self.lightsOffColour
self.lights = False
else:
self.lightsColour = self.lightsOnColour
self.lights = True
# always draws rectangles at the bottom of the surface
self.PlayerLightLeft = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightLeft)
self.PlayerLightRight = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightRight)
# initialize pygame
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
Player = Player()
running = True
while running:
pressedKey = pygame.K_0
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pressedKey = event.key
elif event.type == QUIT:
running = False
screen.blit(background, (0, 0))
Player.update(pressedKey)
screen.blit(Player.surf, Player.rect)
pygame.display.flip()
# end of game, quit
pygame.quit()
按下按键时不旋转播放器表面,而是在 class Player
中添加一个属性 angle
,它存储表面的当前角度。
当按下 K_RIGHT
或 K_LEFT
键时更改 angle
。
class Player(pygame.sprite.Sprite):
height = 48
width = 48
colour = (30,144,255)
def __init__(self):
super(Player, self).__init__()
# [...]
self.angle = 0
def update(self, pressedKey):
if pressedKey == pygame.K_RIGHT:
self.angle = (self.angle - 90) % 360
if pressedKey == pygame.K_LEFT:
self.angle = (self.angle + 90) % 360
# [...]
这导致原始表面永远不会改变,改变 "lights" 将始终有效。
创建一个旋转表面,它由 Player.angle
和 blit
旋转表面旋转:
rotSurf = pygame.transform.rotate(Player.surf, Player.angle)
screen.blit(rotSurf, Player.rect)
太棒了@Rabbid76!谢谢!
如果有人遇到类似问题,请更新下面的代码。
import pygame
from pygame.locals import *
class Player(pygame.sprite.Sprite):
height = 48
width = 48
colour = (30,144,255)
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((self.width, self.height))
self.surf.fill((0, 0, 0))
self.rect = self.surf.get_rect(center = (screen_width / 2, screen_height / 2))
self.angle = 0
self.PlayerBody = pygame.draw.rect(self.surf, self.colour, Rect(15, 15, 20, 35))
self.PlayerLightLeft = pygame.draw.rect(self.surf, (125, 0, 0), Rect(19, 45, 4, 4))
self.PlayerLightRight = pygame.draw.rect(self.surf, (125, 0, 0), Rect(27, 45, 4, 4))
self.lights = False
self.lightsOnColour = (255, 0, 0)
self.lightsOffColour = (125, 0, 0)
self.lightsColour = self.lightsOffColour
def update(self, pressedKey):
if pressedKey == pygame.K_RIGHT:
self.angle = (self.angle - 90) % 360
if pressedKey == pygame.K_LEFT:
self.angle = (self.angle + 90) % 360
if pressedKey == pygame.K_SPACE:
if self.lights:
self.lightsColour = self.lightsOffColour
self.lights = False
else:
self.lightsColour = self.lightsOnColour
self.lights = True
self.PlayerLightLeft = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightLeft)
self.PlayerLightRight = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightRight)
# initialize pygame
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
Player = Player()
running = True
while running:
pressedKey = pygame.K_0
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pressedKey = event.key
elif event.type == QUIT:
running = False
screen.blit(background, (0, 0))
Player.update(pressedKey)
rotSurf = pygame.transform.rotate(Player.surf, Player.angle)
screen.blit(rotSurf, Player.rect)
pygame.display.flip()
# end of game, quit
pygame.quit()
在我旋转该表面后,我无法跟踪我 blit 到该表面上的两个矩形。 表面上绘制了三个矩形,代表玩家。当按下左右键时,矩形都正确地围绕中心点旋转。 当按下 space 栏时,"lights" 应该会打开和关闭,但它们总是在表面底部重新绘制。 你能告诉我做错了什么吗?
import pygame
from pygame.locals import *
class Player(pygame.sprite.Sprite):
height = 48
width = 48
colour = (30,144,255)
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((self.width, self.height))
self.surf.fill((0, 0, 0))
self.rect = self.surf.get_rect(center = (screen_width / 2, screen_height / 2))
self.rotation = 0
self.PlayerBody = pygame.draw.rect(self.surf, self.colour, Rect(15, 15, 20, 35))
self.PlayerLightLeft = pygame.draw.rect(self.surf, (125, 0, 0), Rect(19, 45, 4, 4))
self.PlayerLightRight = pygame.draw.rect(self.surf, (125, 0, 0), Rect(27, 45, 4, 4))
self.lights = False
self.lightsOnColour = (255, 0, 0)
self.lightsOffColour = (125, 0, 0)
self.lightsColour = self.lightsOffColour
def update(self, pressedKey):
if pressedKey == pygame.K_RIGHT:
self.surf = pygame.transform.rotate(self.surf, -90)
if pressedKey == pygame.K_LEFT:
self.surf = pygame.transform.rotate(self.surf, 90)
if pressedKey == pygame.K_SPACE:
if self.lights:
self.lightsColour = self.lightsOffColour
self.lights = False
else:
self.lightsColour = self.lightsOnColour
self.lights = True
# always draws rectangles at the bottom of the surface
self.PlayerLightLeft = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightLeft)
self.PlayerLightRight = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightRight)
# initialize pygame
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
Player = Player()
running = True
while running:
pressedKey = pygame.K_0
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pressedKey = event.key
elif event.type == QUIT:
running = False
screen.blit(background, (0, 0))
Player.update(pressedKey)
screen.blit(Player.surf, Player.rect)
pygame.display.flip()
# end of game, quit
pygame.quit()
按下按键时不旋转播放器表面,而是在 class Player
中添加一个属性 angle
,它存储表面的当前角度。
当按下 K_RIGHT
或 K_LEFT
键时更改 angle
。
class Player(pygame.sprite.Sprite):
height = 48
width = 48
colour = (30,144,255)
def __init__(self):
super(Player, self).__init__()
# [...]
self.angle = 0
def update(self, pressedKey):
if pressedKey == pygame.K_RIGHT:
self.angle = (self.angle - 90) % 360
if pressedKey == pygame.K_LEFT:
self.angle = (self.angle + 90) % 360
# [...]
这导致原始表面永远不会改变,改变 "lights" 将始终有效。
创建一个旋转表面,它由 Player.angle
和 blit
旋转表面旋转:
rotSurf = pygame.transform.rotate(Player.surf, Player.angle)
screen.blit(rotSurf, Player.rect)
太棒了@Rabbid76!谢谢!
如果有人遇到类似问题,请更新下面的代码。
import pygame
from pygame.locals import *
class Player(pygame.sprite.Sprite):
height = 48
width = 48
colour = (30,144,255)
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((self.width, self.height))
self.surf.fill((0, 0, 0))
self.rect = self.surf.get_rect(center = (screen_width / 2, screen_height / 2))
self.angle = 0
self.PlayerBody = pygame.draw.rect(self.surf, self.colour, Rect(15, 15, 20, 35))
self.PlayerLightLeft = pygame.draw.rect(self.surf, (125, 0, 0), Rect(19, 45, 4, 4))
self.PlayerLightRight = pygame.draw.rect(self.surf, (125, 0, 0), Rect(27, 45, 4, 4))
self.lights = False
self.lightsOnColour = (255, 0, 0)
self.lightsOffColour = (125, 0, 0)
self.lightsColour = self.lightsOffColour
def update(self, pressedKey):
if pressedKey == pygame.K_RIGHT:
self.angle = (self.angle - 90) % 360
if pressedKey == pygame.K_LEFT:
self.angle = (self.angle + 90) % 360
if pressedKey == pygame.K_SPACE:
if self.lights:
self.lightsColour = self.lightsOffColour
self.lights = False
else:
self.lightsColour = self.lightsOnColour
self.lights = True
self.PlayerLightLeft = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightLeft)
self.PlayerLightRight = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightRight)
# initialize pygame
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
Player = Player()
running = True
while running:
pressedKey = pygame.K_0
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pressedKey = event.key
elif event.type == QUIT:
running = False
screen.blit(background, (0, 0))
Player.update(pressedKey)
rotSurf = pygame.transform.rotate(Player.surf, Player.angle)
screen.blit(rotSurf, Player.rect)
pygame.display.flip()
# end of game, quit
pygame.quit()