1 year ago
#378306
Jessica Leung
Redrawing Pygame sprites after they disappear from the screen
I'm writing a very simple program to practice using Pygame and Sprites. I found an image of a raindrop and created a grid of raindrops. I also made the raindrops fall toward the bottom of screen until they disappear. My goal is: when a row of raindrops disappears off the bottom of screen, a new row appears at the top of the screen and begins to fall. I'm stuck on this part, here is my code below, can anyone give me some ideas/sample code about how to implement this? Let me know if you have any questions, thank you so much!!
raindrop_grid.py this is the only file that should run in python, I defined a main function "run_game()"
import pygame
import sys
from settings import Settings
import game_functions as gf
from pygame.sprite import Group
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption(ai_settings.caption)
raindrops = Group()
new_raindrops = Group()
gf.create_raindrops(ai_settings, screen, raindrops)
while True:
gf.check_events()
gf.update_raindrops(raindrops)
gf.update_screen(ai_settings, screen, raindrops)
This is the "raindrop.py" file where I created a class called "Raindrop" based on "Sprite":
import pygame
from pygame.sprite import Sprite
class Raindrop(Sprite):
def __init__(self, ai_settings, screen):
super().__init__()
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load("./images/raindrop.bmp")
self.image = pygame.transform.scale(self.image, (self.ai_settings.raindrop_width, self.ai_settings.raindrop_height))
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# set the raindrop's initial position.
self.rect.x = self.rect.width
self.rect.y = self.rect.height
self.y = float(self.rect.y)
self.x = float(self.rect.x)
def blitme(self):
self.screen.blit(self.image, self.rect)
def update(self):
self.y += self.ai_settings.raindrop_speed_factor
self.rect.y = self.y
def check_edges(self):
""" return true if the raindrop hits the bottom of screen."""
if self.rect.bottom >= self.screen_rect.bottom:
return True
This is the main game functions file "game_functions.py":
import sys
import pygame
from raindrop import Raindrop
def check_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
sys.exit()
def update_screen(ai_settings, screen, raindrops):
screen.fill(ai_settings.bg_color)
raindrops.draw(screen)
pygame.display.flip()
def get_number_raindrop_x(ai_settings, raindrop_width):
available_space_x = ai_settings.screen_width - 2 * raindrop_width
number_raindrop_x = int(available_space_x / (2 * raindrop_width))
return number_raindrop_x
def get_row_number(ai_settings, raindrop_height):
available_space_y = ai_settings.screen_height - 2 * raindrop_height
row_number = int(available_space_y / (2 * raindrop_height))
return row_number
def raindrop_position(ai_settings, screen, raindrops, raindrop_number, row_number):
raindrop = Raindrop(ai_settings, screen)
raindrop.x = raindrop.rect.width + 2 * raindrop_number * raindrop.rect.width
raindrop.rect.x = raindrop.x
raindrop.y = raindrop.rect.height + 2 * row_number * raindrop.rect.height
raindrop.rect.y = raindrop.y
raindrops.add(raindrop)
def create_raindrops(ai_settings, screen, raindrops):
raindrop = Raindrop(ai_settings, screen)
number_raindrop_x = get_number_raindrop_x(ai_settings, raindrop.rect.width)
number_rows = get_row_number(ai_settings, raindrop.rect.height)
for row_number in range(number_rows):
for raindrop_number in range(number_raindrop_x):
raindrop_position(ai_settings, screen, raindrops, raindrop_number, row_number)
def update_raindrops(raindrops):
""" update the positions of all raindrops."""
raindrops.update()
And this is the settings file "settings.py" where I created a class "Settings" to store the game settings:
import pygame
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (250, 250, 250)
self.caption = "Raindrop Grid"
# raindrop settings.
self.raindrop_width = 80
self.raindrop_height = 80
self.raindrop_speed_factor = 1
self.raindrop_drop_speed = 1
python
pygame
sprite
0 Answers
Your Answer