Advertisement
Guest User

Untitled

a guest
Apr 26th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.64 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import random
  4. import time
  5. from pygame.locals import *
  6.  
  7. try:
  8.     import android
  9. except ImportError:
  10.     android = None
  11.  
  12. def main():
  13.  
  14.     class Player:
  15.         def __init__(self):
  16.             self.x = 188
  17.             self.y = 180
  18.             self.image = pygame.image.load('player.png')
  19.             self.score = 0
  20.         def draw(self):
  21.             screen.blit(self.image,(self.x,self.y))
  22.         def move(self,move):
  23.             if 0 <= self.x + move <= 336:
  24.                 self.x += move
  25.             #elif self.
  26.  
  27.     class Block:
  28.         def __init__(self):
  29.             global current_block
  30.             #self.x = random.choice(range(0,196,4))
  31.             self.y = 0
  32.             current_block = random.choice(block_images)
  33.             self.image = pygame.image.load(current_block)
  34.             self.already_passed_line = False
  35.         def draw(self):
  36.             screen.blit(self.image, (0,self.y))
  37.         def move(self):
  38.             self.y += block_speed
  39.         def off_screen(self):
  40.             return self.y > 256
  41.         def passed_line(self):
  42.             if (not self.already_passed_line) and (self.y >= 128):
  43.                 self.already_passed_line = True
  44.                 return True
  45.  
  46.     def player_collided():
  47.         for x in range(player.x,player.x+48):
  48.             if sum(screen.get_at((x,180))[:-1]) > 10:
  49.                 return True
  50.         for y in range(180,212):
  51.             if sum(screen.get_at((player.x,y))[:-1]) >10:
  52.                 return True
  53.             elif sum(screen.get_at((player.x+47, y))[:-1]) >10:
  54.                 return True
  55.         return False
  56.  
  57.     def intro():
  58.         while 1:
  59.             screen.fill(Color(239,254,221))
  60.             screen.blit(huge_font.render('FALL UP!',False,pygame.Color('black')),(88,52))
  61.             screen.blit(big_font.render('STAGE 1 | STAGE 2',False,pygame.Color('black')),(24,136))
  62.             screen.blit(font.render('%s'%(high_score_1),False,pygame.Color('black')),(80,200))
  63.             screen.blit(font.render('%s'%(high_score_2),False,pygame.Color('black')),(240,200))
  64.             for event in pygame.event.get():
  65.                 if event.type == QUIT:
  66.                     pygame.quit()
  67.                     sys.exit()
  68.                 if event.type == MOUSEBUTTONDOWN:
  69.                     if event.pos[0] < 192:
  70.                         return 1
  71.                     else:
  72.                         return 2
  73.                 if event.type == KEYDOWN:
  74.                     if event.key == K_BACKSPACE:
  75.                         error #will close the app
  76.             pygame.display.update()
  77.  
  78.     pygame.init()
  79.     screen = pygame.display.set_mode((384,256))
  80.     fps_clock = pygame.time.Clock()
  81.  
  82.     if android:
  83.         android.init()
  84.         android.map_key(android.KEYCODE_MENU, pygame.K_p)
  85.         android.map_key(android.KEYCODE_BACK, pygame.K_BACKSPACE)
  86.  
  87.     PIXEL_SIZE = 4
  88.  
  89.     font = pygame.font.Font('LCD_Solid.ttf',24)
  90.     big_font = pygame.font.Font('LCD_Solid.ttf',32)
  91.     huge_font = pygame.font.Font('LCD_Solid.ttf',44)
  92.  
  93.  
  94.     map_image = pygame.image.load('map4.png')
  95.     map_prime = map_image
  96.     main_menu = True
  97.  
  98.     mouse_x = mouse_y = 0
  99.     while 1:
  100.         with open('stage1_score.txt','r') as score_file:
  101.                 high_score_1 = score_file.read()
  102.                 if not high_score_1:
  103.                     high_score_1 = 0
  104.         with open('stage2_score.txt','r') as score_file:
  105.                 high_score_2 = score_file.read()
  106.                 if not high_score_2:
  107.                     high_score_2 = 0
  108.         scores = [high_score_1,high_score_2]
  109.         if main_menu:
  110.             stage = intro()
  111.         main_menu = False
  112.         move = 0
  113.         block_images = ['block%i.png'%i for i in range(2,9) if 'block%i.png'%i ] #create a list of all the block image names
  114.         player = Player()
  115.         blocks = [Block()]
  116.         game_over = False
  117.         restart = False
  118.         level_up = False
  119.         pause = False
  120.         player_speed = 2
  121.         block_speed = 2
  122.         k = -12630
  123.         start_time = time.time()
  124.         score = 0
  125.         fps = 30
  126.         game_over_msg = 'GAME OVER!'
  127.         pause_time = 0
  128.  
  129.         while not restart:
  130.             block_images = ['block%i.png'%i for i in range(2,9) if 'block%i.png'%i != current_block] #list of available blocks so there's no repetition
  131.             screen.fill(Color(239,254,221)) #light green
  132.             player.draw()
  133.  
  134.             if stage == 1:
  135.                 for block in blocks:
  136.                     block.draw()
  137.             else:
  138.                 screen.blit(map_image,(0,k))
  139.                 if not game_over and not pause: k+= block_speed
  140.  
  141.             screen.blit(font.render('LEVEL: %i'%(int(score)/10),False,Color('black')),(244,0))
  142.             screen.blit(font.render('TIME: %s'%str(score)[:str(score).find('.')+2],False,Color('black')),(0,0))
  143.             if game_over:
  144.                 screen.blit(big_font.render(game_over_msg,False,Color('black')),(100,120))
  145.                 if score > float(scores[stage-1]):
  146.                     scores[stage-1] = str(score)[:str(score).find('.')] + str(score)[str(score).find('.'):str(score).find('.')+2]
  147.                 screen.blit(font.render('BEST: %s'%scores[stage-1],False,Color('black')),(100,148))
  148.  
  149.             for event in pygame.event.get(): #event loop
  150.                 if event.type == QUIT:
  151.                         pygame.quit()
  152.                         sys.exit()
  153.                 if event.type == KEYDOWN:
  154.                     if event.key == K_RIGHT:
  155.                         move = PIXEL_SIZE * player_speed #player moves 4*player_speed px right
  156.                     elif event.key == K_LEFT:
  157.                         move = -PIXEL_SIZE * player_speed
  158.                     elif event.key == K_r:
  159.                         restart = True
  160.                     elif event.key == K_p and not game_over:
  161.                         pause = not pause
  162.                         if pause:
  163.                             pause_start = time.time() - pause_time
  164.                         else:
  165.                             pause_time = round(time.time() - pause_start,2)
  166.                     elif event.key == K_BACKSPACE:
  167.                         restart = main_menu = True
  168.                 if event.type == KEYUP:
  169.                     if event.key == K_RIGHT and move > 0:
  170.                         move = 0
  171.                     if event.key == K_LEFT and move < 0:
  172.                         move = 0
  173.                 if event.type == MOUSEBUTTONDOWN:
  174.                     if game_over:
  175.                         restart = True
  176.                     mouse_x, mouse_y = event.pos
  177.                     if mouse_x < 192:
  178.                         move = -PIXEL_SIZE * player_speed
  179.                     else:
  180.                         move = PIXEL_SIZE * player_speed
  181.                 if event.type == MOUSEBUTTONUP:
  182.                     if mouse_x < 192 and move < 0:
  183.                         move = 0
  184.                     if mouse_x >= 192 and move > 0:
  185.                         move = 0
  186.  
  187.             if not pause:
  188.                 if player_collided() and not game_over:
  189.                     pygame.display.update()
  190.                     game_over = True
  191.                     if score > float(scores[stage-1]):
  192.                         with open('stage%i_score.txt'%stage,'w') as score_file:
  193.                                 rounded_score = str(score)[:str(score).find('.')] + str(score)[str(score).find('.'):str(score).find('.')+2]
  194.                                 score_file.write(rounded_score)
  195.  
  196.                 if move and not game_over:
  197.                     player.move(move)
  198.  
  199.                 if blocks[0].off_screen():
  200.                     blocks = blocks[1:]
  201.  
  202.                 if blocks[0].passed_line():
  203.                     blocks.append(Block())
  204.  
  205.                 if stage == 1 and not game_over:
  206.                     for block in blocks:
  207.                         block.move()
  208.                 if stage == 2 and not game_over:
  209.                     if score > 125:
  210.                         game_over = True
  211.                         game_over_msg = 'YOU WON!'
  212.  
  213.                 if not game_over and int(score) and int(score) % 10==0 and not level_up:
  214.                     fps += 4
  215.                     level_up = True
  216.                 if level_up and not int(time.time() - start_time - pause_time) % 10 == 0:
  217.                     level_up = False
  218.                 if not game_over:
  219.                     score = round(time.time() - start_time - pause_time,2)
  220.             else:
  221.                 screen.blit(huge_font.render('PAUSED',False,Color('black')),(108,120))
  222.  
  223.             pygame.display.update()
  224.             fps_clock.tick(fps)
  225.  
  226. if __name__ == '__main__':
  227.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement