Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.15 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. from pygame.locals import *
  5.  
  6. ### Initialize constants
  7.  
  8. WIDTH = 1000
  9. HEIGHT = 800
  10. BOOST_BAR_WIDTH = 150
  11. BOOST_BAR_HEIGHT = 20
  12. BOOST_BAR_COLOR = (0, 255, 0)
  13. BOOST_BAR_COLOR_DEPLETED = (200, 0, 0)
  14. BOOST_BAR_LINE_COLOR = (50, 50, 50)
  15. BACKGROUND_COLOR = (180, 200, 240)
  16. PAUSE_BACKGROUND_COLOR = (255, 255, 255)
  17. FRAMERATE = 60
  18. DEVILSPEED = 1.2
  19. MAX_POINTS = 10
  20. MAX_SPEED = 10
  21. BOOST_SPEED = 20
  22. MIN_BOOST = 20
  23. MAX_BOOST = 50
  24. MAIN_COLOR = (255, 0, 0)
  25. STAR_COLOR = (255, 255, 255)
  26.  
  27. pygame.init()
  28.  
  29. mainfont = pygame.font.SysFont('Helvetica', 25)
  30.  
  31. mainsurf = pygame.display.set_mode((WIDTH, HEIGHT))
  32.  
  33. cookieimage = pygame.image.load('cookie.png')
  34.  
  35. cookiewidth, cookieheight = cookieimage.get_size()
  36.  
  37. rocketspeed = 1
  38.  
  39. boostmode = False
  40. boostleft = MAX_BOOST
  41.  
  42. devils = []
  43.  
  44. cookiex = random.randint(0, WIDTH)
  45. cookiey = random.randint(0, HEIGHT)
  46.  
  47. score = 0
  48.  
  49. paused = False
  50. gamewon = False
  51. gamelost = False
  52.  
  53. devilgroup = pygame.sprite.Group()
  54.  
  55. class Rocket(pygame.sprite.Sprite):
  56.     def __init__(self):
  57.         super().__init__()
  58.  
  59.         self.image = pygame.image.load("rocket.png")
  60.         self.rect = pygame.rect.Rect((WIDTH / 2, HEIGHT / 2), self.image.get_size())
  61.  
  62.     def draw(self):
  63.         mainsurf.blit(self.image, self.rect)
  64.  
  65.  
  66. class Devil(pygame.sprite.Sprite):
  67.     def __init__(self):
  68.         super().__init__()
  69.         self.image = pygame.image.load("devil.png")
  70.  
  71.         side = random.randint(0, 3)
  72.  
  73.         if side == 0:  # Top
  74.             x = random.randint(0, WIDTH)
  75.             y = 0
  76.         elif side == 1:  # Right
  77.             x = WIDTH
  78.             y = random.randint(0, HEIGHT)
  79.         elif side == 2:  # Bottom
  80.             x = random.randint(0, WIDTH)
  81.             y = HEIGHT
  82.         elif side == 3:  # Left
  83.             x = 0
  84.             y = random.randint(0, HEIGHT)
  85.  
  86.         self.rect = pygame.rect.Rect((x, y), self.image.get_size())
  87.  
  88.  
  89.         devilgroup.add(self)
  90.  
  91.     def draw(self):
  92.         mainsurf.blit(self.image, self.rect)
  93.  
  94. class StarField(pygame.Surface):
  95.     def __init__(self):
  96.         super().__init__((WIDTH, HEIGHT))
  97.  
  98.         x = 0
  99.         while x < WIDTH:
  100.             y = 0
  101.             while y < HEIGHT:
  102.                 if random.randint(0, 100) == 0:
  103.                     self.set_at((x, y), STAR_COLOR)
  104.                 y += 1
  105.             x += 1
  106.  
  107. def winscreen():
  108.     mainsurf.fill(BACKGROUND_COLOR)
  109.  
  110.     textsurf = mainfont.render('YOU WON', True, MAIN_COLOR)
  111.  
  112.     mainsurf.blit(textsurf, (WIDTH / 2, HEIGHT / 2))
  113.  
  114. def losescreen():
  115.     mainsurf.fill(BACKGROUND_COLOR)
  116.  
  117.     textsurf = mainfont.render('YOU LOST', True, MAIN_COLOR)
  118.  
  119.     mainsurf.blit(textsurf, (WIDTH / 2, HEIGHT / 2))
  120.  
  121. def pausescreen():
  122.     mainsurf.fill(PAUSE_BACKGROUND_COLOR)
  123.  
  124.     textsurf = mainfont.render("PAUSED", True, MAIN_COLOR)
  125.  
  126.     mainsurf.blit(textsurf, (WIDTH / 2, HEIGHT / 2))
  127.  
  128. def showscore(score):
  129.     textsurf = mainfont.render(str(score), True, MAIN_COLOR)
  130.     mainsurf.blit(textsurf, (WIDTH - 50, 50))
  131.  
  132. def showboostbar(boostleft):
  133.     width = (boostleft / MAX_BOOST) * BOOST_BAR_WIDTH
  134.  
  135.     if boostleft < MIN_BOOST:
  136.         color = BOOST_BAR_COLOR_DEPLETED
  137.     else:
  138.         color = BOOST_BAR_COLOR
  139.  
  140.     # The x position of the bar is the width of the screen, minus the width of the bar
  141.     # (so it doesn't go off the screen), minus another 30px so it's not right up
  142.     # against the edge of the screen.
  143.     barx = WIDTH - BOOST_BAR_WIDTH - 30
  144.  
  145.     # y position is 20px (just a little bit of padding so it's not right at the top)
  146.     bary = 20
  147.  
  148.     # The width of the bar is the percentage of boost that we have left, times the width
  149.     # of the full bar. So if we're at 50% boost and the full bar is 150 pixels, then
  150.     # we display a bar 75 pixels wide.
  151.     barwidth = (boostleft / MAX_BOOST) * BOOST_BAR_WIDTH
  152.  
  153.     # The height is just a constant
  154.     barheight = BOOST_BAR_HEIGHT
  155.  
  156.     # Time to draw the bar!
  157.     pygame.draw.rect(mainsurf, color, (barx, bary, barwidth, barheight))
  158.  
  159.     # Now, let's make the line that marks the minimum boost level
  160.  
  161.     # To compute the x position for the minimum boost line, we take 3 steps:
  162.     #   - First, we divide the minimum boost over the maximum boost, to find out
  163.     #     how far along the bar we should draw the line (as a ratio). For example,
  164.     #     if MIN_BOOST is 20 and MAX_BOOST is 100, then the ratio would be 0.2.
  165.     #   - Next, we multiply this ratio by the width of the bar to get the actual
  166.     #     number of pixels.
  167.     #   - Finally, we add the x position of the bar to push the whole thing to the
  168.     #     right so it lines up with the bar.
  169.     linex = ((MIN_BOOST / MAX_BOOST) * BOOST_BAR_WIDTH) + barx
  170.  
  171.     # Line y position is the same as bar
  172.     liney = bary
  173.  
  174.     # Line is 2 pixels wide
  175.     linewidth = 2
  176.  
  177.     # Line is same height as bar
  178.     lineheight = barheight
  179.  
  180.     pygame.draw.rect(mainsurf, BOOST_BAR_LINE_COLOR, (linex, liney, linewidth, lineheight))
  181.  
  182. starfield = StarField()
  183.  
  184. rocket = Rocket()  
  185. # Create first devil
  186. devils.append(Devil())
  187. while True:
  188.     event = pygame.event.poll()    
  189.  
  190.     if event.type == QUIT:
  191.         exit()
  192.  
  193.     if gamewon:
  194.         winscreen()
  195.         pygame.display.update()
  196.         continue
  197.  
  198.     if gamelost:
  199.         losescreen()
  200.         pygame.display.update()
  201.         continue
  202.  
  203.     if event.type == KEYUP and event.key == K_ESCAPE:  # If the player just pressed escape...
  204.         paused = not paused  # Flip paused state
  205.  
  206.     # If the game is paused, display the pause screen and skip everything else
  207.     if paused:
  208.         pausescreen()
  209.         pygame.display.update()
  210.         continue
  211.  
  212.     keyspressed = pygame.key.get_pressed()
  213.  
  214.     ### Update rocket speed
  215.  
  216.     # If the player pressed "b" and we have enough boost to start, then go into boost mode
  217.     if event.type == KEYDOWN and event.key == K_b and boostleft > MIN_BOOST:
  218.         boostmode = True
  219.         rocketspeed = 20
  220.  
  221.     if event.type == KEYUP and event.key == K_b:  # Boost mode over
  222.         boostmode = False
  223.         rocketspeed = 1
  224.  
  225.     if boostmode:
  226.         # We're in boost mode
  227.  
  228.         boostleft -= 1  # Deplete the boost counter
  229.         if boostleft <= 0:
  230.             boostmode = False
  231.             rocketspeed = 1
  232.     else:
  233.         # We're not in boost mode
  234.  
  235.         # Replenish boost counter
  236.         if boostleft <= MAX_BOOST:
  237.             boostleft += 0.25
  238.  
  239.         # If space is held down, increase rocket speed (but don't let the speed go over the max)
  240.         if keyspressed[K_SPACE]:
  241.             rocketspeed += .25
  242.             if rocketspeed > MAX_SPEED:
  243.                 rocketspeed = MAX_SPEED
  244.  
  245.         # If shift is held down, decrease rocket speed (but don't let the speed go under 0)
  246.         if keyspressed[K_LSHIFT] or keyspressed[K_RSHIFT]:
  247.             rocketspeed -= 1
  248.             if rocketspeed < 0:
  249.                 rocketspeed = .1
  250.  
  251.     ### Update rocket position using the speed we just calculated
  252.  
  253.     if keyspressed[K_UP]:
  254.         rocket.rect.y -= rocketspeed
  255.  
  256.     if keyspressed[K_DOWN]:
  257.         rocket.rect.y += rocketspeed
  258.  
  259.     if keyspressed[K_LEFT]:
  260.         rocket.rect.x -= rocketspeed
  261.  
  262.     if keyspressed[K_RIGHT]:
  263.         rocket.rect.x += rocketspeed
  264.  
  265.     # If the rocket is now past the edge in any direction, move it back to the edge.
  266.     if rocket.rect.x < 0:
  267.         rocket.rect.x = 0
  268.  
  269.     if rocket.rect.x > WIDTH - rocket.rect.width:
  270.         rocket.rect.x = WIDTH - rocket.rect.width
  271.  
  272.     if rocket.rect.y < 0:
  273.         rocket.rect.y = 0
  274.  
  275.     if rocket.rect.y > HEIGHT - rocket.rect.width:
  276.         rocket.rect.y = HEIGHT - rocket.rect.width
  277.  
  278.     ### Update devil position
  279.  
  280.     i = 0
  281.     while i < len(devils): # For each devil...
  282.         # Get the current x and y position for this devil
  283.         devil = devils[i]
  284.  
  285.         oldx = devil.rect.x
  286.         oldy = devil.rect.y
  287.  
  288.         # Calculate the *new* x and y position for this devil
  289.         if devil.rect.x > rocket.rect.x:
  290.             devil.rect.x -= DEVILSPEED
  291.    
  292.         if devil.rect.x < rocket.rect.x:
  293.             devil.rect.x += DEVILSPEED
  294.      
  295.         if devil.rect.y > rocket.rect.y:
  296.             devil.rect.y -= DEVILSPEED
  297.      
  298.         if devil.rect.y < rocket.rect.y:
  299.             devil.rect.y += DEVILSPEED
  300.  
  301.         devilgroup.remove(devil)
  302.         collidingdevil = pygame.sprite.spritecollideany(devil, devilgroup)
  303.         devilgroup.add(devil)
  304.  
  305.  
  306.         if collidingdevil is not None:
  307.             devil.rect.x = oldx
  308.             devil.rect.y = oldy
  309.  
  310.         i += 1
  311.  
  312.  
  313.     ### We have the new positions for everything. Now, check for collisions and update the game in response
  314.  
  315.     # Create Pygame rectangle objects for the rocket and cookie. Rectangles are not directly displayed
  316.     # to the screen; they're just used for checking for collisions and other calculations.
  317.     cookie_rect = pygame.Rect((cookiex, cookiey), (cookiewidth, cookieheight))
  318.  
  319.     # Check if any of the rocket is colliding with any of the devils
  320.     i = 0
  321.     while i < len(devils):
  322.         devil = devils[i]
  323.  
  324.         if rocket.rect.colliderect(devil.rect):
  325.             gamelost = True
  326.             break
  327.         i += 1
  328.  
  329.     # If the rocket collided with one of the devils, we lost, so we go back to the top of the game
  330.     # loop to display the lose screen.
  331.     if gamelost:
  332.         continue
  333.  
  334.     # Check if the rocket is colliding with the cookie
  335.     if rocket.rect.colliderect(cookie_rect):
  336.         cookiex = random.randint(0, WIDTH)
  337.         cookiey = random.randint(0, HEIGHT)
  338.         score += 1
  339.         devils.append(Devil())
  340.         # spawndevil(devils)
  341.  
  342.     if score >= MAX_POINTS:
  343.         gamewon = True
  344.     else:
  345.         ### The game state has been updated. Time to render!
  346.  
  347.         # mainsurf.fill(BACKGROUND_COLOR)
  348.  
  349.         mainsurf.blit(starfield, (0, 0))
  350.  
  351.         showscore(score)
  352.         showboostbar(boostleft)
  353.  
  354.         # Render rocket and cookie
  355.         rocket.draw()
  356.         mainsurf.blit(cookieimage, (cookiex, cookiey))
  357.  
  358.         # Render devils
  359.         i = 0
  360.         while i < len(devils):
  361.             devil = devils[i]
  362.             devil.draw()
  363.             i += 1
  364.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement