Advertisement
Guest User

Twerk

a guest
Apr 27th, 2012
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.20 KB | None | 0 0
  1. import pygame
  2. from pygame import *
  3. import random
  4. import time
  5. import os
  6. import sys
  7. from pygame.locals import *
  8.  
  9. #I tried using this code but couldn't get apscheduler module installed, try and see if u can
  10. #http://pypi.python.org/pypi/APScheduler/
  11. #so instead I used another code which depends on the screen refresh rate (not 100% accurate)
  12. #===============
  13. # from apscheduler.scheduler import Scheduler
  14.  
  15. # # Start the scheduler
  16. # sched = Scheduler()
  17. # sched.start()
  18.  
  19. # # Schedule job_function to be called every 5 seconds
  20. # @sched.interval_schedule(seconds=5)
  21. # def incr_score():
  22.     # score += 100
  23.    
  24. #=======================
  25.  
  26. black = (0,0,0)
  27. white = (255,255,255)
  28. grey =  (190,190,190)
  29.  
  30. pygame.init()
  31.  
  32. def game():
  33.  
  34.  
  35.   def levels(Score):
  36.     if score >= 100:
  37.       enemies = 6
  38.       velocity = 2
  39.        
  40.        
  41.        
  42.   def text(text,x_pos,color,font2=28):
  43.         tfont = pygame.font.Font(None, font2)
  44.  
  45.         text=tfont.render(text, True, color)
  46.         textpos = text.get_rect(centerx=back.get_width()/2)
  47.         textpos.top = x_pos
  48.         screen.blit(text, textpos)
  49.  
  50.        
  51.   os.environ['SDL_VIDEO_CENTERED'] = '1'
  52.   mouse.set_visible(False)
  53.   #screen
  54.   screen=pygame.display.set_mode((665,500),0,32)
  55.   #load images etc.
  56.   backdrop = pygame.image.load('bg.jpg').convert_alpha()
  57.   menu = pygame.image.load('green.jpg').convert_alpha()
  58.   ballpic = pygame.image.load('ball.png').convert_alpha()
  59.   mouseball = pygame.image.load('mouseball.gif').convert_alpha()
  60.   display.set_caption('Twerk')
  61.   back = pygame.Surface(screen.get_size())     
  62.        
  63.                
  64.   start = False
  65.   repeat = False
  66.  
  67.   while start == False:
  68.         for event in pygame.event.get():
  69.             if event.type == pygame.QUIT:
  70.               pygame.quit()
  71.               sys.exit()
  72.                 #break
  73.                 #falling = True
  74.                 #finish = True
  75.  
  76.             if event.type == pygame.KEYDOWN:
  77.                 if event.key == pygame.K_SPACE:
  78.                     start = True
  79.                     break
  80.        
  81.         screen.blit(menu,[0,0])
  82.         pygame.display.set_caption("TWERK")
  83.        
  84.         #Text
  85.         #"Welcome to Escape"
  86.         text("Twerk",60,white,300)
  87.        
  88.         #"Instructions"
  89.         text("Instructions",310,white)
  90.         text("------------------------------------------",320,white)
  91.         text("Avoid the the enemies",340,white)
  92.         text("Last as long as you can!",360,white)
  93.         text("Press space to start",420,white)
  94.         pygame.display.flip()
  95.      
  96.   while start == True:
  97.     positionx=[]
  98.     positiony=[]
  99.     positionxmove=[]
  100.     positionymove=[]
  101.     falling = False
  102.     finish = False
  103.     score=0
  104.     enemies=3
  105.     velocity=1
  106.    
  107.     scoreIncrementTimer = 0
  108.     lastFrameTicks = pygame.time.get_ticks()
  109.  
  110.    
  111.     for i in range(enemies):
  112.       positionx.append(random.randint(300,400)+random.randint(-300,200))
  113.       positiony.append(random.randint(200,340)+random.randint(-200,100))
  114.       for p in [positionxmove, positionymove]:
  115.           p.append(random.randint(1,velocity))
  116.      
  117.    # Starting Twerk Screen!!
  118.     screen.blit(backdrop, (0,0))
  119.     text("Starting Twerk...",190,grey)
  120.     pygame.display.update()
  121.     time.sleep(2.5)
  122.    
  123.    
  124.     while not finish or falling:      
  125.     #Game control during Game
  126.     #----------------------
  127.       for event in pygame.event.get():
  128.         if event.type == pygame.QUIT:
  129.           pygame.quit()
  130.      
  131.          
  132.         if event.type == pygame.KEYDOWN:
  133.           if event.key == pygame.K_ESCAPE:
  134.             falling = True
  135.             finish = True
  136.             start = False
  137.          #----------------------  
  138.              
  139.       screen.blit(backdrop, (0,0))
  140.       for i in range(enemies):
  141.         screen.blit(ballpic,(positionx[i],positiony[i]))
  142.         mousex,mousey = pygame.mouse.get_pos()
  143.         mousex -= mouseball.get_width()/2
  144.         mousey -= mouseball.get_height()/2
  145.  
  146.         screen.blit(mouseball, (mousex,mousey))
  147.         #?
  148.         pygame.display.update()
  149.         pygame.display.set_caption("Score: %s " % (round(score)))
  150.         levels(score)
  151.         #=============Score System============
  152.         thisFrameTicks = pygame.time.get_ticks()
  153.         ticksSinceLastFrame = thisFrameTicks - lastFrameTicks
  154.         lastFrameTicks = thisFrameTicks
  155.        
  156.         scoreIncrementTimer = scoreIncrementTimer + ticksSinceLastFrame
  157.         if scoreIncrementTimer > 5000:
  158.             score = score + 100
  159.             scoreIncrementTimer = 0
  160.         #======================================
  161.      
  162.      
  163.       if falling:
  164.         for i in range(enemies):
  165.           positionymove[i]=1000
  166.           positionxmove[i]=0
  167.      
  168.       for i in range(enemies):
  169.        positionx[i]=positionx[i]+positionxmove[i]
  170.        positiony[i]=min(600,positiony[i]+positionymove[i])
  171.    
  172.       if falling:
  173.         falling=False
  174.         for posy in positiony:
  175.           if posy<600: falling=True
  176.      
  177.  
  178.       if not falling:
  179.         for i in range(enemies):
  180.           for j in range(i+1,enemies):
  181.             if abs(positionx[i]-positionx[j])<20 and abs(positiony[i]-positiony[j])<20:
  182.               p[i], p[j] = p[i],p[i]
  183.              
  184.         for i in range(enemies):  
  185.           if positionx[i]>600: positionxmove[i]*=-1
  186.           if positionx[i]<0: positionxmove[i]*=-1
  187.           if positiony[i]>440: positionymove[i]*=-1
  188.           if positiony[i]<0: positionymove[i]*=-1
  189.         pygame.display.update()
  190.            
  191.        
  192.         for i in range(enemies):
  193.           if abs(positionx[i]-mousex)<40 and abs(positiony[i]-mousey)<40:
  194.             falling = True
  195.             finish = True
  196.             start = False
  197.     pygame.display.update()
  198.    
  199.  
  200.     while finish == True:
  201.         screen.blit(menu,[0,0])
  202.         pygame.display.set_caption("TWERK")
  203.         text("Game Over",60,white,120)
  204.         text("Score: %s " % (round(score)),220,white,60)
  205.         text("Press space to return to Menu",360,white)
  206.  
  207.         pygame.display.update()
  208.         for event in pygame.event.get():
  209.             if event.type == pygame.QUIT:
  210.               pygame.quit()
  211.               sys.exit()
  212.              
  213.             if event.type == pygame.KEYDOWN:
  214.                 if event.key == pygame.K_SPACE:
  215.                   game()
  216.                  
  217.                
  218. game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement