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.