python - 我如何使pygame中的对象旋转

标签 python pygame sprite game-physics pygame-surface

我正在制作一款游戏,之前使用图像和 bliting 制作游戏,但我现在正在重新制作它,尝试使用对象和 Sprite 。我不确定如何使我的图像和矩形旋转,我也不确定如何创建一个在我的物体后面或在我的物体周围不可见的矩形,因为我打算让两辆车发生碰撞。 我以前使用 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, 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

最佳答案

I am also unsure how to create a rect that is behind or none visible around my object.

如果你不绘制一个对象,它是不可见的。不要混淆 pygame.Rect对象和 pygame.draw.rect .但是,pygame.Rect 存储位置和大小。它始终是轴对齐的,不能表示旋转的矩形。

我怀疑您需要对象的矩形轮廓来进行碰撞检测。参见 How do I detect collision in pygame? , Collision between masks in PyGamePygame 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)

另见 Getting rotated rect of rotated image in Pygame

关于python - 我如何使pygame中的对象旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67353690/

相关文章:

python - 当所有类名称相似时,如何在selenium python中提取数据?

python - Pandas :获取某些行的平均值并作为数据框返回

python - 将 Pygame 模拟保存为 mp4 文件

python - 导入后不同的类行为

python - Manim 展示 latex table

python - 如何将自定义参数添加到 Python 日志格式化程序中?

java - LibGDX 1.5,如何拉伸(stretch)以适应宽度

flash - 为 Sprite 设置 mask

python - 如何在pygame中上下移动一个盒子[矩形为正方形]?

使用 Sprite 的 jQuery - 放置多个效果,可能是语法问题