Advertisement
Guest User

Balloons in Pygame

a guest
Feb 22nd, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. # Import the needed libraries
  2. import pygame, random
  3.  
  4. # Set up a clock
  5. clock = pygame.time.Clock()
  6.  
  7. # Set up your game display
  8. windowHeight, windowWidth = 600, 1000
  9. gameDisplay = pygame.display.set_mode((windowWidth, windowHeight))
  10.  
  11. # Make a list called allBalloons.
  12. allBalloons = []
  13.  
  14. # Start a for loop that will go around 25 times. (This is the number of balloons you will get)
  15. for i in range(25):
  16.     # Set up 3 variables for red, green and blue. These will each have a random value between 0 and 255.
  17.     redAmount     = random.randrange(0, 255)
  18.     greenAmount   = random.randrange(0, 255)
  19.     blueAmount    = random.randrange(0, 255)
  20.     # Create a variable called balloonColour and make it equal to the red, green and blue values.
  21.     balloonColour = [redAmount, greenAmount, blueAmount]
  22.     # Set up 2 variables for balloonXPos and balloonYPos. These will each have a random value between 0 and what you set the width and height as.
  23.     balloonXPos   = random.randrange(50, windowWidth-50)
  24.     balloonYPos   = random.randrange(350, windowHeight-50)
  25.     # Set up a variable called balloonSize. This will have random value between 30 and 50.
  26.     balloonSize   = random.randrange(30, 50)
  27.     # Append all these values to the allBalloons list.
  28.     allBalloons.append([balloonColour,balloonXPos,balloonYPos,balloonSize, i])
  29.  
  30. # Fetch the collisionCheck function from another game.
  31. def collisionCheck(x,y,w,h,x2,y2,w2,h2, balloonNumber):
  32.     if (x < (x2 + w2) and (x + w) > x2 and y < (y2 + h2) and (h + y) > y2):
  33.       print ("Clicked on balloon number", balloonNumber)
  34.       #del allBalloons[balloonNumber]  #<---- THIS IS WHERE MY PROBLEM IS
  35.  
  36. # Create a function called gameLoop.
  37. def gameLoop():
  38.     # Set a variable to true and then use that to start a while loop going.
  39.     gameRunning = True
  40.     while gameRunning == True:
  41.         # You need the for event in pygame.event.get() code here. Pull it from a previous game.
  42.         for event in pygame.event.get():
  43.             if event.type == pygame.QUIT:
  44.                 quit()
  45.             if event.type == pygame.MOUSEBUTTONUP:
  46.                 #Call the collision check function here on all the balloons.
  47.                 for i in range(len(allBalloons)):
  48.                     collisionCheck(allBalloons[i][1], allBalloons[i][2], allBalloons[i][3], allBalloons[i][3], mouseX, mouseY, 1, 1, i)
  49.  
  50.  
  51.         # Fill the game display in here with a colour. You can use the RGB values instead of defining colours.
  52.         gameDisplay.fill([255, 255, 255])
  53.  
  54.         # Create a for loop that uses all the values from allBalloons to create some circles. (pygame.draw.circle)
  55.         for i in range(len(allBalloons)):
  56.             oneBalloon = pygame.draw.circle(gameDisplay, allBalloons[i][0], (allBalloons[i][1],allBalloons[i][2]), allBalloons[i][3])    
  57.             # Make the balloons float up by minusing 3 from its Y Pos value.
  58.             allBalloons[i][2] = allBalloons[i][2] - 1
  59.  
  60.         # Create 2 variables called mouseX and mouseY and use the pygame.mouse.get_pos() feature to set them to the mouses location.
  61.         mouseX = pygame.mouse.get_pos()[0]
  62.         mouseY = pygame.mouse.get_pos()[1]
  63.        
  64.         # Update the screen and tick the clock.
  65.         pygame.display.update()
  66.         clock.tick(30)
  67.  
  68. # Call the gameLoop function here.
  69. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement