To make things less complicated for your program, you will need some classes for your balloon and your screws. The first class, which will be for your player, will look like this, assuming you only move left to right:
class Player(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = pygame.image.get_rect()
self.rect.top, self.rect.left = location
This piece of code will make your balloon a sprite, ready for detection for collisions. Your screws' class will look similar, only with a extra move
and location
function and speed
in the __init__
part of the class:
class Screws(pygame.sprite.Sprite):
def __init__(self, image_file, left, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = pygame.image.get_rect()
self.rect.top = 50
self.rect.left = left
self.speed = speed
def move(self):
self.rect = self.rect.move(self.speed)
This class makes the screws sprites, also ready for detection. This finishes the classes section. Now to the grouping of these sprites and etc.:
balloon = Player('/users/Gaming/Desktop/rsz_baloon.png', [a, b])
screw = Screws('/users/Gaming/Desktop/rsz_screw_png3029.png', random.randint(0, <ScreenSize>), c)
ScrewGroup = pygame.sprite.Group()
Once again, the variables are changeable but the higher c
is, the faster the screws will fall. a
and b
will decide the location of the balloon, and random.randint()
will decide the location of your screws. self.rect.top
is a way to find the location by using it as the location of the rect
's "top" side. In this case it stays the same. Same for, self.rect.left
but it is the location of the rect
's "left" side. Now going to the moving part of the balloon, to avoid excessive presses of the keys UP and DOWN (and tiredness), add these line of code right after:
delay = 100
interval = 50
pygame.key.set_repeat(delay, interval)
on = True
screwy = 0
delay
is the amount of milliseconds between each KEYDOWNis activated and interval is amount of milliseconds to wait until beginning the repeated KEYDOWN's. This will help the user by making him just hold down the LEFT of RIGHT arrow key, and the balloon will continue going in the desired direction. The on
and screwy
variable will be discussed in the next section. Next, the while loop is almost there:
while True:
screen.blit(balloon.image, balloon.rect)
while int(screwy - 1) > -1:
screen.blit(screw.image, screw.rect)
pygame.display.flip()
The second while loop spawns as many screws as it can as long the value of screwy
- 1 is less than -1 (negative 1). This also flips the screen to avoid any "tracks" left on the screen. Now to the moving part of the balloon:
for event in pygame.event.get():
#Remember to do this : from pygame.locals import *#
if event.type == QUIT:
on = False
#Remember to import sys!#
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key = K_LEFT:
balloon.rect.left -= 30
elif event.key == K_RIGHT:
#This can only work if (width_of_the_picture - a) is equal to 30#
balloon.rect.left += int(width_of_the_picture - a)
This will allow you to move the balloon (you can hold the key down to kepp the balloon moving like in a real video game). Next will be the continued spawning of the screws:
if screwy < 10:
ScrewGroup.append(Screws('/users/Gaming/Desktop/rsz_screw_png3029.png', random.randint(0, b), [0, 15]))
screwy += 1
This will spawn screws at random places (same self.rect.top
value to make it look realistic). b
will be equal to the width of your screen in this case. Finally, the sprite detection:
if pygame.sprite.spritecollide(balloon, ScrewGroup, True):
on = False
sys.exit()
pass
This detects if the balloon has collided with the screws. If that is true, well you can decide. You could just exit the while loop and exit the program, which is one way to do it. But if you plan on doing something like print 'Game Over!
, add that line before doing on = False
/sys.exit()
which will exit the loop/program immediately. You will need to re-blit the images and allow the exit of the screen to be smooth (pygame.quit
):
screen.fill([255, 255, 255])
screen.blit(balloon.image, balloon.rect)
while int(screwy - 1) > -1:
screen.blit(screw)
pygame.display.flip()
pygame.quit()
Remember to put the pygame.quit()
outside the while loop or the screen will immediately disappear. Of course, make some code to prevent the balloon from exiting the screen. Changing the KEYDOWN section to this should do it:
elif event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
if int(balloon.rect.left) - 30 < 0:
pass
elif int(balloon.rect.left) - 30 >= 0:
balloon.rect.left -= 30
elif event.key == K_RIGHT:
if int(balloon.rect.left) + (<Width_OF_Balloon> - a) > <Width_OF_Screen>:
pass
elif int(balloon.rect.left) + (<Width_OF_Balloon> - a) <= <Width_OF_Screen>:
#This can only work if (<Width_OF_Balloon> - a) is equal to 30#
baloon.rect.left + (<Width_OF_Balloon> - a)
If going left/right once makes the balloon partly leaves the screen, the program will not allow the balloon to go left/right by doing nothing with pass
. This should improve your program significantly and answer your question. I hope this helps you!