Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Quarantine-game/sprites.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
141 lines (126 sloc)
5.4 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sprite classes | |
from settings import * | |
import pygame | |
vec = pygame.math.Vector2 | |
class SpriteSheet: | |
# utility class for loading and parsing spritesheets | |
def __init__(self, filename): | |
self.spritesheet1 = pygame.image.load(filename).convert() | |
self.spritesheet1 = 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.spritesheet1, (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, x, y): | |
pygame.sprite.Sprite.__init__(self) | |
self.game = game | |
self.load_images() | |
self.image = self.standing_frames[0] | |
self.image.set_colorkey((0, 0, 0)) | |
self.walking = False | |
self.jumping = False | |
self.current_frame = 0 | |
self.last_update = 0 | |
self.rect = self.image.get_rect() | |
self.x = x | |
self.y = y | |
self.pos = vec(x * tilesize, y * tilesize) | |
self.vel = vec(0, 0) | |
self.acc = vec(0, 0) | |
def load_images(self): | |
self.standing_frames = [self.game.spritesheet1.get_image(67, 190, 66, 92), | |
self.game.spritesheet1.get_image(0, 190, 66, 92)] | |
for frame in self.standing_frames: | |
frame.set_colorkey((0, 0, 0)) | |
self.walk_frames_r = [self.game.spritesheet1.get_image(0, 0, 70, 94), | |
self.game.spritesheet1.get_image(71, 0, 70, 94), | |
self.game.spritesheet1.get_image(142, 0, 70, 94), | |
self.game.spritesheet1.get_image(0, 95, 70, 94), | |
self.game.spritesheet1.get_image(71, 95, 70, 94), | |
self.game.spritesheet1.get_image(142, 95, 70, 94), | |
self.game.spritesheet1.get_image(213, 0, 70, 94), | |
self.game.spritesheet1.get_image(284, 0, 70, 94), | |
self.game.spritesheet1.get_image(213, 95, 70, 94), | |
self.game.spritesheet1.get_image(355, 0, 70, 94), | |
self.game.spritesheet1.get_image(284, 95, 70, 94)] | |
self.walk_frames_l = [] | |
for frame in self.walk_frames_r: | |
self.walk_frames_l.append(pygame.transform.flip(frame, True, False)) | |
frame.set_colorkey((0, 0, 0)) | |
for frame in self.walk_frames_l: | |
frame.set_colorkey((0, 0, 0)) | |
self.jump_frame_r = self.game.spritesheet1.get_image(423, 95, 66, 94) | |
self.jump_frame_r.set_colorkey((0, 0, 0)) | |
self.jump_frame_l = pygame.transform.flip(self.jump_frame_r, True, False) | |
self.jump_frame_l.set_colorkey((0, 0, 0)) | |
def jump_cut(self): | |
if self.jumping: | |
if self.vel.y < -3: | |
self.vel.y = -3 | |
def jump(self): | |
# jump only if standing on a platform | |
self.y += 1 | |
hits = pygame.sprite.spritecollide(self, self.game.tile, False) | |
self.y -= 1 | |
if hits and not self.jumping: | |
self.game.jump_sound.play() | |
self.jumping = True | |
self.vel.y = -player_jump | |
def update(self): | |
self.animate() | |
self.acc = vec(0, player_gravity) | |
keys = pygame.key.get_pressed() | |
if keys[pygame.K_LEFT]: | |
self.acc.x = -player_acc | |
if keys[pygame.K_RIGHT]: | |
self.acc.x = player_acc | |
# applies friction | |
self.acc.x += self.vel.x * player_friction | |
# equations of motion | |
self.vel += self.acc | |
if abs(self.vel.x) < 0.1: | |
self.vel.x = 0 | |
self.x += (self.vel.x + 0.5 * self.acc.x) / 32 | |
self.y += (self.vel.y + 0.5 * self.acc.y) / 32 | |
self.rect.x = self.x * tilesize | |
self.rect.y = self.y * tilesize | |
def animate(self): | |
now = pygame.time.get_ticks() | |
if self.vel.x != 0: | |
self.walking = True | |
else: | |
self.walking = False | |
# show walking animation | |
if self.walking: | |
if (now - self.last_update) > 100: | |
self.last_update = now | |
self.current_frame = (self.current_frame + 1) % len(self.walk_frames_l) | |
if self.vel.x > 0: | |
self.image = self.walk_frames_r[self.current_frame] | |
else: | |
self.image = self.walk_frames_l[self.current_frame] | |
self.rect = self.image.get_rect() | |
# show idle animation | |
if not self.jumping and not self.walking: | |
if (now - self.last_update) > 400: | |
self.last_update = now | |
self.current_frame = (self.current_frame + 1) % len(self.standing_frames) | |
self.image = self.standing_frames[self.current_frame] | |
self.rect = self.image.get_rect() | |
class Tile(pygame.sprite.Sprite): # tile based | |
def __init__(self, game, x, y): | |
self.groups = game.all_sprites, game.tile | |
pygame.sprite.Sprite.__init__(self, self.groups) | |
self.game = game | |
self.image = pygame.Surface((tilesize, tilesize)) | |
self.image.fill((0, 128, 0)) | |
self.rect = self.image.get_rect() | |
self.x = x | |
self.y = y | |
self.rect.x = x * tilesize | |
self.rect.y = y * tilesize | |
class Powerup(pygame.sprite.Sprite): | |
pass |