Skip to content

Instantly share code, notes, and snippets.

@daCFniel
Created May 25, 2020 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daCFniel/cbf0a24382997240cedd0baaad5e11be to your computer and use it in GitHub Desktop.
Save daCFniel/cbf0a24382997240cedd0baaad5e11be to your computer and use it in GitHub Desktop.
easycoding.pl [pygame] - jump
import os
import pygame
pygame.init()
# super class
class Character:
# constructor
def __init__(self, img, x, y, x_change, y_change):
self.img = img
self.x = x
self.y = y
self.x_change = x_change
self.y_change = y_change
# methods
def draw(self):
screen.blit(self.img, (self.x, self.y))
# sub class
class Player(Character):
# constructor
def __init__(self, img, x, y, x_change, y_change):
Character.__init__(self, img, x, y, x_change, y_change)
self.is_jumping = False
# methods
def check_borders(self): # prevent from going off the screen
if self.x > 367: # check right border
self.x = 367
elif self.x < 0: # check left border
self.x = 0
def jump(self):
self.img = player_jumping
if self.y_change >= -0.5:
self.y -= self.y_change
self.y_change -= 0.0009
if self.x > 360: # check right border for jump animation
self.x = 360
else:
self.is_jumping = False
self.y_change = 0.5
self.img = player_standing
# sub class
class Bullet(Character):
# constructor
def __init__(self, img, x, y, x_change, y_change):
Character.__init__(self, img, x, y, x_change, y_change)
# CONSTANT variables
WIDTH = 400
HEIGHT = 500
RED = (255, 0, 0)
# website gameart2d to download cool graphics
# how to make path work on every OS (linux/win/ios)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("CodeLab")
# images
file_name = os.path.join('img', 'background.jpg')
background = pygame.image.load(file_name).convert()
file_name = os.path.join('img', 'jump.png')
player_jumping = pygame.image.load(file_name).convert_alpha()
file_name = os.path.join('img', 'player.png')
player_standing = pygame.image.load(file_name).convert_alpha()
# bullets
first_frame = 0
last_frame = 4
bullets = []
for i in range(last_frame + 1):
file_name = os.path.join('img', f'bullet_00{i}.png')
bullets.append(Bullet(pygame.image.load(file_name).convert_alpha(), 0, 0, 0.5, 0.5))
# player
file_name = os.path.join('img', 'player.png')
player = Player(pygame.image.load(file_name).convert_alpha(), 190, 340, 0.3, 0.5)
frame_counter = 0
active_frame = 0
bullet = bullets[active_frame].img
def update_background():
screen.blit(background, (0, 0))
run = True
while run:
pygame.time.delay(1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if not player.is_jumping:
player.is_jumping = True
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
player.x += player.x_change
if keys[pygame.K_LEFT]:
player.x -= player.x_change
if keys[pygame.K_c]: # animate the bullet
frame_counter = frame_counter + 1
if frame_counter % 60 == 0:
active_frame = active_frame + 1
if active_frame > last_frame:
active_frame = first_frame
bullet = bullets[active_frame].img
# player jumping
if player.is_jumping:
player.jump()
update_background()
player.draw()
player.check_borders()
screen.blit(bullet, (0, -10)) # draw bullet
pygame.display.update()
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment