Advertisement
Guest User

Canabalt - SPACE LEVEL

a guest
Jan 27th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.32 KB | None | 0 0
  1. # Import the modules
  2. import pygame
  3. import os
  4. import random
  5. import threading
  6.  
  7.  
  8. # Define colours in RBG format
  9. white = (255, 255, 255)
  10. black = (0, 0, 0)
  11. blue = (0, 0, 255)
  12. snow = (255, 250, 250)
  13. red = (255, 0 , 0)
  14. color = (125, 125, 125)
  15. bullet = (255, 166, 39)
  16.  
  17. # Sets the constants for screen display size
  18. screen_height = 854
  19. screen_width = 480
  20. width = 10
  21. height = 20
  22.  
  23. # Initialize pygame engine
  24. pygame.mixer.init()
  25.  
  26. # Loads the required music files
  27. intro_music = (os.path.join("space.ogg"))
  28.  
  29. # Plays the music, sets volume and sets the loop
  30. pygame.mixer.music.load(os.path.join("space.ogg"))
  31. pygame.mixer.music.set_volume(2)  
  32. pygame.mixer.music.play(-1)
  33.  
  34. # Set display size according to constants
  35. screen = pygame.display.set_mode((1384, 600))
  36.  
  37. # Sets the variable background to the image file
  38. background = pygame.image.load("space.jpg")
  39. background = background.convert()
  40.  
  41. # Sets program display title
  42. pygame.display.set_caption("CANABALT")
  43.  
  44. # Sets visibility of the moouse to false
  45. pygame.mouse.set_visible(0)
  46.  
  47. # Sets the clock for program and allows
  48. # you to set Frame per Second later
  49. clock = pygame.time.Clock()
  50.  
  51. # Initializes font and sets defualt font if
  52. # need to write something later on
  53. pygame.font.init()
  54. font = pygame.font.Font(None, 50)
  55. font_big = pygame.font.Font(None, 70)
  56. font_small = pygame.font.Font(None, 40)
  57. #Set required global variables
  58. lives = 5
  59. done = False
  60. game_running = True
  61. background_speed = 5
  62. back = background
  63. back2 = background
  64. x = 0
  65. y = 0
  66.  
  67.  
  68. screenWidth = 1384
  69.  
  70. # Define relevant texts needed to be used later
  71. space_text = font_big.render("SPACE", True, (255, 255, 255))
  72. objective_text = font.render("THIS IS ENDLESS GAME MODE", True, (255, 255, 255))
  73. start_text = font_small.render("PRESS SPACE TO START", True, (255,255,255))
  74. exit_text = font.render("Press ESCAPE to Exit", True, (255,255,255))
  75.  
  76.  
  77. #Define classes and sub-classes
  78. class Platform(pygame.sprite.Sprite):
  79.     change_x = 0
  80.     change_y = 0
  81.     def __init__(self, x, y):
  82.         pygame.sprite.Sprite.__init__(self)
  83.  
  84.         self.image = pygame.image.load("space_platforms_long.jpg")
  85.        
  86.  
  87.  
  88.         self.rect = self.image.get_rect()
  89.         self.rect.x = x
  90.         self.rect.y = y
  91.  
  92. class Medium(pygame.sprite.Sprite):
  93.     change_x = 0
  94.     change_y = 0
  95.     def __init__(self, x, y):
  96.         pygame.sprite.Sprite.__init__(self)
  97.  
  98.         self.image = pygame.image.load("space platform.jpg")
  99.        
  100.  
  101.  
  102.         self.rect = self.image.get_rect()
  103.         self.rect.x = x
  104.         self.rect.y = y
  105.  
  106.  
  107. class Rock(pygame.sprite.Sprite):
  108.     change_x = 0
  109.     change_y = 0
  110.     def __init__(self, x, y):
  111.         pygame.sprite.Sprite.__init__(self)
  112.  
  113.         self.image = pygame.image.load("asteroid.png")
  114.  
  115.         self.image.set_colorkey(white)
  116.  
  117.         self.rect = self.image.get_rect()
  118.         self.rect.x = x
  119.         self.rect.y = y
  120.  
  121.  
  122.  
  123.      
  124. # Class defining        
  125. class Player(pygame.sprite.Sprite):
  126.  
  127.     change_x = 0
  128.     change_y = 0
  129.  
  130.     frame = 0
  131.  
  132.        
  133.     def __init__(self, x, y):
  134.         pygame.sprite.Sprite.__init__(self)
  135.  
  136.         self.images = []
  137.  
  138.         for i in range(1,8):
  139.             img = img = pygame.image.load("stickman"+str(i)+".png")
  140.             img.set_colorkey(white)
  141.            
  142.             self.images.append(img)
  143.  
  144.         self.image_platform_long = self.images[0]
  145.        
  146.  
  147.         self.rect = self.image_platform_long.get_rect()
  148.              
  149.      # Change the speed of the player
  150.     def changespeed(self,x,y):
  151.         self.change_x+=x
  152.         self.change_y+=y
  153.          
  154.      # Find a new position for the player
  155.     def update(self):
  156.         self.rect.x += self.change_x
  157.         self.rect.y += self.change_y
  158.         collide = collide = pygame.sprite.spritecollide(player, platform_list, False)
  159.  
  160.         if done == False:
  161.             self.frame +=1
  162.  
  163.             if self.frame > 4*5:
  164.                 self.frame = 0
  165.    
  166.            
  167.      # Controls player running images
  168.             self.image = self.images[self.frame//4]
  169.  
  170.         if done ==False:
  171.             self.frame += 1
  172.             if self.frame > 7*8:
  173.                 self.frame = 0
  174.                 self.image = self.images[self.frame//4+4]
  175.  
  176.  
  177.  
  178.     def jump(self):
  179.         collide = collide = pygame.sprite.spritecollide(player, platform_list, False)
  180.  
  181.         if collide:
  182.             self.rect.y-=30
  183.  
  184.     def gravity(self):
  185.         collide = collide = pygame.sprite.spritecollide(player, platform_list, False)
  186.         self.change_y +=.14
  187.         if collide:
  188.             self.change_y = False
  189.         if self.change_y >= 6.5:
  190.             self.change_y = 6.5
  191.         medium_collide = pygame.sprite.spritecollide(player, medium_list, False)
  192.         if medium_collide:
  193.             self.change_y = False
  194.         if self.change_y >= 6.5:
  195.             self.change_y = 6.5
  196.  
  197. class Bullet(pygame.sprite.Sprite):
  198.  
  199.     def __init__(self):
  200.         # Call the parent class (Sprite) constructor
  201.         pygame.sprite.Sprite.__init__(self)
  202.  
  203.         self.image = pygame.Surface([4, 10])
  204.         self.image.fill(black)
  205.  
  206.         self.rect = self.image.get_rect()
  207.          
  208.  
  209. # List of platforms in the game
  210. platform_list = pygame.sprite.Group()
  211. medium_list = pygame.sprite.Group()
  212. # List of all the sprites in the game
  213. all_sprites_list = pygame.sprite.Group()
  214.  
  215. # List of the bullets in the game
  216. bullet_list = pygame.sprite.Group()
  217.  
  218. #list of the rocks in the game
  219. rock_list = pygame.sprite.Group()
  220.  
  221.                  
  222.            
  223. #Set spawning of objects and the amount fo spawns
  224. for x in range(300):
  225.     platform = Platform(0,0)
  226.     # Set x and y
  227.     platform.rect.x = random.normalvariate(-200,20000)
  228.     platform.rect.y = random.normalvariate(370,550)
  229.     platform_list.add(platform)
  230.     all_sprites_list.add(platform)
  231.  
  232. for a in range(150):
  233.     rock = Rock(0,0)
  234.  
  235.     rock.rect.x = random.randrange(-200,30000)
  236.     rock.rect.y = random.randrange(80,500)
  237.  
  238.  
  239.  
  240.  
  241.     # Add platform and rock to list of moving objects
  242.  
  243.     rock_list.add(rock)
  244.     all_sprites_list.add(rock)
  245.  
  246. for l in range(275):
  247.     medium = Medium(0,0)
  248.     medium.rect.x = random.normalvariate(-200,20000)
  249.     medium.rect.y = random.normalvariate(370,550)
  250.  
  251.     medium_list.add(medium)
  252.     all_sprites_list.add(medium)    
  253.  
  254. # Set starting player position and platform speed  
  255. player = Player(20,15)
  256.  
  257. player.rect.x = 50
  258. player.rect.y = 10
  259. platform_speed = -3
  260. rock_speed = -3
  261.  
  262.  
  263. # Add player to list of moving objects    
  264. all_sprites_list.add(player)
  265. all_sprites_list.add(platform)
  266. all_sprites_list.add(rock)
  267. all_sprites_list.add(medium)
  268.  
  269. start =  True
  270.  
  271. while start:
  272.     screen.blit(space_text, [600,20])
  273.     screen.blit(objective_text, [30,100])
  274.     screen.blit(start_text, [30,150])
  275.     screen.blit(exit_text, [1000,555])
  276.     pygame.display.update()
  277.     for event in pygame.event.get():
  278.         if event.type == pygame.KEYDOWN:
  279.             if event.key == pygame.K_SPACE:
  280.                 start = False
  281.             if event.key == pygame.K_SPACE:
  282.                 start = False
  283.             if event.key == pygame.K_1:
  284.                 import forest_level_end
  285.             if event.key == pygame.K_2:
  286.                 import sea_level_end
  287.             if event.key == pygame.K_3:
  288.                 import desert_level_end
  289.             if event.key == pygame.K_4:
  290.                 import underwater_level_end
  291.             if event.key == pygame.K_5:
  292.                 import space_level_end
  293.         if event.type == pygame.KEYDOWN:
  294.             if event.key == pygame.K_ESCAPE:
  295.                 pygame.quit()
  296.        
  297.  
  298.        
  299.  
  300.  
  301.  
  302. # Main program loop and controls Quitting the game
  303. done = False
  304. collide = pygame.sprite.spritecollide(player, platform_list, False)
  305.  
  306. while done == False:
  307.    
  308.  
  309.     for event in pygame.event.get():
  310.         if event.type == pygame.QUIT:
  311.             done=True
  312.  
  313.        
  314.        
  315.  
  316.         # Set the speed based on the key pressed
  317.         collide = pygame.sprite.spritecollide(player, platform_list, False)
  318.  
  319.  
  320.    
  321.         if event.type == pygame.KEYDOWN:
  322.             if event.key == pygame.K_LEFT:
  323.                 player.changespeed(-4,0)                
  324.             if event.key == pygame.K_RIGHT:
  325.                 player.changespeed(4,0)
  326.             if event.key == pygame.K_DOWN:
  327.                 player.changespeed(0,3)
  328.             if event.key == pygame.K_UP:
  329.                 player.changespeed(0,-5.5)
  330.            
  331.  
  332.                
  333.         # Reset speed when key goes up      
  334.         if event.type == pygame.KEYUP:
  335.             if event.key == pygame.K_LEFT:
  336.                 player.changespeed(4,0)
  337.             if event.key == pygame.K_RIGHT:
  338.                 player.changespeed(-4,0)
  339.             if event.key == pygame.K_DOWN:
  340.                 player.changespeed(0,-3)
  341.             if event.key == pygame.K_UP:
  342.                 player.changespeed(0,5.5)
  343.             if event.key == pygame.K_ESCAPE:
  344.                 pygame.quit()
  345.  
  346.     # Stops player from leaving the screen and controls speed of
  347.     # platform to give illsuion of player running faster
  348.     if player.rect.x <= 0:
  349.         diff = 20 + player.rect.x
  350.         player.rect.x=0
  351.         for platform in platform_list:
  352.             platform.rect.x += 1.0
  353.         for rock in rock_list:
  354.             rock.rect.x += 1.0
  355.  
  356.  
  357.     if player.rect.y <= -1:
  358.         player.rect.y=0
  359.  
  360.     if player.rect.x <=75:
  361.         diff = player.rect.x + 75
  362.         for platform in platform_list:
  363.             platform.rect.x += 0.8
  364.         for rock in rock_list:
  365.             rock.rect.x += 0.8
  366.         for medium in medium_list:
  367.             medium.rect.x += 0.8
  368.  
  369.     if player.rect.x >= 75:
  370.         diff = player.rect.x - 75
  371.         for platform in platform_list:
  372.             platform.rect.x -= 0.8
  373.         for rock in rock_list:
  374.             rock.rect.x -= 0.8
  375.         for medium in medium_list:
  376.             medium.rect.x -= 0.8
  377.            
  378.     if player.rect.x >=115:
  379.         diff = player.rect.x -115
  380.         for platform in platform_list:
  381.             platform.rect.x -= 0.9
  382.         for rock in rock_list:
  383.             rock.rect.x -= 0.9
  384.         for medium in medium_list:
  385.             medium.rect.x -= 0.9
  386.            
  387.     if player.rect.x >=140:
  388.         diff = player.rect.x -140
  389.         for platform in platform_list:
  390.             platform.rect.x -= 1.1
  391.         for rock in rock_list:
  392.             rock.rect.x -= 1.1
  393.         for medium in medium_list:
  394.             medium.rect.x -= 1.1
  395.  
  396.     if player.rect.x >=165:
  397.         diff = player.rect.x -165
  398.         for platform in platform_list:
  399.             platform.rect.x -= 1.2
  400.         for rock in rock_list:
  401.             rock.rect.x -= 1.2
  402.         for medium in medium_list:
  403.             medium.rect.x -= 1.2
  404.  
  405.     if player.rect.x >=200:
  406.         diff = player.rect.x -200
  407.         for platform in platform_list:
  408.             platform.rect.x -= 1.3
  409.         background_speed = 9
  410.         for rock in rock_list:
  411.             rock.rect.x -= 1.3
  412.         for medium in medium_list:
  413.             medium.rect.x -= 1.3
  414.  
  415.  
  416.     if player.rect.x >=240:
  417.         diff = player.rect.x -240
  418.         for platform in platform_list:
  419.             platform.rect.x -= 1.4
  420.     if player.rect.x >=240:
  421.         background_speed = 10
  422.         for rock in rock_list:
  423.             rock.rect.x -= 1.4
  424.         for medium in medium_list:
  425.             medium.rect.x -= 1.4
  426.            
  427.     if player.rect.x >=270:
  428.         diff = player.rect.x -270
  429.         for platform in platform_list:
  430.             platform.rect.x -= 1.5
  431.     if player.rect.x >=270:
  432.         background_speed = 11
  433.         for rock in rock_list:
  434.             rock.rect.x -= 1.5
  435.         for medium in medium_list:
  436.             medium.rect.x -= 1.5
  437.  
  438.     if player.rect.x >=300:
  439.         diff = player.rect.x -300
  440.         for platform in platform_list:
  441.             platform.rect.x -= 1.6
  442.     if player.rect.x >=300:
  443.         background_speed = 12
  444.         for rock in rock_list:
  445.             rock.rect.x -= 1.6
  446.         for medium in medium_list:
  447.             medium.rect.x -= 1.6
  448.  
  449.     if player.rect.x >=340:
  450.         diff = player.rect.x -340
  451.         for platform in platform_list:
  452.             platform.rect.x -= 1.7
  453.     if player.rect.x >=340:
  454.         background_speed = 13
  455.         for rock in rock_list:
  456.             rock.rect.x -= 1.7
  457.         for medium in medium_list:
  458.             medium.rect.x -= 1.7
  459.        
  460.     if player.rect.x >=380:
  461.         diff = player.rect.x -380
  462.         for platform in platform_list:
  463.             platform.rect.x -= 1.8
  464.     if player.rect.x >=380:
  465.         background_speed = 14
  466.         for rock in rock_list:
  467.             rock.rect.x -= 1.8
  468.         for medium in medium_list:
  469.             medium.rect.x -= 1.8
  470.  
  471.     if player.rect.x >=420:
  472.         diff = player.rect.x -420
  473.         for platform in platform_list:
  474.             platform.rect.x -= 1.9
  475.     if player.rect.x >=420:
  476.         background_speed = 15
  477.         for rock in rock_list:
  478.             rock.rect.x -= 1.9
  479.         for medium in medium_list:
  480.             medium.rect.x -= 1.9
  481.  
  482.     if player.rect.x >=460:
  483.         diff = player.rect.x -460
  484.         for platform in platform_list:
  485.             platform.rect.x -= 2.0
  486.     if player.rect.x >=460:
  487.         background_speed = 16
  488.         for rock in rock_list:
  489.             rock.rect.x -= 2.0
  490.         for medium in medium_list:
  491.             medium.rect.x -= 2.0
  492.  
  493.     if player.rect.x >=500:
  494.         diff = player.rect.x -500
  495.         for platform in platform_list:
  496.             platform.rect.x -= 2.1
  497.     if player.rect.x >=500:
  498.         background_speed = 17
  499.         for rock in rock_list:
  500.             rock.rect.x -= 2.1
  501.         for medium in medium_list:
  502.             medium.rect.x -= 2.1
  503.  
  504.     if player.rect.x >=540:
  505.         diff = player.rect.x -540
  506.         for platform in platform_list:
  507.             platform.rect.x -= 2.2
  508.     if player.rect.x >=540:
  509.         background_speed = 18
  510.         for rock in rock_list:
  511.             rock.rect.x -= 2.2
  512.         for medium in medium_list:
  513.             medium.rect.x -= 2.2
  514.  
  515.     if player.rect.x >=580:
  516.         diff = player.rect.x -580
  517.         for platform in platform_list:
  518.             platform.rect.x -= 2.3
  519.     if player.rect.x >=580:
  520.         background_speed = 19
  521.         for rock in rock_list:
  522.             rock.rect.x -= 2.3
  523.         for medium in medium_list:
  524.             medium.rect.x -= 2.3
  525.  
  526.     if player.rect.x >= 1340:
  527.         player.rect.x =1340
  528.  
  529.     if player.rect.y <=0:
  530.         player.rect.y =0
  531.        
  532.  
  533.     if player.rect.y >= 601:
  534.         lives -= 1
  535.     if player.rect.y >= 601:
  536.        player.rect.y = 0
  537.  
  538.     if player.rect.x <= 0:
  539.         player.rect.x =0
  540.  
  541.  
  542.  
  543.     if lives > 0:
  544.         y = y + player.rect.x/30
  545.         z = 22000 - y
  546.        
  547.     lives_text = font.render("%d lives" % lives, True, (255, 255, 255))    
  548.     coins_text = font.render("%d score" % y, True, (255, 255, 255))
  549.     current_stage = font.render("SPACE", True, (255, 255, 255))
  550.     lose_text = font.render("You lose, press ESCAPE to quit", True, (255,255,255))
  551.     final_score = font_big.render("You finished with %d" % y , True, (255,255,255))
  552.  
  553.  
  554.     #Platform loop
  555.     for platform in platform_list:
  556.         # Movement of block
  557.         platform.rect.x += platform_speed
  558.  
  559.     for medium in medium_list:
  560.         medium.rect.x += platform_speed
  561.  
  562.     for rock in rock_list:
  563.         rock.rect.x += rock_speed
  564.  
  565.  
  566.  
  567.         collide = pygame.sprite.spritecollide(player, platform_list, False)
  568.         if len(pygame.sprite.spritecollide(player, platform_list, False)) > 0:
  569.             player.rect.y-=2
  570.  
  571.         medium_collide = pygame.sprite.spritecollide(player, medium_list, False)
  572.         if len(pygame.sprite.spritecollide(player, medium_list, False)) > 0:
  573.             player.rect.y-=2
  574.  
  575.  
  576.         rock_collide = pygame.sprite.spritecollide(player, rock_list, True)
  577.         if rock_collide:
  578.             lives-=1
  579.         if rock_collide:
  580.             player.rect.y = 100
  581.             player.rect.x = 50
  582.         if lives <=0:
  583.             lives = 0
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.                
  591.  
  592.            
  593.            
  594.  
  595.  
  596.    
  597.  
  598.     # Draw all the spites
  599.     screen.blit(back, (x,0))
  600.     screen.blit(back2,(x-screenWidth,0))
  601.     screen.blit(lives_text, [20,4])
  602.     screen.blit(coins_text, [1190,4])
  603.     screen.blit(current_stage, [640, 4])
  604.     player.update()
  605.     all_sprites_list.draw(screen)
  606.     platform_list.update()
  607.     rock_list.update()
  608.     medium_list.update()
  609.    
  610.     x = x + background_speed
  611.  
  612.  
  613.     if x == screenWidth:
  614.         x = 0
  615.  
  616.    
  617.     # Gravity
  618.     player.gravity()
  619.    
  620.  
  621.     # Updates all the sprites and other objects
  622.     pygame.display.flip()
  623.  
  624.  
  625.     # Frame rate
  626.     clock.tick(30)
  627.    
  628.        
  629.    
  630.  
  631.     if x == screenWidth:
  632.         x = 0
  633.     if x >= screenWidth:
  634.         x = -1
  635.  
  636.  
  637.     # End game and stop all running variables
  638.     if lives == 1:
  639.         white = red
  640.         pygame.display.update()
  641.     if lives <= 0:
  642.         pygame.mixer.music.fadeout(7000)
  643.         screen.fill(black)
  644.         screen.blit(lose_text, [100,100])
  645.         screen.blit(final_score, [100,200])
  646.         pygame.display.update()
  647.         pygame.time.delay(100)
  648.         for event in pygame.event.get():
  649.             if event.type == pygame.KEYDOWN:
  650.                 if event.key == pygame.K_ESCAPE:
  651.                     pygame.quit()
  652.  
  653.  
  654.    
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661. clock.tick(30)
  662.  
  663. #Quits the game upon clicking the close button
  664.  
  665.  
  666. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement