Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions pygame-a-primer/Pipfile

This file was deleted.

107 changes: 0 additions & 107 deletions pygame-a-primer/Pipfile.lock

This file was deleted.

72 changes: 36 additions & 36 deletions pygame-a-primer/py_tut_with_images.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# Import the pygame module
import pygame

# Import random for random numbers
import random

import pygame

# Import pygame.locals for easier access to key coordinates
# Updated to conform to flake8 and black standards
# from pygame.locals import *
from pygame.locals import (
RLEACCEL,
K_UP,
K_DOWN,
K_ESCAPE,
K_LEFT,
K_RIGHT,
K_UP,
K_ESCAPE,
KEYDOWN,
QUIT,
RLEACCEL,
)

# Define constants for the screen width and height
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600


# Define the Player object extending pygame.sprite.Sprite
# Instead of a surface, we use an image for a better looking sprite
# Define the Player object by extending pygame.sprite.Sprite
# Instead of a surface, use an image for a better-looking sprite
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
Expand All @@ -48,16 +48,16 @@ def update(self, pressed_keys):
# Keep player on the screen
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > SCREEN_WIDTH:
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top <= 0:
self.rect.top = 0
elif self.rect.bottom >= SCREEN_HEIGHT:
if self.rect.bottom >= SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT


# Define the enemy object extending pygame.sprite.Sprite
# Instead of a surface, we use an image for a better looking sprite
# Define the enemy object by extending pygame.sprite.Sprite
# Instead of a surface, use an image for a better-looking sprite
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super(Enemy, self).__init__()
Expand All @@ -72,16 +72,16 @@ def __init__(self):
)
self.speed = random.randint(5, 20)

# Move the enemy based on speed
# Remove it when it passes the left edge of the screen
# Move the sprite based on speed
# Remove the sprite when it passes the left edge of the screen
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
self.kill()


# Define the cloud object extending pygame.sprite.Sprite
# Use an image for a better looking sprite
# Define the cloud object by extending pygame.sprite.Sprite
# Use an image for a better-looking sprite
class Cloud(pygame.sprite.Sprite):
def __init__(self):
super(Cloud, self).__init__()
Expand All @@ -96,51 +96,51 @@ def __init__(self):
)

# Move the cloud based on a constant speed
# Remove it when it passes the left edge of the screen
# Remove the cloud when it passes the left edge of the screen
def update(self):
self.rect.move_ip(-5, 0)
if self.rect.right < 0:
self.kill()


# Setup for sounds, defaults are good
# Setup for sounds. Defaults are good.
pygame.mixer.init()

# Initialize pygame
pygame.init()

# Setup the clock for a decent framerate
# Set up the clock for a decent framerate
clock = pygame.time.Clock()

# Create the screen object
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# Create custom events for adding a new enemy and cloud
# Create custom events for adding a new enemy and a cloud
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 250)
ADDCLOUD = pygame.USEREVENT + 2
pygame.time.set_timer(ADDCLOUD, 1000)

# Create our 'player'
# Create the 'player'
player = Player()

# Create groups to hold enemy sprites, cloud sprites, and all sprites
# - enemies is used for collision detection and position updates
# - clouds is used for position updates
# - all_sprites isused for rendering
# - all_sprites is used for rendering
enemies = pygame.sprite.Group()
clouds = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

# Load and play our background music
# Load and play background music
# Sound source: http://ccmixter.org/files/Apoxode/59262
# License: https://creativecommons.org/licenses/by/3.0/
pygame.mixer.music.load("Apoxode_-_Electric_1.mp3")
pygame.mixer.music.play(loops=-1)

# Load all our sound files
# Load all sound files
# Sound sources: Jon Fincher
move_up_sound = pygame.mixer.Sound("Rising_putter.ogg")
move_down_sound = pygame.mixer.Sound("Falling_putter.ogg")
Expand All @@ -151,33 +151,33 @@ def update(self):
move_down_sound.set_volume(0.5)
collision_sound.set_volume(0.5)

# Variable to keep our main loop running
# Variable to keep the main loop running
running = True

# Our main loop
# Main loop
while running:
# Look at every event in the queue
for event in pygame.event.get():
# Did the user hit a key?
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop
# Was it the Escape key? If so, then stop the loop.
if event.key == K_ESCAPE:
running = False

# Did the user click the window close button? If so, stop the loop
# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
running = False

# Should we add a new enemy?
# Add a new enemy?
elif event.type == ADDENEMY:
# Create the new enemy, and add it to our sprite groups
# Create the new enemy and add it to sprite groups
new_enemy = Enemy()
enemies.add(new_enemy)
all_sprites.add(new_enemy)

# Should we add a new cloud?
# Add a new cloud?
elif event.type == ADDCLOUD:
# Create the new cloud, and add it to our sprite groups
# Create the new cloud and add it to sprite groups
new_cloud = Cloud()
clouds.add(new_cloud)
all_sprites.add(new_cloud)
Expand All @@ -186,20 +186,20 @@ def update(self):
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)

# Update the position of our enemies and clouds
# Update the position of enemies and clouds
enemies.update()
clouds.update()

# Fill the screen with sky blue
screen.fill((135, 206, 250))

# Draw all our sprites
# Draw all sprites
for entity in all_sprites:
screen.blit(entity.surf, entity.rect)

# Check if any enemies have collided with the player
if pygame.sprite.spritecollideany(player, enemies):
# If so, remove the player
# If so, then remove the player
player.kill()

# Stop any moving sounds and play the collision sound
Expand All @@ -213,9 +213,9 @@ def update(self):
# Flip everything to the display
pygame.display.flip()

# Ensure we maintain a 30 frames per second rate
# Ensure program maintains a rate of 30 frames per second
clock.tick(30)

# At this point, we're done, so we can stop and quit the mixer
# All done! Stop and quit the mixer.
pygame.mixer.music.stop()
pygame.mixer.quit()
Loading
Loading