Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericbusboom committed Sep 30, 2024
1 parent d2314b5 commit bf4fb6e
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 119 deletions.
56 changes: 35 additions & 21 deletions examples/02_gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,44 @@
"""
import pygame
from dataclasses import dataclass

# Initialize Pygame
pygame.init()

# Screen dimensions
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 500

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# This is a data class, one way of storing settings and constants for a game.
# We will create an instance of the data class, but since there is only one of
# them, we could also use the class directly, like GameSettings.screen_width.
# You can check that the instance has the same values as the class:
# settings = GameSettings()
# assert GameSettings.screen_width == settings.screen_width
@dataclass
class GameSettings:
"""Class for keeping track of game settings."""
screen_width: int = 500
screen_height: int = 500
player_size: int = 10
player_x: int = 100 # Initial x position of the player
gravity: float = 0.3 # acelleration, the change in velocity per frame
jump_velocity: int = 15
white: tuple = (255, 255, 255)
black: tuple = (0, 0, 0)
tick_rate: int = 30 # Frames per second

# Initialize game settings
settings = GameSettings()

# Game settings
PLAYER_SIZE = 10

GRAVITY = .3
JUMP_VELOCITY = 15

# Initialize screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))

# Define player
player = pygame.Rect(100, SCREEN_HEIGHT - PLAYER_SIZE, PLAYER_SIZE, PLAYER_SIZE)
player = pygame.Rect(settings.player_x,
settings.screen_height - settings.player_size,
settings.player_size, settings.player_size)

player_y_velocity = 0
is_jumping = False

Expand All @@ -44,38 +59,37 @@
if event.type == pygame.QUIT:
running = False

# Continuously jump. If the player is not jumping, may it jump
# Continuously jump. If the player is not jumping, initialize a new jump
if is_jumping is False:
# Jumping means that the player is going up. The top of the
# screen is y=0, and the bottom is y=SCREEN_HEIGHT. So, to go up,
# we need to have a negative y velocity
player_y_velocity = -JUMP_VELOCITY
player_y_velocity = -settings.jump_velocity
is_jumping = True

# Update player position. Gravity is always pulling the player down,
# which is the positive y direction, so we add GRAVITY to the y velocity
# to make the player go up more slowly. Eventually, the player will have
# a positive y velocity, and gravity will pull the player down.
player_y_velocity += GRAVITY
player_y_velocity += settings.gravity
player.y += player_y_velocity


# If the player hits the ground, stop the player from falling.
# The player's position is measured from the top left corner, so the
# bottom of the player is player.y + PLAYER_SIZE. If the bottom of the
# player is greater than the height of the screen, the player is on the
# ground. So, set the player's y position to the bottom of the screen
# and stop the player from falling
if player.y >= SCREEN_HEIGHT - PLAYER_SIZE:
player.y = SCREEN_HEIGHT - PLAYER_SIZE
if player.y >= settings.screen_height - settings.player_size:
player.y = settings.screen_height - settings.player_size
player_y_velocity = 0
is_jumping = False

# Draw everything
screen.fill(WHITE)
pygame.draw.rect(screen, BLACK, player)
screen.fill(settings.white)
pygame.draw.rect(screen, settings.black, player)

pygame.display.flip()
clock.tick(30)
clock.tick(settings.tick_rate)

pygame.quit()
62 changes: 30 additions & 32 deletions examples/03a_gravity_bounce.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
"""
Gravity bounce with x motion
If we add X velocity, the player will bounce around the screen. We will need to
add a check to see if the player hits the left or right side of the screen.
If we add X velocity, from side to side, the player will bounce around the
screen. We will need to add a check to see if the player hits the left or right
side of the screen.
"""
import pygame
from dataclasses import dataclass

@dataclass
class Settings:
"""Class for keeping track of game settings and constants."""
screen_width: int = 500
screen_height: int = 500
white: tuple = (255, 255, 255)
black: tuple = (0, 0, 0)
red: tuple = (255, 0, 0)
player_size: int = 20
gravity: int = 1
jump_velocity: int = 30

# Initialize Pygame
pygame.init()

# Screen dimensions
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 500

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED=(255,0,0)

# Game settings
PLAYER_SIZE = 20


GRAVITY = 1
JUMP_VELOCITY = 30
# Create an instance of Settings
settings = Settings()

# Initialize screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))

# Define player
player = pygame.Rect(100, SCREEN_HEIGHT - PLAYER_SIZE, PLAYER_SIZE, PLAYER_SIZE)
player = pygame.Rect(100, settings.screen_height - settings.player_size, settings.player_size, settings.player_size)
player_y_velocity = 0
player_x_velocity = 10

is_jumping = False


# Main game loop
running = True
clock = pygame.time.Clock()
Expand All @@ -51,43 +51,41 @@
# Continuously jump. If the player is not jumping, may it jump
if is_jumping is False:
# Jumping means that the player is going up. The top of the
# screen is y=0, and the bottom is y=SCREEN_HEIGHT. So, to go up,
# screen is y=0, and the bottom is y=settings.screen_height. So, to go up,
# we need to have a negative y velocity
player_y_velocity = -JUMP_VELOCITY
player_y_velocity = -settings.jump_velocity
is_jumping = True

# If the player hits one side of the screen or the other, bounce the player
if player.x <= 0 or player.x >= SCREEN_WIDTH - PLAYER_SIZE:
if player.x <= 0 or player.x >= settings.screen_width - settings.player_size:
player_x_velocity = -player_x_velocity

# If the player hits the top of the screen, bounce the player
if player.y <= 0:
player_y_velocity = -player_y_velocity


# Update player position. Gravity is always pulling the player down,
# which is the positive y direction, so we add GRAVITY to the y velocity
# which is the positive y direction, so we add settings.gravity to the y velocity
# to make the player go up more slowly. Eventually, the player will have
# a positive y velocity, and gravity will pull the player down.
player_y_velocity += GRAVITY
player_y_velocity += settings.gravity
player.y += player_y_velocity
player.x += player_x_velocity

# If the player hits the ground, stop the player from falling.
# The player's position is measured from the top left corner, so the
# bottom of the player is player.y + PLAYER_SIZE. If the bottom of the
# bottom of the player is player.y + settings.player_size. If the bottom of the
# player is greater than the height of the screen, the player is on the
# ground. So, set the player's y position to the bottom of the screen
# and stop the player from falling
if player.y >= SCREEN_HEIGHT - PLAYER_SIZE:
player.y = SCREEN_HEIGHT - PLAYER_SIZE
if player.y >= settings.screen_height - settings.player_size:
player.y = settings.screen_height - settings.player_size
player_y_velocity = 0
is_jumping = False


# Draw everything
screen.fill(WHITE)
pygame.draw.rect(screen, BLACK, player)
screen.fill(settings.white)
pygame.draw.rect(screen, settings.black, player)

pygame.display.flip()
clock.tick(30)
Expand Down
Loading

0 comments on commit bf4fb6e

Please sign in to comment.