Skip to content

Instantly share code, notes, and snippets.

@Mrmeguyme
Created November 30, 2016 22:57
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 Mrmeguyme/ce1a844af21695d1b853ef88fe8de5aa to your computer and use it in GitHub Desktop.
Save Mrmeguyme/ce1a844af21695d1b853ef88fe8de5aa to your computer and use it in GitHub Desktop.
import pygame, time
from pygame.locals import *
from sys import exit
class char (object):
def __init__ (self, img, x, y, screen):
self.img = pygame.image.load(img)
self.x = float(x)
self.y = float(y)
screen.blit(self.img, (self.x, self.y))
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
nothing = pygame.image.load('char.png')
ground = pygame.image.load('ground.png')
background = pygame.image.load('background.png')
groundHeight = ground.get_height()
backgroundY = SCREEN_HEIGHT - groundHeight - background.get_height()
player = char('char.png', 30, SCREEN_HEIGHT - nothing.get_height() - groundHeight, screen)
blue = (70, 70, 255)
notjump = 0
trail = 0
progRunning = True
jump = False
clock = pygame.time.Clock()
fps = 30
screen.blit(background, (0, backgroundY))
screen.blit(ground, (0, SCREEN_HEIGHT - groundHeight))
while (progRunning):
#Checking if exit is required
for event in pygame.event.get():
if (event.type == QUIT):
pygame.quit()
exit(0)
if not hasattr(event, 'key'): continue
if (event.key == K_ESCAPE):
pygame.quit()
exit(0)
#Player Movement
if (pygame.key.get_pressed()[pygame.K_a] != 0):
player.x -= 2
if (pygame.key.get_pressed()[pygame.K_d] != 0):
player.x += 2
if (pygame.key.get_pressed()[pygame.K_SPACE] != 0 or pygame.key.get_pressed()[pygame.K_w] != 0) & (jump != 50) & (notjump > 50):
jump += 1
player.y = player.y / 1.015
if (jump == 50):
notjump = 0
#Making sure the player can't leave the screen
if (player.x < 0):
player.x = 0
if (player.x + player.img.get_width() > SCREEN_WIDTH):
player.x = SCREEN_WIDTH - player.img.get_width()
if (player.y < 0):
player.y = 0
if (player.y + player.img.get_height() >= SCREEN_HEIGHT - groundHeight):
player.y = SCREEN_HEIGHT - player.img.get_height() - groundHeight
jump = 0
notjump += 1
else:
player.y = player.y * 1.006
screen.blit(background, (0, backgroundY))
screen.blit(player.img, (player.x, player.y))
pygame.display.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment