I'm trying to build a game something like, balls are coming down randomly and the boy in the bottom catch the ball. I made a row of balls on top in a random manner, but I'm not sure how to make them appear randomly and fall down individually.
baseball_game.py
import sys
import pygame
from pygame.sprite import Group
from settings import Settings
from boy import Boy
from ball import Ball
import game_functions as gf
def run_game():
# Initialize pygame, settings, and screen object.
pygame.init()
bg_settings = Settings()
screen = pygame.display.set_mode(
(bg_settings.screen_width, bg_settings.screen_height))
pygame.display.set_caption("Catch the Baseball!")
# Make a boy
boy = Boy(bg_settings, screen)
balls = Group()
# Create the fleet of aliens.
gf.create_fleet(bg_settings, screen, boy, balls)
# Make a ball
ball = Ball(bg_settings, screen)
# Set the background color.
bg_color = (217, 208, 187)
# Start the main loop for the game.
while True:
gf.check_events(boy)
boy.update()
gf.update_balls(balls)
gf.update_screen(bg_settings, screen, boy, balls)
run_game()
ball.py
import pygame
from pygame.sprite import Sprite
import random
class Ball(Sprite):
"""A class to represent a single ball."""
def __init__(self, bg_settings, screen):
"""Initalize the ball and set its starting position."""
super(Ball, self).__init__()
self.screen = screen
self.bg_settings = bg_settings
# Load the ball image and set its rect attribute.
self.image = pygame.image.load('images/ball.png')
self.rect = self.image.get_rect()
# Start each new ball.
self.rect.x = random.randint(-10, 40)
self.rect.y = random.randint(-10, 40)
# Store the ball's exact position.
self.y = float(self.rect.y)
def update(self):
"""Move the ball down."""
self.y += self.bg_settings.ball_speed_factor
self.rect.y = self.y
def blitme(self):
"""Draw the ball at its current location."""
self.screen.blit(self.image, self.rect)
game_functions.py
import sys
import pygame
from ball import Ball
from random import randint
random_number = randint(-15, 39)
def check_events(boy):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, boy)
elif event.type == pygame.KEYUP:
check_keyup_events(event, boy)
def check_keydown_events(event, boy):
"""Respond to keypresses."""
if event.key == pygame.K_RIGHT:
boy.moving_right = True
elif event.key == pygame.K_LEFT:
boy.moving_left = True
elif event.key == pygame.K_q:
sys.exit()
def check_keyup_events(event, boy):
"""Respond to key releases."""
if event.key == pygame.K_RIGHT:
boy.moving_right = False
elif event.key == pygame.K_LEFT:
boy.moving_left = False
def update_screen(bg_settings, screen, boy, balls):
"""Update images on the screen and flip to the new screen."""
# Redraw the screen during each pass through the loop.
screen.fill(bg_settings.bg_color)
boy.blitme()
balls.draw(screen)
# Make the most recently drawn screen visible.
pygame.display.flip()
def get_number_balls_x(bg_settings, ball_width):
"""Determine the number of aliens that fit in a row."""
available_space_x = bg_settings.screen_width - 2 * ball_width
number_balls_x = int(available_space_x / (2 * ball_width))
return number_balls_x
def create_ball(bg_settings, screen, balls, ball_number):
"""Create a ball and place it in the row."""
ball = Ball(bg_settings, screen)
ball_width = ball.rect.width
ball.x = ball_width + 2 * ball_width * ball_number
ball.rect.x = ball.x
balls.add(ball)
def create_fleet(bg_settings, screen, boy, balls):
ball = Ball(bg_settings, screen)
number_balls_x = get_number_balls_x(bg_settings, ball.rect.width)
for ball_number in range(number_balls_x):
create_ball(bg_settings, screen, balls, ball_number)
def update_balls(balls):
"""Update the positions of all balls in the fleet."""
balls.update()
So the first row of balls are randomly placed on top, but how do I make it to appear not all at once, and falling down separately?
Aucun commentaire:
Enregistrer un commentaire