Advertisement
xgeovanni

Avoid Bill

Jan 5th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. import pygame, pygame.mixer, random, sys
  2. from pygame.locals import *
  3. pygame.init()
  4.  
  5. screen = pygame.display.set_mode((640, 480))
  6.  
  7. clock = pygame.time.Clock()
  8.  
  9. pygame.mouse.set_visible(False)
  10.  
  11. hero = pygame.image.load("Images/hero.png")
  12. bill = pygame.image.load("Images/bill.png")
  13. gameover = pygame.image.load("Images/gameover.png")
  14. bullet = pygame.image.load("Images/bullet.png")
  15. cursor = pygame.image.load("Images/cursor.png")
  16. pausescreen = pygame.image.load("Images/paused.png")
  17.  
  18. pygame.mixer.music.load("Sounds/makaimura.mp3")
  19. pygame.mixer.music.set_volume(0.5)
  20. pygame.mixer.music.play(loops = -1, start = 0.0)
  21.  
  22. shot_sound = pygame.mixer.Sound("Sounds/shot.wav")
  23. death_sound = pygame.mixer.Sound("Sounds/death.wav")
  24.  
  25.  
  26. hero_y = 304 # hero y axis
  27. hero_x = 228 # hero x axis
  28.  
  29. bill_x = random.randint(0, 480)
  30. bill_y = random.randint(0, 640)
  31.  
  32. moveup = False
  33. movedown = False
  34. moveright = False
  35. moveleft = False
  36.  
  37. death_sound_played = False
  38.  
  39. dead = False
  40. shot = False
  41. paused = False
  42. bill_hp = 100
  43. rate = 60
  44.  
  45. shotmoves = 0
  46.  
  47. while True:
  48.    
  49.     clock.tick(rate)
  50.     cursor_x, cursor_y = pygame.mouse.get_pos()
  51.     screen.fill((255, 255, 255))
  52.  
  53.     if shotmoves == 60:
  54.         shot = False
  55.         shotmoves = 0
  56.  
  57.     if not paused:
  58.         if moveup:
  59.             hero_x -= 1
  60.         if movedown:
  61.             hero_x += 1
  62.         if moveright:
  63.             hero_y += 1
  64.         if moveleft:
  65.             hero_y -= 1
  66.  
  67.         if hero_x < -10:
  68.             hero_x = 470
  69.         elif hero_x > 480:
  70.             hero_x = 0
  71.         if hero_y < -10:
  72.             hero_y = 630
  73.         elif hero_y > 640:
  74.             hero_y = 0
  75.  
  76.         if bill_x > hero_x:
  77.             bill_x -= 0.9
  78.         elif bill_x < hero_x:
  79.             bill_x += 0.9
  80.         if bill_y > hero_y:
  81.             bill_y -= 0.9
  82.         elif bill_y < hero_y:
  83.             bill_y += 0.9
  84.  
  85.    
  86.     if round(bill_y) in range(hero_y - 6, hero_y + 6) and round(bill_x) in range(hero_x - 6, hero_x + 6) and bill_hp > 0:
  87.         dead = True
  88.        
  89.     screen.blit(hero, (hero_y, hero_x))
  90.  
  91.     if bill_hp > 0:
  92.         screen.blit(bill, (bill_y, bill_x))
  93.  
  94.     if shot and not paused:
  95.         if bill_hp > 0:
  96.             if round(shot_y) in range(round(bill_y), round(bill_y + 20)) and round(shot_x) in range(round(bill_x), round(bill_x + 28)):
  97.                 bill_hp -= 1
  98.                 shot = False
  99.                 shotmoves = 0
  100.            
  101.         dif_x = target_y - shot_x
  102.         dif_y = target_x - shot_y
  103.        
  104.         shot_x += dif_x / 24
  105.         shot_y += dif_y / 24
  106.        
  107.         shotmoves += 1
  108.  
  109.     if shot:
  110.         screen.blit(bullet, (shot_y, shot_x))
  111.  
  112.     if dead:
  113.         pygame.mixer.music.pause()
  114.         pygame.mixer.music.rewind()
  115.        
  116.         if not death_sound_played:
  117.             death_sound.play()
  118.             death_sound_played = True
  119.            
  120.         screen.fill((255, 255, 255))
  121.         screen.blit(gameover, (0, 0))
  122.  
  123.         for event in pygame.event.get():
  124.             if event.type == pygame.QUIT:
  125.                 sys.exit()
  126.            
  127.             elif event.type == KEYDOWN and event.key == K_RETURN:
  128.                 hero_y = 304 # hero y axis
  129.                 hero_x = 228 # hero x axis
  130.  
  131.                 bill_x = random.randint(0, 480)
  132.                 bill_y = random.randint(0, 640)
  133.  
  134.                 moveup = False
  135.                 movedown = False
  136.                 moveright = False
  137.                 moveleft = False
  138.  
  139.                 shot = False
  140.                 death_sound_played = False
  141.                 rate = 60
  142.                 bill_hp = 100
  143.                
  144.                 pygame.mixer.music.unpause()
  145.                 dead = False
  146.  
  147.     if paused:
  148.         screen.blit(pausescreen, (0, 0))
  149.  
  150.     screen.blit(cursor, (cursor_x - 8, cursor_y - 8))
  151.        
  152.  
  153.     pygame.display.update()
  154.    
  155.     for event in pygame.event.get():
  156.         if event.type == pygame.QUIT:
  157.             sys.exit()
  158.  
  159.         elif event.type == KEYDOWN and event.key == K_RETURN:
  160.             if paused:
  161.                 paused = False
  162.             elif not paused:
  163.                 paused = True
  164.  
  165.         elif event.type == MOUSEBUTTONDOWN and not paused:
  166.             shot_sound.play()
  167.             target_x, target_y = pygame.mouse.get_pos()
  168.             shot_x, shot_y = hero_x + 6, hero_y + 8
  169.             shotmoves = 0
  170.             shot = True
  171.            
  172.            
  173.         elif event.type == KEYDOWN and not paused:
  174.             if event.key == K_UP or event.key == K_w:
  175.                 moveup = True
  176.             elif event.key == K_DOWN or event.key == K_s:
  177.                 movedown = True
  178.             elif event.key == K_RIGHT or event.key == K_d:
  179.                 moveright = True
  180.             elif event.key == K_LEFT or event.key == K_a:
  181.                 moveleft = True
  182.  
  183.             if event.key == K_SPACE:
  184.                 rate = 150
  185.  
  186.         elif event.type == KEYUP:
  187.             if event.key == K_UP or event.key == K_w:
  188.                 moveup = False
  189.             elif event.key == K_DOWN or event.key == K_s:
  190.                 movedown = False
  191.             elif event.key == K_RIGHT or event.key == K_d:
  192.                 moveright = False
  193.             elif event.key == K_LEFT or event.key == K_a:
  194.                 moveleft = False
  195.  
  196.             if event.key == K_SPACE:
  197.                 rate = 60
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement