1

I try to create multiple meteorites in this game but i don't know how, and i also try to rotate them. I manage to create only one successful. But when I try to rotate him, the meteorite is going down much faster and is going one side, I don't know why. Can someone help me?

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()

Is possible to create multiple instance of a class while the code is running? Just an idea.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
SomnionY
  • 11
  • 2
  • Yes, one important fact about python's lists are that they can store objects inside of them. Therefore, you just have a `for` loop loop through each meteorite in a list, then perform necessary operations on each one. And more of a side note, look into `pygame.sprite.Sprite`. It is the standard module for making characters in pygame, and it also has many functions (such as `pygame.sprite.Group`) that can automatically loop though a list of sprites. And it's completely compatible with all of pygame (I don't know why, it's not like it's part of pygame, or anything preposterous like that :) – User 12692182 Mar 06 '21 at 02:43
  • 1
    Your changes to the question has completely invalidated the only answer, making it useless for future readers. Make sure to not change your question after receiving an answer and also to not ask multiple questions at once. – Ted Klein Bergman Mar 07 '21 at 22:20
  • Changing the question makes my answer useless. If you have a new question (based on this question), please [Ask a public question](https://stackoverflow.com/questions/ask). This is to your advantage, then several contributors can answer your new question. – Rabbid76 Mar 07 '21 at 22:21

1 Answers1

0

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

You have to keep the original image (orig_image). Add an angle attribute. Increment the angle and create a the rotated Surface from the original Surface:

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

Use a pygame.Rect object to rotate the meteorite around its center. Get the bounding rectangle of the rotated Surface. Set the center of the rectangle with coordinates (self.x, self.y) and use the rectangle to blit the rotated meteorite image:

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

Meteorite class:

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)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Ty @Rabbid76 you really help me with the rotation. You have any idea how to add multiple meteorites on the screen? – SomnionY Mar 07 '21 at 22:00