Skip to content

Instantly share code, notes, and snippets.

@cbscribe
Last active August 7, 2021 00:04
Show Gist options
  • Save cbscribe/5618270f06a06c7de84de40a11aa2579 to your computer and use it in GitHub Desktop.
Save cbscribe/5618270f06a06c7de84de40a11aa2579 to your computer and use it in GitHub Desktop.
pygame jumper
# https://gist.github.com/cbscribe
import pygame
import random
WIDTH = 800
HEIGHT = 640
FPS = 60
RUN_SPEED = 11
GRAVITY = 2
JUMP_SPEED = -31
CELLSIZE = 32
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
player_stand = pygame.image.load("alienGreen_front.png")
player_stand = pygame.transform.rotozoom(player_stand, 0, 0.5)
grass = pygame.image.load("grassMid.png")
grass = pygame.transform.rotozoom(grass, 0, 0.25)
coin = pygame.image.load("coinGold.png")
coin = pygame.transform.rotozoom(coin, 0, 0.4)
snail = pygame.image.load("snail_move.png")
snail = pygame.transform.rotozoom(snail, 0, 0.5)
class Mob(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(all_sprites, mobs)
self.image = snail
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.speed = 8
self.dir = 1
self.vx = 0
self.vy = 0
def update(self):
self.vy += GRAVITY
self.vx = self.speed * self.dir
# pasted from Player
self.rect.x += self.vx
self.check_platforms("x")
self.rect.y += self.vy
self.check_platforms("y")
def check_platforms(self, direction):
hits = pygame.sprite.spritecollide(self, platforms, False)
if hits and direction == "y":
if self.vy > 0:
self.rect.bottom = hits[0].rect.top
elif self.vy < 0:
self.rect.top = hits[0].rect.bottom
self.vy = 0
if hits and direction == "x":
if self.vx > 0:
self.rect.right = hits[0].rect.left
self.dir = -1
self.vy = -10
self.image = pygame.transform.flip(snail, False, False)
elif self.vx < 0:
self.rect.left = hits[0].rect.right
self.dir = 1
self.vy = -10
self.image = pygame.transform.flip(snail, True, False)
self.vx = 0
class Coin(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(all_sprites, coins)
self.image = coin
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(all_sprites, platforms)
self.image = grass
#self.image.fill((54, 171, 46))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__(all_sprites)
self.image = player_stand
#self.image.fill((224, 222, 56))
self.rect = self.image.get_rect()
self.rect.center = (WIDTH/2, HEIGHT - 150)
self.vx = 0
self.vy = 0
def update(self):
self.vy += GRAVITY
keys = pygame.key.get_pressed()
self.vx = 0
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.vx += RUN_SPEED
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.vx -= RUN_SPEED
#if keys[pygame.K_UP] or keys[pygame.K_SPACE]:
# self.jump()
self.rect.x += self.vx
self.check_platforms("x")
self.rect.y += self.vy
self.check_platforms("y")
def check_platforms(self, direction):
hits = pygame.sprite.spritecollide(self, platforms, False)
if hits and direction == "y":
if self.vy > 0:
self.rect.bottom = hits[0].rect.top
elif self.vy < 0:
self.rect.top = hits[0].rect.bottom
self.vy = 0
if hits and direction == "x":
if self.vx > 0:
self.rect.right = hits[0].rect.left
elif self.vx < 0:
self.rect.left = hits[0].rect.right
self.vx = 0
def jump(self):
self.rect.y += 1
hits = pygame.sprite.spritecollide(self, platforms, False)
self.rect.y -= 1
if hits and self.vy >= 0:
self.vy = JUMP_SPEED
class Camera:
def __init__(self, width, height):
self.rect = pygame.Rect(0, 0, width, height)
self.width = width
self.height = height
def update(self, target):
x = -target.rect.centerx + int(WIDTH/2)
y = -target.rect.centery + int(HEIGHT/2)
x = min(0, x)
y = min(0, y)
x = max(WIDTH - self.width, x)
y = max(HEIGHT - self.height, y)
self.rect = pygame.Rect(x, y, self.width, self.height)
def apply(self, object):
return object.rect.move(self.rect.topleft)
all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
coins = pygame.sprite.Group()
mobs = pygame.sprite.Group()
player = Player()
map_data = []
with open("map.txt", "r") as f:
for line in f:
map_data.append(line.strip())
for row, items in enumerate(map_data):
for col, tile in enumerate(items):
if tile == "#":
Platform(col * CELLSIZE, row * CELLSIZE)
if tile == "c":
Coin(col *CELLSIZE, row * CELLSIZE)
if tile == "m":
Mob(col *CELLSIZE, row * CELLSIZE)
camera = Camera(len(map_data[0]) * CELLSIZE,
len(map_data) * CELLSIZE)
running = True
while running:
clock.tick(FPS)
# events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player.jump()
# updates
all_sprites.update()
camera.update(player)
pygame.sprite.spritecollide(player, coins, True)
# mob collisions
hits = pygame.sprite.spritecollide(player, mobs, False)
for hit in hits:
if player.rect.bottom < hit.rect.centery:
hit.kill()
player.vy = JUMP_SPEED / 2
else:
running = False
# draw
screen.fill( (40, 40, 40) )
#all_sprites.draw(screen)
for sprite in all_sprites:
screen.blit(sprite.image, camera.apply(sprite))
pygame.display.flip() # DO THIS LAST!
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment