'How do I control increase and descrease of sprite spawning in PyGame?
I am attempting to create a game where you are a cloud that can move left and right, and when you press space you start raining at an increasing rate - and when you stop the rate of rain decreases until it hits 0 - the rain will have particles when it hits the ground.
What I have so far: a cloud that can move left to right and when you hold space for long enough it starts raining - but only at the same rate and it does increase or decrease - and when you let go of space it doesn't ever stop. I have spent hours trying to fix this to no avail and i believe its because i wrote the code pretty badly however I cannot think of an alternative that will work. I would greatly appreciate if someone read my code and helped me figure out how i can go from were I am now up to where i want to be (before the particles part) - the code and image is below, sorry if i did this wrong its my first time!
main page:
import sys
import pygame
from settings import Settings
from cloud import Cloud
from drop import Drop
class Raining:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.clock = pygame.time.Clock()
self.settings = Settings()
self.dropsperframe = 0
self.drops_allowed = 100
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Raining")
self.cloud = Cloud(self)
self.drop = pygame.sprite.Group()
self.is_raining = False
def run_game(self):
"""Start the main loop for the game."""
while True:
self._rain_dropping()
self._check_events()
self.cloud.update()
self._update_drops()
self._update_screen()
self.clock.tick(60)
def _check_events(self):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)
def _check_keydown_events(self, event):
"""Respond to keypresses."""
if event.key == pygame.K_RIGHT:
self.cloud.moving_right = True
elif event.key == pygame.K_LEFT:
self.cloud.moving_left = True
elif event.key == pygame.K_q:
sys.exit()
elif event.key == pygame.K_SPACE:
self.is_raining = True
def _check_keyup_events(self, event):
"""Respond to key releases."""
if event.key == pygame.K_RIGHT:
self.cloud.moving_right = False
elif event.key == pygame.K_LEFT:
self.cloud.moving_left = False
elif event.key == pygame.K_SPACE:
if self.dropsperframe > 0 and self.drops_allowed > 0:
self.dropsperframe -= 0.1
self.drops_allowed -= 10
def _rain_dropping(self):
"""Create a new drop and add it to the drops group."""
if len(self.drop) < self.drops_allowed:
new_drop = Drop(self)
if self.dropsperframe >= 2:
self.drop.add(new_drop)
self.drop.add(new_drop)
elif self.dropsperframe >= 1:
self.drop.add(new_drop)
if self.is_raining == True:
self.dropsperframe += 0.01
self.drops_allowed += 50
def _update_drops(self):
"""Update position of drops and get rid of old drops."""
# Update drop positions.
self.drop.update()
# Get rid of drops that have disappeared.
for drop in self.drop.copy():
if drop.rect.bottom >= 800:
self.drop.remove(drop)
def _update_screen(self):
"""Update images on the screen, and flip to the new screen."""
self.screen.fill(self.settings.bg_color)
self.cloud.blitme()
for drop in self.drop.sprites():
drop.draw_drop()
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
rai = Raining()
rai.run_game()
cloud code (the cloud that moves around):
import pygame
class Cloud:
"""A class to manage the cloud."""
def __init__(self, rai_game):
"""Initialize the ship and set its starting position."""
self.screen = rai_game.screen
self.settings = rai_game.settings
self.screen_rect = rai_game.screen.get_rect()
# Load the cloud image and get its rect.
self.image = pygame.image.load('raincloudcrop.png')
self.rect = self.image.get_rect()
# Start each new cloud at the bottom center of the screen.
self.rect.top = self.screen_rect.top
# Store a decimal value for the cloud's horizontal position.
self.x = float(self.rect.x)
# Movement flags
self.moving_right = False
self.moving_left = False
def update(self):
"""Update the cloud's position based on movement flags."""
# Update the cloud's x value, not the rect.
if self.moving_right and self.rect.right < self.screen_rect.right:
self.x += self.settings.cloud_speed
if self.moving_left and self.rect.left > 0:
self.x -= self.settings.cloud_speed
# Update rect object from self.x.
self.rect.x = self.x
def blitme(self):
"""Draw the cloud at its current location."""
self.screen.blit(self.image, self.rect)
droplet code(the raindrop):
import pygame
from pygame.sprite import Sprite
from random import randint
class Drop(Sprite):
"""A class to manage drops dropped from the cloud"""
def __init__(self, rai_game):
"""Create a drop object at the cloud's current position."""
super().__init__()
self.screen = rai_game.screen
self.settings = rai_game.settings
self.color = self.settings.drop_color
# Create a drop rect at (0, 0) and then set correct position.
self.rect = pygame.Rect(0, 0, self.settings.drop_width,
self.settings.drop_height)
self.rect.x = rai_game.cloud.rect.x + randint(1, 122)
self.rect.y = rai_game.cloud.rect.y + 62
# Store the drop's position as a decimal value.
self.y = float(self.rect.y)
self.is_raining = False
def update(self):
"""Move the drop up the screen."""
# Update the decimal position of the drop.
self.y += randint(1,5)
# Update the rect position.
self.rect.y = self.y
def draw_drop(self):
"""Draw the drop to the screen."""
pygame.draw.rect(self.screen, self.color, self.rect)
the settings of the game:
from random import randint
class Settings:
"""A class to store all settings for cloud."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
# cloud settings
self.cloud_speed = 1.5
# droplet settings
self.drop_width = 4
self.drop_height = 5
self.drop_color = (66, 135, 245)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
