问题描述
我正在做一个游戏,以前使用图像制作它并使其变色,但是现在我在尝试使用对象和精灵来对其进行重新制作.我不确定如何使图像和rect旋转,也不确定如何创建在物体后方或周围看不见的rect,因为我打算让两辆汽车发生碰撞.我以前使用sin和cos来计算方向,并且当我单击左右箭头时图像会旋转,但是我不知道如何将其应用于对象类.任何帮助将不胜感激.
这是我的代码
导入pygame,随机#让我们导入汽车课从进口车pygame.init()速度= 1屏幕宽度= 800屏幕高度= 600大小=(SCREENWIDTH,SCREENHEIGHT)屏幕= pygame.display.set_mode(size)pygame.display.set_caption(赛车")#这将是一个列表,其中包含我们打算在游戏中使用的所有精灵.all_sprites_list = pygame.sprite.Group()playerCar =汽车(60、80、70)playerCar.rect.x = 160playerCar.rect.y = 100#将汽车添加到对象列表中all_sprites_list.add(playerCar)#允许用户关闭窗口...进位=真时钟= pygame.time.Clock()而随身携带:对于pygame.event.get()中的事件:如果event.type == pygame.QUIT:结转=假elif event.type == pygame.KEYDOWN:如果event.key == pygame.K_x:playerCar.moveRight(10)键= pygame.key.get_pressed()如果键[pygame.K_LEFT]:playerCar.moveLeft(5)如果键[pygame.K_RIGHT]:playerCar.moveRight(5)如果键[pygame.K_UP]:playerCar.moveUp(5)如果键[pygame.K_DOWN]:playerCar.moveDown(5)all_sprites_list.update()#在屏幕上绘图screen.fill(白色")#现在让我们一口气绘制所有精灵.(目前,我们只有1个精灵!)all_sprites_list.draw(屏幕)#刷新屏幕pygame.display.flip()#每秒钟的帧数60clock.tick(60)pygame.quit()
###第二个文件###
import pygame白=(255,255,255)Car(pygame.sprite.Sprite)类:#此类代表一辆汽车.它从精灵"派生而来.在Pygame中上课.def __init __(自身,宽度,高度,速度):#调用父类(Sprite)构造函数super().__ init __()#相反,我们可以加载正确的汽车图片...self.image = pygame.image.load("car.png").convert_alpha()#Initialise汽车的属性.self.width =宽度self.height =高度#self.speed =速度#绘制汽车(矩形!)pygame.draw.rect(self.image,(0,0,0,0),[0,0,self.width,self.height])#获取具有图像尺寸的矩形对象.self.rect = self.image.get_rect()def moveRight(自我,像素):self.rect.x + =像素def moveLeft(自我,像素):self.rect.x-=像素def moveUp(self,pixel):self.rect.y-=像素def moveDown(self,pixel):self.rect.y + =像素def changeSpeed(自我,速度):self.speed =速度
如果不绘制对象,则不可见.不要混淆
使用
如果要旋转矩形,则必须填充
I am making a game, and previously had made it using images and bliting them but I am now remaking it trying to uses object and sprites. I am unsure how I would make my image and rect rotate and I am also unsure how to create a rect that is behind or none visible around my object as I plan to have two cars that can collide.I previously used sin and cos to calculate the direction and the image would turn when I clicked the left and right arrow but I don't know how to apply this to an object class.Any help would be appreciated thanks.
This is my code
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()
###second file###
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
If you don't draw an object it is not visible. Don't confuse a pygame.Rect
objects and pygame.draw.rect
. However, a pygame.Rect
stores a position and a size. It is always axis aligned and cannot represent a rotated rectangle.
I'm suspect you need the rectangular outline of the objects for collision detection. See How do I detect collision in pygame?, Collision between masks in PyGame and Pygame mask collision.
Use pygame.transform.rotate
to rotate an image. e.g:
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)
See also How do I rotate an image around its center using PyGame?
If you want to rotate a rectangle you have to fill a pygame.Surface
object with transparency information with a uniform color and rotate the surface. Set the SRCALPHA
to crate a Surface with per pixel alpha format:
rect_surf = pygame.Surface((width, height), pygame.SRCALPHA)
rect_surf.fill(color)
blitRotateCenter(screen, rect_surf, (x, y), angle)
See also Getting rotated rect of rotated image in Pygame
这篇关于如何在pygame中旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!