Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # GAME STARTED 11.11.13
- import pygame, pygame._view
- from pygame.locals import *
- import sys, os
- import math, time, random, copy, cPickle
- wincon=pygame.image.load("grfx\dg.bmp")
- pygame.display.set_icon(wincon)
- pygame.display.set_caption('SURVIVOR-EMorphus Productions 2013')
- #initialize pygame, load and start music while the game loads
- clock = pygame.time.Clock()
- pygame.mixer.pre_init(44100, 16, 2, 4096) # setup mixer to avoid sound lag
- pygame.init()
- pygame.mixer.set_num_channels(64)
- ##startmusic="resources\snd\SGOpeningtheme.ogg"
- ##pygame.mixer.set_num_channels(64)
- ##pygame.mixer.music.load(startmusic)
- ##pygame.mixer.music.set_volume(0.3)
- ##pygame.mixer.music.play(-1)
- if sys.platform == 'win32' or sys.platform == 'win64':
- #os.environ['SDL_VIDEO_CENTERED'] = '2'# center of screen
- os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (10,30)#top left corner
- # getlist of available screen resolutions first is highest resolution
- Dlist = pygame.display.list_modes(0,FULLSCREEN)
- SCREENW = Dlist[0][0]-24
- SCREENH = Dlist[0][1]-50
- SCREENW = 800
- SCREENH = 760
- SCREENSIZE = (SCREENW,SCREENH)
- SCREENMODE = 0
- GRAVITY=2.81
- screen = pygame.display.set_mode(SCREENSIZE,SCREENMODE,0)
- background = pygame.Surface(screen.get_size())
- font = pygame.font.Font("data\IgniteTheLight.ttf", 22)
- text = font.render("Game Loading. Please wait....", 2, (150,150,250))
- a=text.get_rect()
- screen.blit(text,[SCREENW/2-text.get_width()/2,SCREENH/2-text.get_height()])
- pygame.display.update(a)
- score=[0,0,0,0,0,0,0] #mil,hunthous, tenthous,thous,huns,tens,ones
- scorestr= "".join(str(i) for i in score)
- realscore=int(scorestr)
- ##f=open('resources\data\scores.sg','rb')# Get highscores from disk n close file
- ##highscores=cPickle.load(f)
- ##f.close()
- ##
- ##f=open('resources\data\saves.sg','rb')# Get save list from disk n close file
- ##savelist=cPickle.load(f)
- ##f.close()
- white = (255,255,255)
- blue = ( 0, 0,255)
- green = ( 0,255, 0)
- red = (255, 0, 0)
- purple = (255, 0,255)
- yellow = (220,220, 0)
- thk=10 # wall thickness
- confirm=['Do you really want to quit?','Y to Quit',\
- 'any other key to get back to the game']
- #------------------------------------------------------------------------------#
- #----------------------------T H E F U N C T I O N S--------------------------#
- def write(msg,fontsize):
- mafont = pygame.font.Font("data\IgniteTheLight.ttf", fontsize)
- matext = mafont.render(msg, True, yellow)
- matext = matext.convert_alpha()
- return matext
- def update_all():
- global thk
- screen.fill((0,0,0))
- ## allsprites.clear(screen,background)
- allsprites.update()
- allsprites.draw(screen)
- for i in xrange (Hero.lives):
- screen.blit(Herolives,(SCREENW-thk-(25*(i+1)),thk))
- if Hero.lives>0 and Hero.status=="A":
- ## Herosprite.clear(screen,background)
- Hero.update()
- Herosprite.draw(screen)
- ## b=Hero.rect.inflate(0,0)
- ## pygame.draw.rect(screen, (255,0,255), b,2)
- #---------------------VARIABLE CHECK-------------------
- matext=write(' FLOOR:'+str(Hero.floor)+' FALLCOUNTER:'+str(Hero.fallcounter),24)
- screen.blit(matext,[SCREENW-matext.get_width(),SCREENH-matext.get_height()])
- matext=write(' MODE:'+str(Hero.mode)+' GROUND:'+str(Hero.ground)+\
- ' STOP:'+str(Hero.stop),24)
- screen.blit(matext,[SCREENW-matext.get_width(),\
- SCREENH-matext.get_height()*2])
- ##------------------------------------------------------
- matext =write(scorestr,24) # print the score
- screen.blit(matext, [scoretext.get_width()+thk,thk])
- screen.blit(scoretext, [thk,thk]) # print score text
- screen.blit(health, [SCREENW/2-health.get_width(),thk])# print helath text
- pygame.draw.rect(screen,yellow,[(SCREENW/2+health.get_width()/4),\
- (thk*2),Hero.life,5])
- pygame.display.update()
- def update_screen():
- pygame.display.flip()
- screen.fill((0,0,0))
- def game_over():
- pygame.mixer.music.stop()
- pygame.quit()
- sys.exit()
- def really_quit():
- gv.quitgame=True
- while gv.quitgame==True:
- screen.fill((0,0,0))
- font = pygame.font.Font("data\IgniteTheLight.ttf", 25)
- y=gv.SCREENH/2
- for i in xrange(len(confirm)):
- text=font.render(str(confirm[i]),2,[10,10,250])
- screen.blit(text,(gv.SCREENW/2-text.get_width()/2,y))
- y+=text.get_height()
- for event in pygame.event.get():
- if event.type==KEYDOWN:
- if event.key==K_y:
- game_over()
- elif event.key!=K_y:
- gv.quitgame=False
- pygame.display.flip()
- def add_to_imagedict(w,x,y):
- w[x]=y
- return w
- class Walls_and_platforms(pygame.sprite.Sprite):
- def __init__(self,x,y,width,height,color):
- pygame.sprite.Sprite.__init__(self)
- self.image=pygame.Surface([width,height])
- self.image.fill(color)
- self.rect=self.image.get_rect()
- self.rect.x=x
- self.rect.y=y
- self.width=width
- self.height=height
- self.speedx=0
- class Hero_create(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.image=Heroimages["UR"]
- self.status="A" #A-Alive D-Dead
- self.rect=(self.image.get_rect()).inflate(-10,0)
- self.rect.x=thk
- self.rect.y=SCREENH -self.rect.height - thk
- self.ssx=0
- self.ssy=0
- self.speedx=0
- self.speedy=0
- self.jump=False
- self.jumplimit=self.rect.y-0 # height of jump
- self.fallcounter=0
- self.floor=0 # the place from where falls
- self.life=100
- self.lives=5
- # WR - Walk Right Normal WL - Walk Left Normal
- # SR - Stand Right Normal SL - Stand Left Normal
- # UR - Jump Right Up UL - Jump Left Up
- # DR - Jump Right Down DL - Jump Left Down
- self.mode="UR"
- self.ground=False# if feet on ground True else False
- self.stop=False # stop when against a wall
- self.noframes=7
- self.frame=0 # when walking
- self.increment=0
- def move(self,speedx,speedy):
- self.rect.x+=speedx
- # DIAGONAL movement LEFT and RIGHT Collisions
- hit_list=pygame.sprite.spritecollide(self,allsprites,False)
- for platform in hit_list:
- if self.speedx>0:
- self.rect.right=platform.rect.left
- self.stop=True
- ## self.mode="SR"
- else:
- self.rect.left=platform.rect.right
- self.stop=True
- ## self.mode="SL"
- self.rect.y+=speedy
- # VERTICAL movement BOTTOM and TOP Collisions
- hit_list=pygame.sprite.spritecollide(self,allsprites,False)
- # befor vertical collision check make ground False
- #If collision is True ground=True
- self.ground=False
- for platform in hit_list:
- if self.speedy>0:
- self.rect.bottom=platform.rect.top # feet on ground
- self.ground=True
- self.floor=self.rect.bottom
- self.jump=False
- self.jumplimit=self.rect.y-110# height of jump
- if self.mode[0]<>"F":
- if self.mode[1]=="R" and speedx==0:
- self.mode="SR"
- elif self.mode[1]=="R" and speedx>0:
- self.mode="WR"
- elif self.mode[1]=="L" and speedx==0:
- self.mode="SL"
- elif self.mode[1]=="L" and speedx<0:
- self.mode="WL"
- if self.mode[0]=="F" and self.mode[2]=="4":
- Hero.mode="F"+Hero.mode[1]+"5"
- print 'here'
- Hero.life-=100
- self.lives-=1
- elif self.mode[0]=="F" and self.mode[2]<>"5":
- Hero.mode="S"+Hero.mode[1]
- else:
- self.rect.top=platform.rect.bottom # head hit top platform
- self.ground=False
- if self.mode=="UR":
- self.mode="DR"
- elif self.mode=="UL":
- self.mode="DL"
- def update(self):
- if self.mode:
- self.image=Heroimages[self.mode] # choose image to match mode
- if self.mode=="WR":
- self.image=WalkRframes[self.frame]
- if self.increment>3:
- self.frame+=1
- self.increment=0
- self.increment+=1
- if self.frame>=7:
- self.frame=0
- elif self.mode=="WL":
- self.image=WalkLframes[self.frame]
- if self.increment>3:
- self.frame+=1
- self.increment=0
- self.increment+=1
- if self.frame>=7:
- self.frame=0
- class Game_variables():
- def __init__(self):
- self.SCREENW=SCREENW
- self.SCREENH=SCREENH
- self.gamestate="Level 1" #P-passive A-active start initial
- self.gamepause=False
- self.quitgame=False
- self.gamespeed=50
- self.framerate=60
- self.glt_sec=clock.tick(self.framerate)/1000 # game looptime in seconds
- gv=Game_variables() # initialize the game variables
- Heroimages={}
- WalkLframes=[] # Left walk animation frames
- WalkRframes=[] # Right walk animation frames
- #------------------------------------------------------------------------------#
- # LOAD ALL THE GRAPHICS and convert to scale
- #------------------------------------------------------------------------------#
- Heroimages["SL"]=pygame.image.load("grfx\psl.png").convert_alpha() # stand left
- Heroimages["SR"]=pygame.image.load("grfx\psr.png").convert_alpha() # stand right
- Heroimages["UL"]=pygame.image.load("grfx\pjl.png").convert_alpha() # jump left
- Heroimages["DL"]=pygame.image.load("grfx\pjl.png").convert_alpha() # jump left
- Heroimages["UR"]=pygame.image.load("grfx\pjr.png").convert_alpha() # jump right
- Heroimages["DR"]=pygame.image.load("grfx\pjr.png").convert_alpha() # jump right
- Heroimages["FL1"]=pygame.image.load("grfx\pf01l.png").convert_alpha()#fall left1
- Heroimages["FL2"]=pygame.image.load("grfx\pf02l.png").convert_alpha()#fall left2
- Heroimages["FL3"]=pygame.image.load("grfx\pf03l.png").convert_alpha()#fall left3
- Heroimages["FL4"]=pygame.image.load("grfx\pf04l.png").convert_alpha()#fall lef4
- Heroimages["FL5"]=pygame.image.load("grfx\pf05l.png").convert_alpha()#fall left5
- Heroimages["FR1"]=pygame.image.load("grfx\pf01r.png").convert_alpha()#f right1
- Heroimages["FR2"]=pygame.image.load("grfx\pf02r.png").convert_alpha()#f right2
- Heroimages["FR3"]=pygame.image.load("grfx\pf03r.png").convert_alpha()#f right3
- Heroimages["FR4"]=pygame.image.load("grfx\pf04r.png").convert_alpha()#f right4
- Heroimages["FR5"]=pygame.image.load("grfx\pf05r.png").convert_alpha()#f right5
- Herolives=pygame.image.load("grfx\hl.png").convert_alpha() # no of lives display icon
- scoretext = write("Score: ", 22)
- health=write(("Health:"),20)
- # ROOM 1 Walls
- room1walls=[[0,0,SCREENW,thk,blue],[0,0,thk,SCREENH,blue],
- [SCREENW-thk,0,thk,SCREENH,blue],[0,SCREENH-thk,SCREENW,thk,blue]]
- # ROOM 1 Platforms
- r1platforms=[[100,650,80,thk,blue],[200,550,80,thk,blue],[300,450,80,thk,blue],
- [400,350,80,thk,blue],[300,250,80,thk,blue],[200,150,80,thk,blue],
- [50,100,80,thk,blue],[400,550,80,thk,blue],[500,236,80,thk,blue],
- [650,150,80,thk,blue],[600,550,80,thk,blue],[100,450,80,thk,blue],
- [0,330,80,thk,blue],[200,340,80,thk,blue],[720,450,80,thk,blue],
- [550,350,80,thk,blue],[470,150,80,thk,blue]]
- Herosprite = pygame.sprite.Group()
- wallsprites=pygame.sprite.Group()
- platformsprites=pygame.sprite.Group()
- allsprites=pygame.sprite.Group()
- # ---------------create the walk cycle animation frame lists--------------
- for x in xrange(7):
- name="grfx\pwl0"+str(x)+".png"
- frame=pygame.image.load(name).convert_alpha()
- WalkLframes.append(frame)
- for x in xrange(7):
- name="grfx\pwr0"+str(x)+".png"
- frame=frame=pygame.image.load(name).convert_alpha()
- WalkRframes.append(frame)
- Heroimages["WL"]=WalkLframes
- Heroimages["WR"]=WalkRframes
- for item in room1walls:
- wall=Walls_and_platforms(item[0],item[1],item[2],item[3],item[4])
- allsprites.add(wall)
- for item in r1platforms:
- platform=Walls_and_platforms(item[0],item[1],item[2],item[3],item[4])
- allsprites.add(platform)
- #------------------------------------------------------------------------
- # status, x,scale, walkleft animframes, walkright animframes
- Hero=Hero_create()
- Herosprite.add(Hero)
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # MAIN GAME LOOP
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- while True:
- Hero.stop=False
- gv.glt_sec=clock.tick(gv.framerate)/1000.0# game loop time in seconds
- constant=round(gv.gamespeed*gv.glt_sec,2) # round to 2 decimals
- scorestr= "".join(str(i) for i in score)
- realscore=int(scorestr)
- pygame.event.pump()
- keys = pygame.key.get_pressed()
- if keys[K_ESCAPE]:
- ## really_quit()
- game_over()
- # WR - WALK RIGHT WL - WALK LEFT SR - STAND RIGHT SL - STAND LEFT
- ## if Hero.stop==False:
- if keys[K_RIGHT] and Hero.ground==True: # WALKING RIGHT
- Hero.speedx=2*constant
- Hero.mode="WR"
- # Jumping up and moving right
- elif keys[K_RIGHT] and Hero.jump==True and Hero.ground==False\
- and Hero.mode[1]=="R":
- Hero.speedx=3*constant
- elif keys[K_LEFT] and Hero.ground==True: # WALKING LEFT
- Hero.speedx=-2*constant
- Hero.mode="WL"
- # Jumping up and moving left
- elif keys[K_LEFT] and Hero.jump==True and Hero.ground==False\
- and Hero.mode[1]=="L":
- Hero.speedx=-3*constant
- else:
- Hero.speedx=0
- for event in pygame.event.get():
- if event.type==KEYDOWN:
- #-----------GAME PAUSE
- if event.key==K_p:
- gv.gamepause=not gv.gamepause
- if gv.gamepause==True:
- print 'pause simulation ON',gv.gamepause
- else:
- print 'pause simulation OFF',gv.gamepause
- #-----------TOGGLE FULLSCREEN ON/OFF
- elif event.key==K_f and SCREENMODE==0:
- SCREENMODE=FULLSCREEN
- screen = pygame.display.set_mode((SCREENSIZE),SCREENMODE,0)
- elif event.key==K_f and SCREENMODE==FULLSCREEN:
- SCREENMODE=0
- screen = pygame.display.set_mode((SCREENSIZE),SCREENMODE,0)
- #-----------JUMP UP, RIGHT OR LEFT
- if Hero.ground==True:
- if event.key==K_UP and Hero.mode[1]=="R": # UR - UP RIGHT
- Hero.mode="UR" # RIGHT UP
- Hero.ground=False
- Hero.jump=True
- elif event.key==K_UP and Hero.mode[1]=="L": # UL - UP LEFT
- Hero.mode="UL" # LEFT UP
- Hero.ground=False
- Hero.jump=True
- #------------------ The Jump and Fall automated routine ---------------------
- if Hero.mode=="UR" and Hero.rect.y>=Hero.jumplimit:
- Hero.speedy=-4*constant # UP RIGHT
- if Hero.mode=="UR" and Hero.rect.y<=Hero.jumplimit:
- Hero.fallcounter=0
- Hero.mode="DR" # DOWN RIGHT
- Hero.floor=Hero.rect.bottom
- if Hero.mode=="UL" and Hero.rect.y>=Hero.jumplimit:
- Hero.speedy=-4*constant# UP LEFT
- if Hero.mode=="UL" and Hero.rect.y<=Hero.jumplimit:
- Hero.fallcounter=0
- Hero.mode="DL" # DOWN LEFT
- Hero.floor= Hero.rect.bottom
- #-----------------------------------------------------------------------------
- # No ground.. Hero FALLS!
- if Hero.ground==False and Hero.mode[0]<>"U":
- Hero.speedy+=1+GRAVITY*constant
- Hero.fallcounter=(Hero.rect.bottom-Hero.floor)
- if Hero.speedy>10:
- Hero.speedy=10
- if Hero.fallcounter>110 and Hero.fallcounter<300:# lowest platform
- Hero.mode="F"+Hero.mode[1]+"1"
- if Hero.fallcounter>301 and Hero.fallcounter<400:
- Hero.mode="F"+Hero.mode[1]+"2"
- if Hero.fallcounter>401 and Hero.fallcounter<500:
- Hero.mode="F"+Hero.mode[1]+"3"
- if Hero.fallcounter>501:
- Hero.mode="F"+Hero.mode[1]+"4"
- Hero.life-=0
- if Hero.life<0:
- Hero.life=0
- ## print Hero.fallcounter
- if Hero.life>0:
- Hero.move(Hero.speedx,Hero.speedy)
- update_all()
- #==============================================================================#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement