Advertisement
diliupg

This is a game in progress a platform jumper

Aug 25th, 2014
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.73 KB | None | 0 0
  1. # GAME STARTED 11.11.13
  2.  
  3. import pygame, pygame._view
  4. from pygame.locals import *
  5. import sys, os
  6. import math, time, random, copy, cPickle
  7.  
  8. wincon=pygame.image.load("grfx\dg.bmp")
  9. pygame.display.set_icon(wincon)
  10. pygame.display.set_caption('SURVIVOR-EMorphus Productions 2013')
  11. #initialize pygame, load and start music while the game loads
  12. clock = pygame.time.Clock()
  13. pygame.mixer.pre_init(44100, 16, 2, 4096) # setup mixer to avoid sound lag
  14. pygame.init()
  15. pygame.mixer.set_num_channels(64)
  16. ##startmusic="resources\snd\SGOpeningtheme.ogg"
  17. ##pygame.mixer.set_num_channels(64)
  18. ##pygame.mixer.music.load(startmusic)
  19. ##pygame.mixer.music.set_volume(0.3)
  20. ##pygame.mixer.music.play(-1)
  21.  
  22. if sys.platform == 'win32' or sys.platform == 'win64':
  23.     #os.environ['SDL_VIDEO_CENTERED'] = '2'# center of screen
  24.     os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (10,30)#top left corner
  25.  
  26. # getlist of available screen resolutions first is highest resolution
  27. Dlist = pygame.display.list_modes(0,FULLSCREEN)
  28. SCREENW = Dlist[0][0]-24
  29. SCREENH = Dlist[0][1]-50
  30. SCREENW = 800
  31. SCREENH = 760
  32. SCREENSIZE = (SCREENW,SCREENH)
  33. SCREENMODE = 0
  34. GRAVITY=2.81
  35. screen = pygame.display.set_mode(SCREENSIZE,SCREENMODE,0)
  36. background = pygame.Surface(screen.get_size())
  37. font = pygame.font.Font("data\IgniteTheLight.ttf", 22)
  38. text = font.render("Game Loading. Please wait....", 2, (150,150,250))
  39. a=text.get_rect()
  40. screen.blit(text,[SCREENW/2-text.get_width()/2,SCREENH/2-text.get_height()])
  41. pygame.display.update(a)
  42.  
  43. score=[0,0,0,0,0,0,0] #mil,hunthous, tenthous,thous,huns,tens,ones
  44. scorestr= "".join(str(i) for i in score)
  45. realscore=int(scorestr)
  46.  
  47. ##f=open('resources\data\scores.sg','rb')# Get highscores from disk n close file
  48. ##highscores=cPickle.load(f)
  49. ##f.close()
  50. ##
  51. ##f=open('resources\data\saves.sg','rb')# Get save list from disk n close file
  52. ##savelist=cPickle.load(f)
  53. ##f.close()
  54.  
  55. white  = (255,255,255)
  56. blue   = (  0,  0,255)
  57. green  = (  0,255,  0)
  58. red    = (255,  0,  0)
  59. purple = (255,  0,255)
  60. yellow = (220,220,  0)
  61. thk=10 # wall thickness
  62.  
  63. confirm=['Do you really want to quit?','Y to Quit',\
  64.              'any other key to get back to the game']
  65.  
  66. #------------------------------------------------------------------------------#
  67. #----------------------------T H E  F U N C T I O N S--------------------------#
  68.  
  69. def write(msg,fontsize):
  70.     mafont = pygame.font.Font("data\IgniteTheLight.ttf", fontsize)
  71.     matext = mafont.render(msg, True, yellow)
  72.     matext = matext.convert_alpha()
  73.     return matext
  74.  
  75. def update_all():
  76.     global thk
  77.     screen.fill((0,0,0))
  78. ##    allsprites.clear(screen,background)
  79.     allsprites.update()
  80.     allsprites.draw(screen)
  81.     for i in xrange (Hero.lives):
  82.         screen.blit(Herolives,(SCREENW-thk-(25*(i+1)),thk))
  83.     if Hero.lives>0 and Hero.status=="A":
  84. ##        Herosprite.clear(screen,background)
  85.         Hero.update()
  86.         Herosprite.draw(screen)
  87. ##        b=Hero.rect.inflate(0,0)
  88. ##        pygame.draw.rect(screen, (255,0,255), b,2)
  89. #---------------------VARIABLE CHECK-------------------
  90.     matext=write(' FLOOR:'+str(Hero.floor)+' FALLCOUNTER:'+str(Hero.fallcounter),24)
  91.     screen.blit(matext,[SCREENW-matext.get_width(),SCREENH-matext.get_height()])
  92.     matext=write(' MODE:'+str(Hero.mode)+' GROUND:'+str(Hero.ground)+\
  93.                  ' STOP:'+str(Hero.stop),24)
  94.     screen.blit(matext,[SCREENW-matext.get_width(),\
  95.                         SCREENH-matext.get_height()*2])
  96. ##------------------------------------------------------
  97.     matext =write(scorestr,24) # print the score
  98.     screen.blit(matext, [scoretext.get_width()+thk,thk])
  99.     screen.blit(scoretext, [thk,thk]) # print score text
  100.     screen.blit(health, [SCREENW/2-health.get_width(),thk])# print helath text
  101.     pygame.draw.rect(screen,yellow,[(SCREENW/2+health.get_width()/4),\
  102.                                  (thk*2),Hero.life,5])
  103.     pygame.display.update()
  104.  
  105. def update_screen():
  106.     pygame.display.flip()
  107.     screen.fill((0,0,0))
  108.  
  109. def game_over():
  110.     pygame.mixer.music.stop()
  111.     pygame.quit()
  112.     sys.exit()
  113.  
  114. def really_quit():
  115.     gv.quitgame=True
  116.     while gv.quitgame==True:
  117.         screen.fill((0,0,0))
  118.         font = pygame.font.Font("data\IgniteTheLight.ttf", 25)
  119.         y=gv.SCREENH/2
  120.         for i in xrange(len(confirm)):
  121.             text=font.render(str(confirm[i]),2,[10,10,250])
  122.             screen.blit(text,(gv.SCREENW/2-text.get_width()/2,y))
  123.             y+=text.get_height()
  124.         for event in pygame.event.get():
  125.             if event.type==KEYDOWN:
  126.                 if event.key==K_y:
  127.                     game_over()
  128.  
  129.                 elif event.key!=K_y:
  130.                     gv.quitgame=False
  131.         pygame.display.flip()
  132.  
  133. def add_to_imagedict(w,x,y):
  134.     w[x]=y
  135.     return w
  136.  
  137. class Walls_and_platforms(pygame.sprite.Sprite):
  138.     def __init__(self,x,y,width,height,color):
  139.         pygame.sprite.Sprite.__init__(self)
  140.         self.image=pygame.Surface([width,height])
  141.         self.image.fill(color)
  142.         self.rect=self.image.get_rect()
  143.         self.rect.x=x
  144.         self.rect.y=y
  145.         self.width=width
  146.         self.height=height
  147.         self.speedx=0
  148.  
  149. class Hero_create(pygame.sprite.Sprite):
  150.     def __init__(self):
  151.         pygame.sprite.Sprite.__init__(self)
  152.         self.image=Heroimages["UR"]
  153.         self.status="A" #A-Alive D-Dead
  154.         self.rect=(self.image.get_rect()).inflate(-10,0)
  155.  
  156.         self.rect.x=thk
  157.         self.rect.y=SCREENH -self.rect.height - thk
  158.         self.ssx=0
  159.         self.ssy=0
  160.         self.speedx=0
  161.         self.speedy=0
  162.         self.jump=False
  163.         self.jumplimit=self.rect.y-0 # height of jump
  164.         self.fallcounter=0
  165.         self.floor=0 # the place from where falls
  166.         self.life=100
  167.         self.lives=5
  168.         # WR - Walk Right Normal    WL - Walk Left Normal
  169.         # SR - Stand Right Normal   SL - Stand Left Normal
  170.         # UR - Jump Right Up        UL - Jump Left Up
  171.         # DR - Jump Right Down      DL - Jump Left Down
  172.         self.mode="UR"
  173.         self.ground=False# if feet on ground True else False
  174.         self.stop=False # stop when against a wall
  175.         self.noframes=7
  176.         self.frame=0 # when walking
  177.         self.increment=0
  178.  
  179.     def move(self,speedx,speedy):
  180.  
  181.         self.rect.x+=speedx
  182.         # DIAGONAL movement LEFT and RIGHT Collisions
  183.         hit_list=pygame.sprite.spritecollide(self,allsprites,False)
  184.  
  185.         for platform in hit_list:
  186.             if self.speedx>0:
  187.                 self.rect.right=platform.rect.left
  188.                 self.stop=True
  189. ##                self.mode="SR"
  190.  
  191.  
  192.             else:
  193.                 self.rect.left=platform.rect.right
  194.                 self.stop=True
  195. ##                self.mode="SL"
  196.  
  197.         self.rect.y+=speedy
  198.  
  199.         # VERTICAL movement BOTTOM and TOP Collisions
  200.         hit_list=pygame.sprite.spritecollide(self,allsprites,False)
  201.  
  202.         # befor vertical collision check make ground False
  203.         #If collision is True ground=True
  204.         self.ground=False
  205.         for platform in hit_list:
  206.             if self.speedy>0:
  207.                 self.rect.bottom=platform.rect.top # feet on ground
  208.                 self.ground=True
  209.                 self.floor=self.rect.bottom
  210.                 self.jump=False
  211.                 self.jumplimit=self.rect.y-110# height of jump
  212.                 if self.mode[0]<>"F":
  213.                     if self.mode[1]=="R" and speedx==0:
  214.                         self.mode="SR"
  215.  
  216.                     elif self.mode[1]=="R" and speedx>0:
  217.                         self.mode="WR"
  218.  
  219.                     elif self.mode[1]=="L" and speedx==0:
  220.                         self.mode="SL"
  221.  
  222.                     elif self.mode[1]=="L" and speedx<0:
  223.                         self.mode="WL"
  224.                 if self.mode[0]=="F" and self.mode[2]=="4":
  225.                     Hero.mode="F"+Hero.mode[1]+"5"
  226.                     print 'here'
  227.                     Hero.life-=100
  228.                     self.lives-=1
  229.  
  230.                 elif self.mode[0]=="F" and self.mode[2]<>"5":
  231.                     Hero.mode="S"+Hero.mode[1]
  232.  
  233.             else:
  234.                 self.rect.top=platform.rect.bottom # head hit top platform
  235.                 self.ground=False
  236.  
  237.                 if self.mode=="UR":
  238.                     self.mode="DR"
  239.  
  240.                 elif self.mode=="UL":
  241.                     self.mode="DL"
  242.  
  243.     def update(self):
  244.         if self.mode:
  245.             self.image=Heroimages[self.mode] # choose image to match mode
  246.  
  247.         if  self.mode=="WR":
  248.             self.image=WalkRframes[self.frame]
  249.             if self.increment>3:
  250.                 self.frame+=1
  251.                 self.increment=0
  252.             self.increment+=1
  253.             if self.frame>=7:
  254.                 self.frame=0
  255.  
  256.         elif self.mode=="WL":
  257.             self.image=WalkLframes[self.frame]
  258.             if self.increment>3:
  259.                 self.frame+=1
  260.                 self.increment=0
  261.             self.increment+=1
  262.             if self.frame>=7:
  263.                 self.frame=0
  264.  
  265.  
  266. class Game_variables():
  267.     def __init__(self):
  268.         self.SCREENW=SCREENW
  269.         self.SCREENH=SCREENH
  270.         self.gamestate="Level 1" #P-passive A-active start initial
  271.         self.gamepause=False
  272.         self.quitgame=False
  273.         self.gamespeed=50
  274.         self.framerate=60
  275.         self.glt_sec=clock.tick(self.framerate)/1000 # game looptime in seconds
  276.  
  277. gv=Game_variables() # initialize the game variables
  278.  
  279. Heroimages={}
  280. WalkLframes=[] # Left walk animation frames
  281. WalkRframes=[] # Right walk animation frames
  282. #------------------------------------------------------------------------------#
  283. #                 LOAD ALL THE GRAPHICS and convert to scale
  284. #------------------------------------------------------------------------------#
  285.  
  286. Heroimages["SL"]=pygame.image.load("grfx\psl.png").convert_alpha() # stand left
  287. Heroimages["SR"]=pygame.image.load("grfx\psr.png").convert_alpha() # stand right
  288. Heroimages["UL"]=pygame.image.load("grfx\pjl.png").convert_alpha() # jump left
  289. Heroimages["DL"]=pygame.image.load("grfx\pjl.png").convert_alpha() # jump left
  290. Heroimages["UR"]=pygame.image.load("grfx\pjr.png").convert_alpha() # jump right
  291. Heroimages["DR"]=pygame.image.load("grfx\pjr.png").convert_alpha() # jump right
  292. Heroimages["FL1"]=pygame.image.load("grfx\pf01l.png").convert_alpha()#fall left1
  293. Heroimages["FL2"]=pygame.image.load("grfx\pf02l.png").convert_alpha()#fall left2
  294. Heroimages["FL3"]=pygame.image.load("grfx\pf03l.png").convert_alpha()#fall left3
  295. Heroimages["FL4"]=pygame.image.load("grfx\pf04l.png").convert_alpha()#fall lef4
  296. Heroimages["FL5"]=pygame.image.load("grfx\pf05l.png").convert_alpha()#fall left5
  297. Heroimages["FR1"]=pygame.image.load("grfx\pf01r.png").convert_alpha()#f right1
  298. Heroimages["FR2"]=pygame.image.load("grfx\pf02r.png").convert_alpha()#f right2
  299. Heroimages["FR3"]=pygame.image.load("grfx\pf03r.png").convert_alpha()#f right3
  300. Heroimages["FR4"]=pygame.image.load("grfx\pf04r.png").convert_alpha()#f right4
  301. Heroimages["FR5"]=pygame.image.load("grfx\pf05r.png").convert_alpha()#f right5
  302. Herolives=pygame.image.load("grfx\hl.png").convert_alpha() # no of lives display icon
  303.  
  304. scoretext = write("Score: ", 22)
  305. health=write(("Health:"),20)
  306.  
  307. # ROOM 1 Walls
  308. room1walls=[[0,0,SCREENW,thk,blue],[0,0,thk,SCREENH,blue],
  309.        [SCREENW-thk,0,thk,SCREENH,blue],[0,SCREENH-thk,SCREENW,thk,blue]]
  310. # ROOM 1 Platforms
  311. r1platforms=[[100,650,80,thk,blue],[200,550,80,thk,blue],[300,450,80,thk,blue],
  312.              [400,350,80,thk,blue],[300,250,80,thk,blue],[200,150,80,thk,blue],
  313.              [50,100,80,thk,blue],[400,550,80,thk,blue],[500,236,80,thk,blue],
  314.              [650,150,80,thk,blue],[600,550,80,thk,blue],[100,450,80,thk,blue],
  315.              [0,330,80,thk,blue],[200,340,80,thk,blue],[720,450,80,thk,blue],
  316.              [550,350,80,thk,blue],[470,150,80,thk,blue]]
  317.  
  318. Herosprite = pygame.sprite.Group()
  319. wallsprites=pygame.sprite.Group()
  320. platformsprites=pygame.sprite.Group()
  321. allsprites=pygame.sprite.Group()
  322.  
  323. # ---------------create the walk cycle animation frame lists--------------
  324. for x in xrange(7):
  325.     name="grfx\pwl0"+str(x)+".png"
  326.     frame=pygame.image.load(name).convert_alpha()
  327.     WalkLframes.append(frame)
  328.  
  329. for x in xrange(7):
  330.     name="grfx\pwr0"+str(x)+".png"
  331.     frame=frame=pygame.image.load(name).convert_alpha()
  332.     WalkRframes.append(frame)
  333.  
  334. Heroimages["WL"]=WalkLframes
  335. Heroimages["WR"]=WalkRframes
  336.  
  337. for item in room1walls:
  338.     wall=Walls_and_platforms(item[0],item[1],item[2],item[3],item[4])
  339.     allsprites.add(wall)
  340. for item in r1platforms:
  341.     platform=Walls_and_platforms(item[0],item[1],item[2],item[3],item[4])
  342.     allsprites.add(platform)
  343. #------------------------------------------------------------------------
  344. # status, x,scale, walkleft animframes, walkright animframes
  345. Hero=Hero_create()
  346. Herosprite.add(Hero)
  347.  
  348. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  349. #                           MAIN GAME LOOP
  350. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  351. while True:
  352.     Hero.stop=False
  353.     gv.glt_sec=clock.tick(gv.framerate)/1000.0# game loop time in seconds
  354.     constant=round(gv.gamespeed*gv.glt_sec,2) # round to 2 decimals
  355.     scorestr= "".join(str(i) for i in score)
  356.     realscore=int(scorestr)
  357.  
  358.     pygame.event.pump()
  359.     keys = pygame.key.get_pressed()
  360.     if keys[K_ESCAPE]:
  361. ##        really_quit()
  362.         game_over()
  363.  
  364. # WR - WALK RIGHT     WL - WALK LEFT    SR - STAND RIGHT    SL - STAND LEFT
  365. ##    if Hero.stop==False:
  366.     if keys[K_RIGHT] and Hero.ground==True: # WALKING RIGHT
  367.         Hero.speedx=2*constant
  368.         Hero.mode="WR"
  369.  
  370.     # Jumping up and moving right
  371.     elif keys[K_RIGHT] and Hero.jump==True and Hero.ground==False\
  372.          and Hero.mode[1]=="R":
  373.         Hero.speedx=3*constant
  374.  
  375.     elif keys[K_LEFT] and Hero.ground==True: # WALKING LEFT
  376.         Hero.speedx=-2*constant
  377.         Hero.mode="WL"
  378.  
  379.     # Jumping up and moving left
  380.     elif keys[K_LEFT] and Hero.jump==True and Hero.ground==False\
  381.          and Hero.mode[1]=="L":
  382.         Hero.speedx=-3*constant
  383.  
  384.     else:
  385.         Hero.speedx=0
  386.  
  387.     for event in pygame.event.get():
  388.         if event.type==KEYDOWN:
  389.  
  390. #-----------GAME PAUSE
  391.  
  392.             if event.key==K_p:
  393.                 gv.gamepause=not gv.gamepause
  394.                 if gv.gamepause==True:
  395.                     print 'pause simulation ON',gv.gamepause
  396.                 else:
  397.                     print 'pause simulation OFF',gv.gamepause
  398.  
  399. #-----------TOGGLE FULLSCREEN ON/OFF
  400.  
  401.             elif event.key==K_f and SCREENMODE==0:
  402.                 SCREENMODE=FULLSCREEN
  403.                 screen = pygame.display.set_mode((SCREENSIZE),SCREENMODE,0)
  404.             elif event.key==K_f and SCREENMODE==FULLSCREEN:
  405.                 SCREENMODE=0
  406.                 screen = pygame.display.set_mode((SCREENSIZE),SCREENMODE,0)
  407.  
  408. #-----------JUMP UP, RIGHT OR LEFT
  409.  
  410.             if Hero.ground==True:
  411.                 if event.key==K_UP and Hero.mode[1]=="R": # UR - UP RIGHT
  412.                     Hero.mode="UR" # RIGHT UP
  413.                     Hero.ground=False
  414.                     Hero.jump=True
  415.  
  416.                 elif event.key==K_UP and Hero.mode[1]=="L": # UL - UP LEFT
  417.                     Hero.mode="UL" # LEFT UP
  418.                     Hero.ground=False
  419.                     Hero.jump=True
  420.  
  421. #------------------ The Jump and Fall automated routine ---------------------
  422.  
  423.     if Hero.mode=="UR" and Hero.rect.y>=Hero.jumplimit:
  424.         Hero.speedy=-4*constant # UP RIGHT
  425.     if Hero.mode=="UR" and Hero.rect.y<=Hero.jumplimit:
  426.         Hero.fallcounter=0
  427.         Hero.mode="DR" # DOWN RIGHT
  428.         Hero.floor=Hero.rect.bottom
  429.  
  430.     if Hero.mode=="UL" and Hero.rect.y>=Hero.jumplimit:
  431.         Hero.speedy=-4*constant# UP LEFT
  432.     if Hero.mode=="UL" and Hero.rect.y<=Hero.jumplimit:
  433.         Hero.fallcounter=0
  434.         Hero.mode="DL" # DOWN LEFT
  435.         Hero.floor= Hero.rect.bottom
  436.  
  437. #-----------------------------------------------------------------------------
  438.  
  439.     # No ground.. Hero FALLS!
  440.     if Hero.ground==False and Hero.mode[0]<>"U":
  441.         Hero.speedy+=1+GRAVITY*constant
  442.         Hero.fallcounter=(Hero.rect.bottom-Hero.floor)
  443.         if Hero.speedy>10:
  444.             Hero.speedy=10
  445.         if Hero.fallcounter>110 and Hero.fallcounter<300:# lowest platform
  446.             Hero.mode="F"+Hero.mode[1]+"1"
  447.  
  448.         if Hero.fallcounter>301 and Hero.fallcounter<400:
  449.             Hero.mode="F"+Hero.mode[1]+"2"
  450.  
  451.         if Hero.fallcounter>401 and Hero.fallcounter<500:
  452.             Hero.mode="F"+Hero.mode[1]+"3"
  453.  
  454.         if Hero.fallcounter>501:
  455.             Hero.mode="F"+Hero.mode[1]+"4"
  456.             Hero.life-=0
  457.  
  458.         if Hero.life<0:
  459.             Hero.life=0
  460. ##    print Hero.fallcounter
  461.  
  462.     if Hero.life>0:
  463.         Hero.move(Hero.speedx,Hero.speedy)
  464.  
  465.     update_all()
  466. #==============================================================================#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement