【问题标题】:Everytime I Resize My Car A Part Goes Missing Pygame How Do I Fix This?每次我调整我的汽车的大小时,Pygame 的零件都会丢失,我该如何解决这个问题?
【发布时间】:2020-07-17 17:11:48
【问题描述】:

最近我在制作基本的汽车物理方面得到了帮助,但我遇到的一个问题是我的车太小了,每次我调整汽车的大小时,汽车的某些部分都会丢失video我不知道如何解决这个问题这辆车太小了,我正在做一个停车游戏,我需要把车开大一点

我的车课

pygame.display.set_caption("car game")
img = pygame.image.load("s23.png")
img = pygame.transform.scale(img,(img.get_width()+ 36,img.get_height()+ 36))

class Car:
    def __init__(self, x, y, height, width, color):
        self.x = x - width / 2
        self.y = y - height / 2
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x, y, height, width)
        self.surface = pygame.Surface((height, width)) # 1
        self.surface.blit(img, (0, 0))
        self.angle = 0
        self.speed = 0 # 2

    def draw(self): # 3
        self.rect.topleft = (int(self.x), int(self.y))
        rotated = pygame.transform.rotate(self.surface, self.angle)
        surface_rect = self.surface.get_rect(topleft = self.rect.topleft)
        new_rect = rotated.get_rect(center = surface_rect.center)
        window.blit(rotated, new_rect.topleft)

white = (255, 255, 255)
car1 = Car(300, 300, 73, 73, white) # 4
clock = pygame.time.Clock()

我的主循环

runninggame = True
while runninggame:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False
    
    pressed = pygame.key.get_pressed()
    car1.speed *= 0.9 # 5
    if pressed[pygame.K_UP]: car1.speed += 0.5 # 6
    if pressed[pygame.K_DOWN]: car1.speed -= 0.5 # 6

    if pressed[pygame.K_LEFT]: car1.angle += car1.speed / 2 # 7
    if pressed[pygame.K_RIGHT]: car1.angle -= car1.speed / 2 # 7
    car1.x -= car1.speed * math.sin(math.radians(car1.angle)) # 8
    car1.y -= car1.speed * math.cos(math.radians(-car1.angle)) # 8
    
    window.fill((0, 0, 0)) # 9
    car1.draw()
    pygame.display.flip()
    clock.tick(60) # 10
pygame.quit()

【问题讨论】:

    标签: python html css python-3.x pygame


    【解决方案1】:

    您在加载图像时对其进行缩放。稍后,当您实例化 Car 时,您传入汽车的 heightwidth 并创建具有该尺寸的 Surface。但是,您不会缩放图像以适应它,您只需将初始图像blit 到该表面(这是请求的大小)。将您的图像 Blit' 到一个较小的表面会导致它被裁剪为它正在被 Blit'ed 到的表面的大小。

    要解决此问题,请不要在首次加载图像时对其进行缩放。只需在 __init__()Car 创建 self.surface 时对其进行缩放。

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2020-03-25
      • 2020-02-14
      相关资源
      最近更新 更多