Skip to content

Commit c0839b6

Browse files
author
Chris Bradfield
committed
initial commit
1 parent bacfc05 commit c0839b6

File tree

850 files changed

+35088
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

850 files changed

+35088
-0
lines changed

1-2 sprite example.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Pygame sprite Example
2+
import pygame
3+
import random
4+
5+
WIDTH = 800
6+
HEIGHT = 600
7+
FPS = 30
8+
9+
# define colors
10+
WHITE = (255, 255, 255)
11+
BLACK = (0, 0, 0)
12+
RED = (255, 0, 0)
13+
GREEN = (0, 255, 0)
14+
BLUE = (0, 0, 255)
15+
16+
class Player(pygame.sprite.Sprite):
17+
# sprite for the Player
18+
def __init__(self):
19+
# this line is required to properly create the sprite
20+
pygame.sprite.Sprite.__init__(self)
21+
# create a plain rectangle for the sprite image
22+
self.image = pygame.Surface((50, 50))
23+
self.image.fill(GREEN)
24+
# find the rectangle that encloses the image
25+
self.rect = self.image.get_rect()
26+
# center the sprite on the screen
27+
self.rect.center = (WIDTH / 2, HEIGHT / 2)
28+
29+
def update(self):
30+
# any code here will happen every time the game loop updates
31+
self.rect.x += 5
32+
if self.rect.left > WIDTH:
33+
self.rect.right = 0
34+
35+
# initialize pygame and create window
36+
pygame.init()
37+
pygame.mixer.init()
38+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
39+
pygame.display.set_caption("Sprite Example")
40+
clock = pygame.time.Clock()
41+
42+
all_sprites = pygame.sprite.Group()
43+
player = Player()
44+
all_sprites.add(player)
45+
# Game loop
46+
running = True
47+
while running:
48+
# keep loop running at the right speed
49+
clock.tick(FPS)
50+
# Process input (events)
51+
for event in pygame.event.get():
52+
# check for closing window
53+
if event.type == pygame.QUIT:
54+
running = False
55+
56+
# Update
57+
all_sprites.update()
58+
59+
# Draw / render
60+
screen.fill(BLACK)
61+
all_sprites.draw(screen)
62+
# *after* drawing everything, flip the display
63+
pygame.display.flip()
64+
65+
pygame.quit()

1-3 sprite example.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Pygame sprite Example
2+
import pygame
3+
import random
4+
import os
5+
6+
WIDTH = 800
7+
HEIGHT = 600
8+
FPS = 30
9+
10+
# define colors
11+
WHITE = (255, 255, 255)
12+
BLACK = (0, 0, 0)
13+
RED = (255, 0, 0)
14+
GREEN = (0, 255, 0)
15+
BLUE = (0, 0, 255)
16+
17+
# set up assets folders
18+
# Windows: "C:\Users\chris\Documents\img"
19+
# Mac: "/Users/chris/Documents/img"
20+
game_folder = os.path.dirname(__file__)
21+
img_folder = os.path.join(game_folder, "img")
22+
23+
class Player(pygame.sprite.Sprite):
24+
# sprite for the Player
25+
def __init__(self):
26+
pygame.sprite.Sprite.__init__(self)
27+
self.image = pygame.image.load(os.path.join(img_folder, "p1_jump.png")).convert()
28+
self.image.set_colorkey(BLACK)
29+
self.rect = self.image.get_rect()
30+
self.rect.center = (WIDTH / 2, HEIGHT / 2)
31+
self.y_speed = 5
32+
33+
def update(self):
34+
self.rect.x += 5
35+
self.rect.y += self.y_speed
36+
if self.rect.bottom > HEIGHT - 200:
37+
self.y_speed = -5
38+
if self.rect.top < 200:
39+
self.y_speed = 5
40+
if self.rect.left > WIDTH:
41+
self.rect.right = 0
42+
43+
# initialize pygame and create window
44+
pygame.init()
45+
pygame.mixer.init()
46+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
47+
pygame.display.set_caption("My Game")
48+
clock = pygame.time.Clock()
49+
50+
all_sprites = pygame.sprite.Group()
51+
player = Player()
52+
all_sprites.add(player)
53+
# Game loop
54+
running = True
55+
while running:
56+
# keep loop running at the right speed
57+
clock.tick(FPS)
58+
# Process input (events)
59+
for event in pygame.event.get():
60+
# check for closing window
61+
if event.type == pygame.QUIT:
62+
running = False
63+
64+
# Update
65+
all_sprites.update()
66+
67+
# Draw / render
68+
screen.fill(BLUE)
69+
all_sprites.draw(screen)
70+
# *after* drawing everything, flip the display
71+
pygame.display.flip()
72+
73+
pygame.quit()

breakout/working/main.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# KidsCanCode - Game Development with Pygame video series
2+
# Jumpy! (a platform game) - Part 2
3+
# Video link: https://www.youtube.com/watch?v=8LRI0RLKyt0
4+
# Player movement
5+
6+
import pygame as pg
7+
import random
8+
from settings import *
9+
from sprites import *
10+
11+
class Game:
12+
def __init__(self):
13+
# initialize game window, etc
14+
pg.init()
15+
pg.mixer.init()
16+
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
17+
pg.display.set_caption(TITLE)
18+
self.clock = pg.time.Clock()
19+
self.running = True
20+
self.font_name = pg.font.match_font('arial')
21+
22+
def new(self):
23+
# start a new game
24+
self.all_sprites = pg.sprite.Group()
25+
self.bricks = pg.sprite.Group()
26+
self.create_bricks()
27+
self.paddle = Paddle()
28+
self.all_sprites.add(self.paddle)
29+
self.ball = Ball()
30+
self.all_sprites.add(self.ball)
31+
self.run()
32+
33+
def create_bricks(self):
34+
# Create a grid of bricks (14 x 5)
35+
padding = (WIDTH - (BRICK_WIDTH + BRICK_SPACING) * BRICK_COLS) // 2
36+
for y in range(BRICK_ROWS):
37+
for x in range(BRICK_COLS):
38+
brick = Brick(padding + x * (BRICK_WIDTH + BRICK_SPACING),
39+
BRICK_HEIGHT * 2 + y * (BRICK_HEIGHT + BRICK_SPACING),
40+
BRICK_COLORS[y])
41+
self.all_sprites.add(brick)
42+
self.bricks.add(brick)
43+
44+
def run(self):
45+
# Game Loop
46+
self.playing = True
47+
while self.playing:
48+
self.clock.tick(FPS)
49+
self.events()
50+
self.update()
51+
self.draw()
52+
53+
def update(self):
54+
# Game Loop - Update
55+
self.all_sprites.update()
56+
57+
def events(self):
58+
# Game Loop - events
59+
for event in pg.event.get():
60+
# check for closing window
61+
if event.type == pg.QUIT:
62+
if self.playing:
63+
self.playing = False
64+
self.running = False
65+
66+
def draw(self):
67+
# Game Loop - draw
68+
self.screen.fill(BLACK)
69+
self.all_sprites.draw(self.screen)
70+
# *after* drawing everything, flip the display
71+
pg.display.flip()
72+
73+
def show_start_screen(self):
74+
# game splash/start screen
75+
pass
76+
77+
def show_go_screen(self):
78+
# game over/continue
79+
pass
80+
81+
def draw_text(surf, text, size, x, y):
82+
font = pg.font.Font(font_name, size)
83+
text_surface = font.render(text, True, WHITE)
84+
text_rect = text_surface.get_rect()
85+
text_rect.midtop = (x, y)
86+
surf.blit(text_surface, text_rect)
87+
88+
g = Game()
89+
g.show_start_screen()
90+
while g.running:
91+
g.new()
92+
g.show_go_screen()
93+
94+
pg.quit()

breakout/working/settings.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# game options/settings
2+
TITLE = "Breakout"
3+
WIDTH = 800
4+
HEIGHT = 600
5+
FPS = 60
6+
7+
# define colors
8+
WHITE = (255, 255, 255)
9+
BLACK = (0, 0, 0)
10+
RED = (255, 0, 0)
11+
GREEN = (0, 255, 0)
12+
BLUE = (0, 0, 255)
13+
YELLOW = (255, 255, 0)
14+
ORANGE = (255, 128, 0)
15+
16+
# Paddle properties
17+
PADDLE_WIDTH = 100
18+
PADDLE_HEIGHT = 20
19+
PADDLE_COLOR = WHITE
20+
PADDLE_ACC = 1.2
21+
PADDLE_FRICTION = -0.1
22+
23+
# Ball properties
24+
BALL_SIZE = 15
25+
BALL_COLOR = WHITE
26+
BALL_SPEED = 9
27+
28+
# Brick properties
29+
BRICK_WIDTH = 50
30+
BRICK_HEIGHT = 20
31+
BRICK_ROWS = 5
32+
BRICK_COLS = 14
33+
BRICK_SPACING = 5
34+
BRICK_COLORS = [BLUE, GREEN, YELLOW, ORANGE, RED]

breakout/working/sprites.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Sprite classes for platform game
2+
import pygame as pg
3+
from random import randrange, choice
4+
from settings import *
5+
vec = pg.math.Vector2
6+
7+
class Paddle(pg.sprite.Sprite):
8+
def __init__(self):
9+
pg.sprite.Sprite.__init__(self)
10+
self.image = pg.Surface((PADDLE_WIDTH, PADDLE_HEIGHT))
11+
self.image.fill(WHITE)
12+
self.rect = self.image.get_rect()
13+
self.rect.center = (WIDTH / 2, HEIGHT - 30)
14+
self.pos = vec(WIDTH / 2, HEIGHT - 30)
15+
self.vel = vec(0, 0)
16+
self.acc = vec(0, 0)
17+
18+
def update(self):
19+
self.update_acc()
20+
21+
def update_abs(self):
22+
# absolute position - paddle follows mouse
23+
pos = pg.mouse.get_pos()
24+
self.pos.x = pos[0]
25+
if self.pos.x > WIDTH - PADDLE_WIDTH / 2:
26+
self.pos.x = WIDTH - PADDLE_WIDTH / 2
27+
if self.pos.x < PADDLE_WIDTH / 2:
28+
self.pos.x = PADDLE_WIDTH / 2
29+
self.rect.center = self.pos
30+
31+
def update_acc(self):
32+
# paddle accelerates towards mouse
33+
self.acc = vec(0, 0)
34+
pos = pg.mouse.get_pos()
35+
if abs(pos[0] - self.pos.x) < 50:
36+
self.acc = vec(0, 0)
37+
elif pos[0] < self.pos.x:
38+
self.acc.x = -PADDLE_ACC
39+
elif pos[0] > self.pos.x:
40+
self.acc.x = PADDLE_ACC
41+
42+
self.acc.x += self.vel.x * PADDLE_FRICTION
43+
self.vel += self.acc
44+
self.pos += self.vel + 0.5 * self.acc
45+
if self.pos.x > WIDTH - PADDLE_WIDTH / 2:
46+
self.pos.x = WIDTH - PADDLE_WIDTH / 2
47+
if self.pos.x < PADDLE_WIDTH / 2:
48+
self.pos.x = PADDLE_WIDTH / 2
49+
self.rect.center = self.pos
50+
51+
class Ball(pg.sprite.Sprite):
52+
def __init__(self):
53+
pg.sprite.Sprite.__init__(self)
54+
self.speed = BALL_SPEED
55+
self.image = pg.Surface((BALL_SIZE, BALL_SIZE))
56+
self.image.fill(BALL_COLOR)
57+
self.rect = self.image.get_rect()
58+
self.rect.center = (WIDTH / 2, HEIGHT / 2)
59+
self.pos = vec(WIDTH / 2, HEIGHT / 2)
60+
self.vel = vec(choice([-2, -1, 1, 2]), -1)
61+
self.vel = self.vel.normalize() * self.speed
62+
63+
def update(self):
64+
self.pos += self.vel
65+
self.rect.center = self.pos
66+
if self.rect.left < 0 or self.rect.right > WIDTH:
67+
self.vel.x *= -1
68+
if self.rect.top < 0 or self.rect.bottom > HEIGHT:
69+
self.vel.y *= -1
70+
71+
class Brick(pg.sprite.Sprite):
72+
def __init__(self, x, y, col):
73+
pg.sprite.Sprite.__init__(self)
74+
self.image = pg.Surface((BRICK_WIDTH, BRICK_HEIGHT))
75+
self.image.fill(col)
76+
self.rect = self.image.get_rect()
77+
self.rect.x = x
78+
self.rect.y = y

0 commit comments

Comments
 (0)