Skip to content

Instantly share code, notes, and snippets.

@eddison12345
Created June 19, 2018 01:00
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 eddison12345/0c55cc5e0af93854582f1c972b55fc31 to your computer and use it in GitHub Desktop.
Save eddison12345/0c55cc5e0af93854582f1c972b55fc31 to your computer and use it in GitHub Desktop.
import pygame
#########################
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = ( 0,255, 0)
BLUE = ( 0, 0,255)
ORANGE = (255,127, 0) #THE COLORS
CYAN = ( 0,183,235)
MAGENTA = (255, 0,255)
YELLOW = (255,255, 0)
WHITE = (255,255,255)
#########################
clock=pygame.time.Clock()
WIDTH = 800
HEIGHT = 600
GROUND = HEIGHT #THE SETTINGS
RUN_SPEED = 10
GRAVITY = 2
clock=pygame.time.Clock()
JUMP_SPEED=-30
FPS = 30
class SpriteSheet():
def __init__(self):
self.spritesheet=pygame.image.load("spritesheet_jumper.png").convert()
def get_image(self, x, y, width, height):
image=pygame.Surface(width,height)
image.blit(self.spritesheet,(0,0)(x,y,width,height))
return image
class Platforms(pygame.sprite.Sprite): # THIS CLASS CONTAINS EVERYTHING TO DO WITH STILL PLATFORMS. THIS IS WHERE EVERYTHING REQUIRED TO DRAW THE PLATFORM IS SET UP
def __init__(self,X,Y,W,H,):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((W,H))
self.image.fill(BLUE)
self.rect=self.image.get_rect()
self.X=X
self.Y=Y
self.W=W
self.H=H
self.WHITE=WHITE
def draw(self,screen):
self.rect=pygame.Rect(self.X,self.Y,self.H,self.W)
pygame.draw.rect(screen,self.WHITE,self.rect) #DRAWING THE PLATFORMS
class Moving_PlatformRight(pygame.sprite.Sprite):
def __init__(self,X,Y,W,H,):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((W,H))
self.image.fill(BLUE)
self.rect=self.image.get_rect()
self.X=X
self.Y=Y
self.W=W
self.H=H
self.WHITE=WHITE
self.MOVE_SPEED=4
self.WIDTH=WIDTH
#other.otherX=otherX
def draw(self,screen):
self.rect=pygame.Rect(self.X,self.Y,self.H,self.W)
self.X +=self.MOVE_SPEED
# otherX+=MOVE_SPEED
if self.X>self.WIDTH:
self.X=0
#Allows player to come out of either side of screen
if self.X<0:
self.X=800
pygame.draw.rect(screen,self.WHITE,self.rect)
class Moving_PlatformLeft(pygame.sprite.Sprite):
def __init__(self,X,Y,W,H,):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((W,H))
self.image.fill(BLUE)
self.rect=self.image.get_rect()
self.X=X
self.Y=Y
self.W=W
self.H=H
self.WHITE=WHITE
self.MOVE_SPEED=4
self.WIDTH=WIDTH
def draw(self,screen):
self.rect=pygame.Rect(self.X,self.Y,self.H,self.W)
self.X -=self.MOVE_SPEED
if self.X>self.WIDTH:
self.X=0
#Allows player to come out of either side of screen
if self.X<0:
self.X=800
pygame.draw.rect(screen,self.WHITE,self.rect)
class Player(pygame.sprite.Sprite,): #PLAYER CLASS, HAS EVERYTHING RELATED TO PLAYER U CONTROL AS WELL AS COLLISIONS
def __init__(self,X,Y,W,H):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((W,H))
self.image.fill(RED)
self.rect=self.image.get_rect()
self.X=X
self.Y=Y
self.W=W
self.H=H
self.rect=pygame.Rect(self.X,self.Y,self.H,self.W)
self.objectVy=0
self.RUN_SPEED= 10
self.JUMP_SPEED=5
self.GRAVITY=2
self.GROUND=600
self.RED=RED
def draw(self,screen):
pygame.draw.rect(screen, self.RED, self.rect) #DRAWING THE PLAYER ON SCREEN
def move_right(self): #MOVING THE PLAYER RIGHT
self.X +=self.RUN_SPEED
self.rect=pygame.Rect(self.X,self.Y,self.H,self.W)
def move_left(self): #MOVING THE PLAYER LEFT
self.X-=self.RUN_SPEED
self.rect=pygame.Rect(self.X,self.Y,self.H,self.W)
def gravity(self): #THIS METHOD HAS EVERYTHING RELATED TO GRAVITY, IT MAKES THE PLAYER FALL DOWNWARDS IF PLAYER IS NOT ON A PLATFORM
self.objectVy = self.objectVy + self.GRAVITY
self.Y = self.Y + self.objectVy
if self.Y+self.H >= self.GROUND:
self.Y = self.GROUND - self.H
self.objectVy = 0
self.rect=pygame.Rect(self.X,self.Y,self.H,self.W)
def move_jump(self):
#THIS METHOD MAKES THE PLAYER JUMP, IT MOVES THE OBJECT IN A VERTICAL DISTANCE
self.Y = self.Y +JUMP_SPEED
self.rect=pygame.Rect(self.X,self.Y,self.H,self.W)
import pygame
from classes import *
import random
pygame.init()
WIDTH = 800
HEIGHT = 600
screen=pygame.display.set_mode((WIDTH,HEIGHT))
BLACK = ( 0, 0, 0)
WHITE = (255,255,255)
RED = (255, 0, 0)
OUTLINE=0
GROUND = HEIGHT
RUN_SPEED = 10
GRAVITY = 2
clock=pygame.time.Clock()
#################################################
galaxy1 = pygame.image.load("galaxy.bmp")
galaxy1H = 1024
galaxy1X = 0
galaxy1Y = 0
galaxy2 = pygame.image.load("galaxy_flipped.bmp") #PICTURES AND PIC SETTINGS
galaxy2H = 1024
galaxy2X = 0
galaxy2Y = -1024
galaxySpeed = 10
#################################################
#########################
platform_list = []
platform_width = 90 #Platform variables
platform_height = 20
platform_difference = 80 #Height between each new platform
#########################
######################### SOUND EFFECTS AND MUSIC
jumpSound=pygame.mixer.Sound('jump2.ogg')
jumpSound.set_volume(12.0)
backgroundMusic=pygame.mixer.music.load("music.ogg") #music
########################
player1=Player(800,600,20,20)
#########################
for i in range(7):
platform_list.append(Platforms(random.randint(0, WIDTH), i * platform_difference, platform_height, platform_width)) #ADDING THE PLATFORMS TO THE LIST
for i in range(3):
platform_list.append(Moving_PlatformRight(random.randint(0, WIDTH), i * platform_difference, platform_height, platform_width))
for i in range(3):
platform_list.append(Moving_PlatformLeft(random.randint(0, WIDTH), i * platform_difference, platform_height, platform_width))
##########################
smallfont=pygame.font.SysFont("comicsansms", 25)
medfont=pygame.font.SysFont("comicsansms", 50) #DEFINING THE FONTS
largefont=pygame.font.SysFont("comicsansms", 60)
def text_objects(text,font,size): #function that contains all the diffrent fonts you can use
if size=="small":
textSurface=smallfont.render(text,True,BLACK)
elif size=="medium":
textSurface=medfont.render(text,True,BLACK) #Rendering the fonts
if size=="large":
textSurface=largefont.render(text,True,BLACK)
return textSurface,textSurface.get_rect()
def msg_screen(msg,color, y_displace=0, size = "small"): # function that puts msg on screen
textSurf, textRect = text_objects(msg,color, size)
textRect.center = (WIDTH / 2), (HEIGHT / 2)+y_displace #placing the text
screen.blit(textSurf, textRect)
def intro(): #intro screen
intro=True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() #GETTING ALL THE EVENTS FOR PRESSING BUTTONS
if event.type==pygame.KEYDOWN:
if event.key == pygame.K_p:
intro=False
if event.type==pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
screen.fill(WHITE)
msg_screen("Welcome to Platform Jump",GREEN,-100,"large")
msg_screen("Jump on the platforms to get higher",BLACK,-30)
msg_screen("If you fall off you lose!",BLACK,10)
msg_screen("Avoid the monsters",BLACK,50) #INSTRUCTIONS
msg_screen("Press p to play or Q to quit.",BLACK,180)
pygame.display.update()
#---------------------------------------#
# function that redraws all objects #5
#---------------------------------------#
def redrawGameWindow():
screen.fill(BLACK)
screen.blit(galaxy1, (galaxy1X,galaxy1Y))
screen.blit(galaxy2, (galaxy2X,galaxy2Y))
scoredisplay=smallfont.render("Score: "+str(score),1,WHITE)
StartPlatform.draw(screen)
for platform in platform_list: #DRAWING THE PLATFORMS
platform.draw(screen)
screen.blit(scoredisplay,(0,0))
player1.draw(screen) #DRAWING THE PLAYER
pygame.display.update()
#---------------------------------------#
# main program starts here #
#---------------------------------------#
#---------------------------------------#
####################################
all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
all_sprites.add(player1) #Sprites groups
all_sprites.add(platform_list)
platforms.add(platform_list)
####################################
score=0
pygame.mixer.music.play(loops=-1)
player1=Player(30,30,30,30) #PLAYER INSTANCE
StartPlatform=Platforms(30,30,20,80)
platform_list.append(StartPlatform)
intro() #CALLING INTRO SCREEN
inPlay = True
while inPlay:
if player1.Y<=HEIGHT/2:
for i in platform_list: #CREATING A CAMERA EFFECT BY MOVING THE PLATFORMS DOWN WHEN PLAYER REACHES TOP QUARTER OF SCREEN
i.Y+=10
if i.Y>=HEIGHT:
platform_list.remove(i) #DELETING THE PLATFORMS THAT ARE UNDER THE SCREEN
score+=10
while (len(platform_list)) <12:
for i in range(2,4):
platform_list.append(Platforms(random.randint(0, WIDTH),(-1) *i * platform_difference, platform_height, platform_width)) #ADDING THE PLATFORMS TO THE LIST IF THERE ARE LESS THAN 12 IN THE LIST
for i in range(2):
platform_list.append(Moving_PlatformRight(random.randint(0, WIDTH), (-1)*i * platform_difference, platform_height, platform_width))
for i in range(1,3):
platform_list.append(Moving_PlatformLeft(random.randint(0, WIDTH), (-1)*i * platform_difference, platform_height, platform_width))
clock.tick(FPS)
for event in pygame.event.get(): #DEFINIG WHAT ALL THE KEYS DO
if event.type==pygame.QUIT:
inPlay=False
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_UP and player1.objectVy==0:
player1.objectVy = JUMP_SPEED
jumpSound.play()
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]: #MOVES PLAYER LEFT
player1.move_left()
if keys[pygame.K_RIGHT]:
#MOVES PLAYER RIGHT
player1.move_right()
galaxy1Y = galaxy1Y + galaxySpeed
if galaxy1Y + galaxySpeed > galaxy1H:
galaxy1Y = -galaxy2H
galaxy2Y = galaxy2Y + galaxySpeed
if galaxy2Y + galaxySpeed > galaxy2H:
galaxy2Y = -galaxy1H
player1.gravity() #calling the gravity function
redrawGameWindow() #calling redrawGameWindow
pygame.display.update()
if player1.X>WIDTH:
player1.X=0
#Allows player to come out of either side of screen
if player1.X<0:
player1.X=800
hits = pygame.sprite.spritecollide(player1,platform_list, False) #Using py games built in collisions detection
if hits:
player1.Y= hits[0].rect.top-30
player1.objectVy=0
#if player1.rect.bottom>=HEIGHT: #WHEN PLAYER TOUCHES GROUND GAME ENDs
#inPlay=False
pygame.time.delay(1)
pygame.display.update()
#---------------------------------------#
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment