如何解决我如何使pygame中的对象旋转
我正在制作一个游戏,之前使用图像和闪烁它们制作了它,但我现在正在尝试使用对象和精灵来重新制作它。我不确定如何让我的图像和矩形旋转,我也不确定如何创建一个在我的对象后面或不可见的矩形,因为我计划让两辆车相撞。 我以前使用 sin 和 cos 来计算方向,当我单击左右箭头时,图像会转动,但我不知道如何将其应用于对象类。 任何帮助将不胜感激谢谢。
这是我的代码
import pygame,random
#Let's import the Car Class
from Car import Car
pygame.init()
speed = 1
SCREENWIDTH=800
SCREENHEIGHT=600
size = (SCREENWIDTH,SCREENHEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Car Racing")
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()
playerCar = Car(60,80,70)
playerCar.rect.x = 160
playerCar.rect.y = 100
# Add the car to the list of objects
all_sprites_list.add(playerCar)
#Allowing the user to close the window...
carryOn = True
clock=pygame.time.Clock()
while carryOn:
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_x:
playerCar.moveRight(10)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
playerCar.moveLeft(5)
if keys[pygame.K_RIGHT]:
playerCar.moveRight(5)
if keys[pygame.K_UP]:
playerCar.moveUp(5)
if keys[pygame.K_DOWN]:
playerCar.moveDown(5)
all_sprites_list.update()
#Drawing on Screen
screen.fill("white")
#Now let's draw all the sprites in one go. (For Now we only have 1 sprite!)
all_sprites_list.draw(screen)
#Refresh Screen
pygame.display.flip()
#Number of frames per secong e.g. 60
clock.tick(60)
pygame.quit()
###第二个文件###
import pygame
WHITE = (255,255,255)
class Car(pygame.sprite.Sprite):
#This class represents a car. It derives from the "Sprite" class in Pygame.
def __init__(self,width,height,speed):
# Call the parent class (Sprite) constructor
super().__init__()
# Instead we Could load a proper picture of a car...
self.image = pygame.image.load("car.png").convert_alpha()
#Initialise attributes of the car.
self.width=width
self.height=height
#self.speed = speed
# Draw the car (a rectangle!)
pygame.draw.rect(self.image,(0,0),[0,self.width,self.height])
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
def moveRight(self,pixels):
self.rect.x += pixels
def moveLeft(self,pixels):
self.rect.x -= pixels
def moveUp(self,pixels):
self.rect.y -= pixels
def moveDown(self,pixels):
self.rect.y += pixels
def changeSpeed(self,speed):
self.speed = speed
解决方法
我也不确定如何创建一个在我的对象后面或不可见的矩形。
如果您不绘制对象,则它是不可见的。不要混淆 pygame.Rect
对象和 pygame.draw.rect
。但是,pygame.Rect
存储位置和大小。它始终是轴对齐的,不能表示旋转的矩形。
我怀疑您需要对象的矩形轮廓来进行碰撞检测。请参阅 How do I detect collision in pygame?、Collision between masks in PyGame 和 Pygame mask collision。
使用 pygame.transform.rotate
旋转图像。例如:
def blitRotateCenter(surf,image,center,angle):
rotated_image = pygame.transform.rotate(image,angle)
new_rect = rotated_image.get_rect(center = image.get_rect(center = center).center)
surf.blit(rotated_image,new_rect)
另见How do I rotate an image around its center using PyGame?
如果您想旋转一个矩形,您必须使用具有统一颜色的透明度信息填充 pygame.Surface
对象并旋转表面。设置 SRCALPHA
以创建具有每像素 Alpha 格式的 Surface:
rect_surf = pygame.Surface((width,height),pygame.SRCALPHA)
rect_surf.fill(color)
blitRotateCenter(screen,rect_surf,(x,y),angle)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。