Skip to content
Permalink
Browse files
Sprite class
  • Loading branch information
dacostag committed Apr 28, 2020
1 parent 36592ab commit 858b8daee5561ce8e0a238a089370148e731eec7
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 15 deletions.
BIN +237 Bytes (140%) __pycache__/settings.cpython-38.pyc
Binary file not shown.
BIN +598 Bytes (130%) __pycache__/sprites.cpython-38.pyc
Binary file not shown.
BIN +31.2 KB img/p2_spritesheet.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,16 @@
p2_duck = 355 95 67 72
p2_front = 0 190 66 92
p2_hurt = 426 0 67 92
p2_jump = 423 95 66 94
p2_stand = 67 190 66 92
p2_walk01 = 0 0 70 94
p2_walk02 = 71 0 70 94
p2_walk03 = 142 0 70 94
p2_walk04 = 0 95 70 94
p2_walk05 = 71 95 70 94
p2_walk06 = 142 95 70 94
p2_walk07 = 213 0 70 94
p2_walk08 = 284 0 70 94
p2_walk09 = 213 95 70 94
p2_walk10 = 355 0 70 94
p2_walk11 = 284 95 70 94
70 main.py
@@ -4,7 +4,7 @@ import math
from pygame import mixer
from settings import *
from sprites import *

import os.path

class Game:
def __init__(self):
@@ -17,12 +17,21 @@ class Game:
pygame.display.set_caption(title)
icon = pygame.image.load('img/icon.png')
pygame.display.set_icon(icon)
self.font_name = pygame.font.match_font(basic_font)

self.clock = pygame.time.Clock()
self.running = True
self.load_data()

def load_data(self):
self.dir = os.path.dirname(__file__)
img_dir = os.path.join(self.dir, 'img')
self.spritesheet = SpriteSheet(os.path.join(img_dir, player_spritesheet))


def new(self):
# starts a new game not a new program
self.score = 0
self.all_sprites = pygame.sprite.Group()
self.platforms = pygame.sprite.Group()
self.player = Player(self)
@@ -52,6 +61,23 @@ class Game:
self.player.pos.y = hits[0].rect.top
self.player.vel.y = 0

# if player reaches 3/4 of width of the screen
if self.player.rect.x >= (width * 0.75):
self.player.pos.x -= abs(self.player.vel.x)
for plat in self.platforms:
plat.rect.x -= self.player.vel.x
# if player reaches goes left screen moves left 1/10
if self.player.rect.x <= (width * 0.1):
self.player.pos.x += abs(self.player.vel.x)
for plat in self.platforms:
plat.rect.x -= self.player.vel.x

# Die, by falling of the screen
if self.player.rect.bottom > height:
for sprite in self.all_sprites:
sprite.rect.y -= max(self.player.vel.y, 10)
self.playing = False

def events(self):
# Game loop - events
for event in pygame.event.get():
@@ -65,22 +91,56 @@ class Game:

def draw(self):
# Game loop - draw/render
self.screen.blit(background, (0, 0))
self.screen.fill(BG_colour)
self.all_sprites.draw(self.screen)
self.draw_text("Score: " + str(self.score), 26, (255, 255, 255), 40, 15)

# after everything is drawn "flip the board"
pygame.display.flip()

def show_start_screen(self):
# game start screen
pass
self.screen.fill(BG_colour)
self.draw_text(title, 48, (255, 255, 255), width / 2, int(height / 4) + 50)
self.draw_text("Arrows to move, Z to shoot", 22, (255, 255, 255), width / 2, int(height / 2))
self.draw_text("Press a key to play", 22, (255, 255, 255), width / 2, int(height / 2) + 40)
pygame.display.flip()
self.wait_for_key()

def show_go_screen(self):
# game over screen
pass
if not self.running:
return
self.screen.fill(BG_colour)
self.draw_text("GAME OVER", 48, (255, 255, 255), width / 2, int(height / 4) + 50)
self.draw_text("Final Score: " + str(self.score), 22, (255, 255, 255), width / 2, int(height / 2))
self.draw_text("Press a key to play again", 22, (255, 255, 255), width / 2, int(height / 2) + 40)
pygame.display.flip()
self.wait_for_key()

def wait_for_key(self):
waiting = True
while waiting:
self.clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
waiting = False
self.running = False
if event.type == pygame.KEYUP:
waiting = False



def draw_text(self, text, size, color, x, y):
font = pygame.font.Font(self.font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
self.screen.blit(text_surface, text_rect)


g = Game()
g.show_start_screen()

while g.running:
g.new()
g.show_go_screen() # go stands for Game Over
@@ -4,21 +4,29 @@ import pygame
width = 800
height = 425
fps = 30
basic_font = 'courier new.ttf'
textX, textY = 10, 10
player_spritesheet = "p2_spritesheet.png"

title = "You should be in quarantine!"
# setup game assets (images/sounds)
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "img")
sound_folder = os.path.join(game_folder, "sound")
background = pygame.image.load('img/background.jpg')
BG_colour = (69, 219, 222)
score_font = '8-BIT WONDER.ttf'
# Player properties
player_acc = 0.9
player_friction = -0.12
player_gravity = 1.2
player_jump = 15

# Starting platforms

platform_list = [(0, height - 40, width, 40),
# X Y WIDTH HEIGTH
platform_list = [(0, 220, 40, height),
(0, height - 40, width * 3, 40),
(width / 2, 250, 60, 20),
(90, height - 300, 115, 20)]

@@ -5,12 +5,25 @@ import pygame
vec = pygame.math.Vector2


class SpriteSheet:
# utility class for loading and parsing spritesheets
def __init__(self, filename):
self.spritesheet = pygame.image.load(filename).convert()

def get_image(self, x, y, width, height):
# grab single image out of a spritesheet
image = pygame.Surface((width, height))
image.blit(self.spritesheet, (0, 0), (x, y, width, height))
image = pygame.transform.scale(image, (width // 2, height // 2))
return image


class Player(pygame.sprite.Sprite):
def __init__(self,game):
def __init__(self, game):
pygame.sprite.Sprite.__init__(self)
self.game = game
self.image = pygame.Surface((20, 40))
self.image.fill((0, 0, 255))
self.image = self.game.spritesheet.get_image(67, 190, 66, 92)
self.image.set_colorkey((0, 0, 0))
self.rect = self.image.get_rect()
self.rect.center = (width / 2, height / 2)
self.pos = vec(width / 2, height / 2) # x and y position
@@ -23,7 +36,7 @@ class Player(pygame.sprite.Sprite):
hits = pygame.sprite.spritecollide(self, self.game.platforms, False)
self.rect.x -= 1
if hits:
self.vel.y = -20
self.vel.y = -player_jump

def update(self):
self.acc = vec(0, player_gravity)
@@ -38,19 +51,18 @@ class Player(pygame.sprite.Sprite):
# equations of motion
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
#wrap around the screen
if self.pos.x > width:
# wrap around the screen
if self.pos.x < 0:
self.pos.x = 0
elif self.pos.x < 0:
self.pos.x = width

self.rect.midbottom = self.pos


class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, w, h):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((w, h))
self.image.fill((128, 128, 128))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.rect.y = y

0 comments on commit 858b8da

Please sign in to comment.