Okay, i just started using pygame a week ago but i think i understand the basics. My game is really simple, move the balloon left and right to dodge the incoming screws. I successful made the balloon move left and right, but i'm very unfamiliar with classes and i dont know any other way to rapidly spawn multiple screws on the screen. Any help would be greatly appreciated.
Heres my code:
import pygame
from pygame.locals import *
import sys
import time
pygame.init()
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption('Baloon Pop')
baloon_size = (70,70)
white = (255,255,255)
cyan = (0,255,255)
red = (255,0,0)
screen.fill(cyan)
baloon = pygame.image.load('/users/Gaming/Desktop/rsz_baloon.png')
screw = pygame.image.load('/users/Gaming/Desktop/rsz_screw_png3029.png')
FPS = 30
fps_time = pygame.time.Clock()
baloonX = 280
baloonY = 500
import random
screwX = random.randint(0,600)
screwY = 20
LEFT = "left"
RIGHT = "right"
movement = "down"
while True:
screwY = screwY+10
screen.fill(cyan)
screen.blit(screw, (screwX,screwY))
screen.blit(baloon, (baloonX,baloonY))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
baloonX = baloonX+30
if baloonX >= 580:
baloonX = baloonX -30
elif event.key == K_LEFT:
baloonX = baloonX -30
if baloonX <=-30:
baloonX = baloonX+30
pygame.display.update()
fps_time.tick(FPS)
First of all, I would recommend using a class to control your player. This makes drawing and detecting collisions easier. A simple way to do this is:
Then, a similar class to control your enemy.
Here, time_passed is your Clock() object's tick value. screen is your pygame.display surface.
Now that you have your enemy objectified, you can create a list to store the instances of the enemy class.
As I've suggested, using this mechanism can smooth your game quite a bit. Using an increment on each KEYDOWN event is not recommended.
Create a list to store the enemy instances and create player instance:
Moving on to the creation of random enemies. If you need to create, say a screw (that is, a Enemy()) every n loops, you can create a flag that activates in that interval. You can have a variable 'count', that decides this. Initial value can be 0 and then increase it at every loop by 1. (Example: if you want an enemy spawning every 5 loops, replace n by 5.)
That is, spawn an enemy at a random place in the screen, and have them move down. 0.1 is just a sample speed.
Now, to the loop section... Your for event loop should have a KEYDOWN and a KEYUP check, to ensure single keypresses. Here, Player1 is the name of the Player class instance.
Add checks to ensure that the player does not get out of the screen.
The player's movement is now complete, to the requirements of this game.
Call the update() function of the Player, the enemies and flip the screen.
Now, add the collision checks to complete your game. Additionally, you can remove the instances that have passed out of the screen to save memory.