SHARE
TWEET

Ari's Game

a guest May 22nd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Libraries
  2. import pygame
  3. import time
  4. import random
  5.  
  6. pygame.init()
  7.  
  8. display_width=600
  9. display_height=400
  10.  
  11. # RGB colors
  12. black=(0,0,0)
  13. white=(255,255,255)
  14. red=(255,0,0)
  15. green=(0,255,0)
  16. blue=(0,0,255)
  17. laser_color=(34,255,0)
  18.  
  19. stormtrooper_width=61
  20.  
  21. # Game display windows
  22. gameDisplay = pygame.display.set_mode((display_width,display_height))
  23.  
  24. # Title
  25. pygame.display.set_caption("Stormtrooper Dodging Lasers")
  26.  
  27. # Clock
  28. clock = pygame.time.Clock()
  29.  
  30. # Image of stormtrooper
  31. StormtrooperImg = pygame.image.load("Stormtrooper.png")
  32.  
  33. # Displays scoreboard
  34. def things_dodged(count):
  35.     font = pygame.font.SysFont(None,40)
  36.     text = font.render("Score "+str(count),True,black)
  37.     gameDisplay.blit(text,(20,20))
  38.  
  39. # Displays the stormtrooper
  40. def stormtrooper(x,y):
  41.     gameDisplay.blit(StormtrooperImg,(x,y))
  42.  
  43. # Displays lasers
  44. def things(thingx,thingy,thingw,thingh,color):
  45.     color = laser_color
  46.     pygame.draw.rect(gameDisplay,color,[thingx,thingy,thingw,thingh])
  47.  
  48. # Creates text
  49. def text_objects(text,font):
  50.     textSurface=font.render(text, True, red)
  51.     return textSurface, textSurface.get_rect()
  52.  
  53. # Displays text
  54. def message_display(text):
  55.     largeText = pygame.font.Font('freesansbold.ttf',35)
  56.     TextSurf, TextRect = text_objects(text, largeText)
  57.     TextRect.center = ((display_width/2),(display_height/2))
  58.     gameDisplay.blit(TextSurf, TextRect)
  59.     pygame.display.update()
  60.     time.sleep(2)
  61.     game_loop()
  62.  
  63. # Displays "You Crashed"
  64. def crash():
  65.     message_display('You Died. The Empire Expected More of You.')
  66.  
  67. def game_loop():
  68.     x=(display_width*0.48)
  69.     y=(display_height*0.79)
  70.  
  71.     x_change=0
  72.     y_change=0
  73.    
  74.     thing_startx=random.randrange(0,display_width)
  75.     thing_starty=-600
  76.     thing_speed=5
  77.     thing_width=5
  78.     thing_height=100
  79.  
  80.     dodged=0
  81.  
  82.     gameExit=False
  83.  
  84.     while not gameExit:
  85.        
  86.         for event in pygame.event.get():
  87.             if event.type==pygame.QUIT:
  88.                 pygame.quit()
  89.                 quit()
  90.  
  91.             if event.type==pygame.KEYDOWN:
  92.                 if event.key==pygame.K_LEFT:
  93.                     x_change=-5
  94.                 elif event.key==pygame.K_RIGHT:
  95.                     x_change=5
  96.                 elif event.key==pygame.K_DOWN:
  97.                     y_change=5
  98.                 elif event.key==pygame.K_UP:
  99.                     y_change=-5
  100.  
  101.             if event.type==pygame.KEYUP:
  102.                 if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
  103.                     x_change=0
  104.                 elif event.key==pygame.K_UP or event.key==pygame.K_DOWN:
  105.                     y_change=0
  106.  
  107.         x+=x_change
  108.         y+=y_change
  109.                
  110.         gameDisplay.fill(white)
  111.  
  112.         #things(thingx,thingy,thingw,thingh,color)
  113.         things(thing_startx,thing_starty,thing_width,thing_height,black)
  114.         thing_starty+=thing_speed
  115.         stormtrooper(x,y)
  116.         things_dodged(dodged)
  117.  
  118.         if x>display_width-stormtrooper_width or x<0:
  119.             crash()
  120.         if thing_starty>display_height:
  121.             thing_starty=0-thing_height
  122.             thing_startx=random.randrange(0,display_width)
  123.             dodged+=1
  124.            
  125.             if dodged % 2 == 0:
  126.                 thing_speed += 1
  127.  
  128.         if y<thing_starty+thing_height:
  129.             print('y crossover')
  130.  
  131.             if x>thing_startx and x<thing_startx+thing_width or x+stormtrooper_width>thing_startx and x+stormtrooper_width<thing_startx+thing_width:
  132.                 print('x crossover')
  133.                 crash()
  134.        
  135.         pygame.display.update()
  136.         clock.tick(60)
  137.  
  138. game_loop()
  139. pygame.quit()
  140. quit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top