【问题标题】:Pygame create multiple meteorites and rotate them [duplicate]Pygame创建多个陨石并旋转它们[重复]
【发布时间】:2021-06-04 16:21:55
【问题描述】:

我尝试在这个游戏中创建多个陨石,但我不知道如何,我也尝试旋转它们。 我设法只创造了一个成功的。但是当我试图让他旋转时,陨石下降得更快,并且向一侧移动,我不知道为什么。 有人可以帮我吗?

import pygame as py
import os
import random

py.font.init()
py.mixer.init()

WIN_WIDTH = (600)
WIN_HEIGHT = (690)

SPACESHIP_WIDTH = (55)
SPACECHIP_HEIGHT = (40)

WIN = py.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
py.display.set_caption("Ship Survivor")

SPACESHIP_VEL = (7)
FPS = (30)

SPACE_BACKGROUND = py.image.load(os.path.join("Assets", "space.png"))
SPACE_BACKGROUND = py.transform.rotate(SPACE_BACKGROUND, 90)
SPACE_BACKGROUND = py.transform.scale(SPACE_BACKGROUND, (WIN_WIDTH, WIN_HEIGHT))

RED_SPACESHIP_IMAGE = py.image.load(os.path.join("Assets", "spaceship_red.png"))
RED_SPACESHIP_IMAGE = py.transform.scale(RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACECHIP_HEIGHT))
RED_SPACESHIP_IMAGE = py.transform.rotate(RED_SPACESHIP_IMAGE, 180)



class SpaceShip:

    def __init__(self, x, y, image):
        self.x = x
        self.y = y
        self.image = image
        self.mask = py.mask.from_surface(self.image)


    def movement(self):
        # Check for the keystrokes and store the value
        self.keys_pressed = py.key.get_pressed()
        if (self.keys_pressed[py.K_LEFT] or self.keys_pressed[py.K_a]) and self.x - SPACESHIP_VEL > 0:
            self.x -= SPACESHIP_VEL
        if (self.keys_pressed[py.K_RIGHT] or self.keys_pressed[py.K_d]) and self.x + SPACESHIP_WIDTH + SPACESHIP_VEL < WIN_WIDTH:
            self.x += SPACESHIP_VEL
        if (self.keys_pressed[py.K_UP] or self.keys_pressed[py.K_w]) and self.y - SPACESHIP_VEL > 0:
            self.y -= SPACESHIP_VEL
        if (self.keys_pressed[py.K_DOWN] or self.keys_pressed[py.K_s]) and self.y + SPACECHIP_HEIGHT + SPACESHIP_VEL < WIN_HEIGHT:
            self.y += SPACESHIP_VEL

    def draw(self, win):
        win.blit(self.image, (self.x, self.y))

# -----------------------------------------------------------------------

class Meteorite:
    meteorite_VEL = 4
    meteorite_image = py.image.load(os.path.join("Assets", "meteor1.png"))
    meteorite_image = py.transform.scale(meteorite_image, (random.randrange(50, 300), random.randrange(40, 300)))

    def __init__ (self):
        self.image = self.meteorite_image
        self.mask = py.mask.from_surface(self.image)
        self.y = 0
        self.x = 0
        self.x = random.randrange(0, WIN_WIDTH - self.image.get_width())

    def movement(self):
        self.y +=  self.meteorite_VEL
        # self.image = py.transform.rotate(self.image, 0.50)


    def draw(self, win):
        win.blit(self.image, (self.x, self.y))


# -----------------------------------------------------------------------

def draw_window(win, space_ship, meteorite):

    win.blit(SPACE_BACKGROUND, (0, 0))
    meteorite.draw(win)
    space_ship.draw(win)




    py.display.update()


# Execution--------------------------
def main():
    # Objets-----------------------------
    red_spaceship = SpaceShip(WIN_WIDTH//2 - SPACESHIP_WIDTH, 600, RED_SPACESHIP_IMAGE)
    grinMeteorite = Meteorite()


    clock = py.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in py.event.get():
            if event.type == py.QUIT:
                run = False


        red_spaceship.movement()
        grinMeteorite.movement()

        draw_window(WIN, red_spaceship, grinMeteorite)

        offset = (grinMeteorite.x - red_spaceship.x, grinMeteorite.y - red_spaceship.y)
        print(red_spaceship.mask.overlap_area(grinMeteorite.mask, offset))


    py.quit()

main()

是否可以在代码运行时创建一个类的多个实例?只是一个想法。

【问题讨论】:

  • 是的,关于 python 列表的一个重要事实是它们可以在其中存储对象。因此,您只需使用for 循环遍历列表中的每个陨石,然后对每个陨石执行必要的操作。还有更多的旁注,请查看pygame.sprite.Sprite。它是 pygame 中制作角色的标准模块,它还具有许多可以自动循环遍历 sprite 列表的功能(例如pygame.sprite.Group)。而且它与所有 pygame 完全兼容(我不知道为什么,它不像是 pygame 的一部分,或者像这样荒谬的东西:)
  • 您对问题的更改已完全使唯一答案无效,使其对未来的读者毫无用处。确保在收到答案后不要更改您的问题,也不要一次提出多个问题。
  • 更改问题会使我的答案毫无用处。如果您有新问题(基于此问题),请Ask a public question。这对您有利,然后有几个贡献者可以回答您的新问题。

标签: python pygame multiple-inheritance pygame-surface


【解决方案1】:

How do I rotate an image around its center using PyGame?

您必须保留原始图像 (orig_image)。添加angle 属性。增加角度并从原始 Surface 创建一个旋转的 Surface

self.angle += 0.5
self.image = py.transform.rotate(self.orig_image, self.angle)

使用pygame.Rect 对象围绕其中心旋转陨石。获取旋转 Surface 的边界矩形。 用坐标(self.x,self.y)设置矩形的中心,用矩形为blit旋转的陨石图片:

self.rect = self.image.get_rect(center = (self.x, self.y))
win.blit(self.image, self.rect)

陨石类:

class Meteorite:
    meteorite_VEL = 4
    meteorite_image = py.image.load(os.path.join("Assets", "meteor1.png"))
    meteorite_image = py.transform.scale(meteorite_image, (random.randrange(50, 300), random.randrange(40, 300)))

    def __init__ (self):

        self.orig_image = self.meteorite_image
        self.image = self.meteorite_image
        
        self.y = self.image.get_height() // 2
        self.x = random.randrange(0, WIN_WIDTH - self.image.get_width())

        self.angle = 0
        self.rect = self.image.get_rect(center = (self.x, self.y))
        self.mask = py.mask.from_surface(self.image)
        
    def movement(self):
        self.y +=  self.meteorite_VEL

        self.angle += 0.5
        self.image = py.transform.rotate(self.orig_image, self.angle)
        
        self.rect = self.image.get_rect(center = (self.x, self.y))
        self.mask = py.mask.from_surface(self.image)

    def draw(self, win):
        win.blit(self.image, self.rect)

【讨论】:

  • Ty @Rabbid76 你真的帮我轮换了。你知道如何在屏幕上添加多个陨石吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
  • 2013-08-19
  • 2013-10-19
  • 2022-02-03
相关资源
最近更新 更多