mercredi 26 avril 2017

Pygame sprite group using random.rand

I am working with a sprite group which contains 60 sprite objects. each object represents a asteroid image and all images displayed after each other in the right order creates an animation of a rotating asteroid, in other words i am iterating through the sprite group as seen in code below.

Evcerything works fine until the objects passes the screen height and resets at the top again and the objects then starts appearing at random locations in the x direction. The screen now seem to blit each sprite at random locations and the asteroid now just flickers back and forth horizontally.

Is there a way to get the same new random x location for all the sprites after asteroid obj passes the screen instead of giving each sprite in the group a new x value?

Any suggestions?

import pygame
import sys
import os
import random
import time
import math

black = (0, 0, 0)
white = (255, 255, 255)
red = (200, 40, 60)
green = (50, 180, 50)
blue = (50, 30, 200)
skyblue = (135, 206, 250)
silver = (192, 192, 192)
darkgray = (47, 79, 79)
vegasgold = (197, 179, 88)
nightblue = (25, 25, 112)
steelblue = (70, 130, 180)
deepblue = (0, 26, 51)

screen_width = 1920
screen_height = 1080
half_width = screen_width/2
half_height = screen_height/2

screen = pygame.display.set_mode([screen_width, screen_height])
Title = pygame.display.set_caption('Space Mash')
clock = pygame.time.Clock()

class Spaceship(pygame.sprite.Sprite):

    def __init__(self, width=30, height=30, color=white):
        super(Spaceship, self).__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = 0
        self.rect.y = 0

    def center_set_position(self, x, y):
        self.rect.x = x - self.rect.center[0]
        self.rect.y = y - self.rect.width

    def rotate_ship(self, dg):
        self.image = pygame.transform.rotate(self.image, dg)

    def set_image(self, filename):
        self.image = pygame.image.load(filename)
        self.rect = self.image.get_rect()

    def draw_ship(self):
        screen.blit(self.image, [self.rect.x, self.rect.y])


player_spaceship = Spaceship()
player_spaceship.set_image("main_ship.png")
player_spaceship.center_set_position(half_width, screen_height)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player_spaceship)


class Asteroids(pygame.sprite.Sprite):
    def __init__(self, nr):
        super(Asteroids, self).__init__()

        self.image = pygame.image.load('Asteroid_{0}.png'.format(nr))
        self.rect = self.image.get_rect()
        self.rect.x = 0
        self.rect.y = 0

    def update(self):
        self.rect.y += 5

        if self.rect.y > screen_height + 50:
            self.rect.y = -50
            self.rect.x = random.randrange(0, (screen_width - (self.rect.width))


asteroids_list = pygame.sprite.LayeredUpdates() 

for i in range(1, 61):
    asteroid = Asteroids(nr=i)

    asteroids_list.add(asteroid)
    all_sprites_list.add(asteroid)



class Stars(pygame.sprite.Sprite):
    def __init__(self):
        super(Stars, self).__init__()

        self.image = pygame.Surface([1, 1])
        self.image.fill(white)
        self.rect = self.image.get_rect()

stars_group = pygame.sprite.Group()


def making_star_objects():

    for i in range(100):
        x_loc = random.randint(0, screen_width - 1)
        y_loc = random.randint(0, screen_height - 1)
        star = Stars()
        star.rect.x = x_loc
        star.rect.y = y_loc

        stars_group.add(star)


def gameloop():
    ending = False
    x_change = 0
    y_change = 0
    making_star_objects()
    frame = 0

    while not ending:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    x_change = 10
                elif event.key == pygame.K_LEFT:
                    x_change = -10
                elif event.key == pygame.K_UP:
                    y_change = -8
                elif event.key == pygame.K_DOWN:
                    y_change = 8
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    x_change = 0
                elif event.key == pygame.K_LEFT:
                    x_change = 0


        player_spaceship.rect.x += x_change
        player_spaceship.rect.y += y_change
        if frame > 59:
            frame = 0

        asteroids_list.update()


        asteroids_hit_list = pygame.sprite.spritecollide(player_spaceship, asteroids_list, False)

        screen.fill(deepblue)
        stars_group.draw(screen)
        screen.blit(asteroids_list.get_sprite(frame).image, (asteroids_list.get_sprite(frame).rect.x,
                                                        asteroids_list.get_sprite(frame).rect.y))



        frame += 1

        player_spaceship.draw_ship()
        pygame.display.update()
        clock.tick(30)

    pygame.quit()
    quit()

gameloop()




Aucun commentaire:

Enregistrer un commentaire