Why is my ship not shooting bullets? - python

I have made a game using Python and Pygame. My ship moves around but I am having trouble making it shoot bullets. I have defined a function called shootBullets() but it is not working. And now, if I press the space bar, my ship moves. It is only supposed to move when I press the left or right arrow keys. I want my ship to shoot bullets towards the bottom of the screen when I press the space bar. here is my code:
import pygame,sys
from pygame.locals import *
pygame.init()
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
bright_blue = (0, 135, 255)
yellow = (255,242,0)
ship_body = (33, 117, 243)
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("Battleship")
gameExit = False
background = pygame.image.load("Sky Background.png")
bulletImg = pygame.image.load("Bullet.png")
bulletY = 80
def shootBullets():
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_SPACE:
bulletY += 5
screen.blit(bulletImg,(247,bulletY))
pygame.key.set_repeat(50,50)
ship_points = [ [100, 50], [180, 95], [320, 95], [400, 50], [250, 35] ]
x = 0
y = 0
while not gameExit:
for event in pygame.event.get():
if event.type == QUIT:
gameExit = True
if event.type == KEYDOWN:
if event.key == pygame.K_LEFT: x = -5
if event.key == pygame.K_RIGHT: x = 5
for point in ship_points:
point[0] += x
for point in ship_points:
if point[0] <= 0 or point[0] >= 500:
gameExit = True
shootBullets()
screen.fill(black)
screen.blit(background, (0,0))
ship = [
pygame.draw.polygon(screen, ship_body, ship_points),
pygame.draw.polygon(screen, black, ship_points, 1)]
pygame.display.update()
pygame.quit()
quit()

In your main loop you are only checking for left and right, not for space. You check in the function shootBullets whether space is pressed, but that is too late, shootBullets will never be executes (actually it will get executed if the for event in get() loop is somehow exited but that is not what you want).
Instead do something like:
while not gameExit:
for event in pygame.event.get():
if event.type == QUIT:
gameExit = True
if event.type == KEYDOWN:
if event.key == pygame.K_LEFT:
move_left()
if event.key == pygame.K_RIGHT:
move_right()
if event.key == pygame.SPACE:
shootBullet()
[...]

Related

After the screen is blitted how to make a rectangle when there is already an existing moving rectangle?

I am making this game in PyGame and I have a moving rectangle, So I want to make another rectangle which is drawn to the screen when I press the Space Bar. So I have tried adding the if statement but it doesn't work as the screen fills as soon as the Rectangle is drawn. Can anyone tell me how to draw the rectangle after the screen is filled with a color?
Here is my code
import pygame
from pygame.locals import *
pygame.init()
screenwidth = 1200
screenheight = 500
screen = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption('Bullepacito')
def _rect():
run = False
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_SPACE:
pygame.draw.rect(screen, (255, 0, 255), pygame.Rect(400, 400, 20, 20))
def gameloop():
run = False
shooterx = 350
shootery = 350
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
shooterx -= 5
if event.key == pygame.K_RIGHT:
shooterx += 5
screen.fill((30, 30, 30))
pygame.draw.rect(screen, (255, 0, 255), pygame.Rect(shooterx, shootery, 50, 50))
# Code to draw the rectangle on key press
pygame.display.update()
gameloop()
Set a boolean state (draw_rect) when SPACE is pressed and draw the rectangle depending on the state:
import pygame
from pygame.locals import *
pygame.init()
screenwidth = 1200
screenheight = 500
screen = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption('Bullepacito')
def gameloop():
run = False
shooterx = 350
shootery = 350
draw_rect = False
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
shooterx -= 5
if event.key == pygame.K_RIGHT:
shooterx += 5
if event.key == pygame.K_SPACE:
draw_rect = True
screen.fill((30, 30, 30))
pygame.draw.rect(screen, (255, 0, 255), pygame.Rect(shooterx, shootery, 50, 50))
if draw_rect:
pygame.draw.rect(screen, (255, 0, 255), pygame.Rect(400, 400, 20, 20))
pygame.display.update()
gameloop()

Confused with movement in pygame

I am trying to make a small game in pygame. The player is the left hand red rectangle (if you run it) and I am just trying to make the rocket variable move to the left quickly. Whenever I run the program if I press any key or move my mouse it moves the rocket.
Here is the code:
import pygame,sys,random
pygame.init()
pygame.key.set_repeat(1, 100)
size=width,height=1280,830
screen = pygame.display.set_mode(size)
black = [0, 0, 0]
white = [255, 255, 255]
sky_blue = ((0,255,255))
red = ((255,0,0))
font = pygame.font.SysFont("Arial",14)
rocket=pygame.Rect(1200,350,150,50)
player= pygame.Rect(250,350,250,50)
hull=100
player_speed=100
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type==pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
player_speed=player_speed+100
if event.key == pygame.K_LEFT:
player_speed=player_speed-100
if event.key == pygame.K_UP:
player.top=player.top-50
if event.key == pygame.K_DOWN:
player.top=player.top+50
if player_speed>1000:
player_speed=1000
if player_speed<100:
player_speed=100
if player.top<0:
player.top=0
elif player.bottom>height:
player.bottom=height
#rocket code
if player.colliderect(rocket):
hull=hull-100
if player_speed>99:
rocket.right=rocket.right-5
screen.fill(sky_blue)
pygame.draw.rect(screen,red,player)
pygame.draw.rect(screen,red,rocket)
renderedText = font.render("Speed: "+str(player_speed),1,black)
screen.blit(renderedText, (width/2+50,10))
if hull<1:
renderedText = font.render("GAME OVER",1,black)
screen.blit(renderedText, (width/2+500,500))
pygame.display.flip()
pygame.time.wait(10)
You indentation is incorrect which is causing a logic error.
if player_speed>1000:
...
rocket.right=rocket.right-5
The code between these lines need to be one indent lower. Else this code will only be run when there is a event. Since the for loop will skip execution of any code within if there is no events.

Draw rectangle on key click

I want to draw a rectangle with a button click , but the problem is with every loop it updates the display , display is filled with a color
So the rectangle is only seen for a brief period of time.
How to solve this.
while not run:
display.fill((130,190,255) )
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
p_x -= 30
if event.key == pygame.K_p:
p_x += 30
if event.key == pygame.K_g:
pygame.draw.rect(display , (0,0,0) ,((p_x + 25),1309 ,20,30))
You have to draw the rectangle in the application loop. For example add a new rectangle to a list when g is pressed. Draw each rectangles in the list in the application loop
rectangles = []
exit_game = False
while not exit_game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
p_x -= 30
if event.key == pygame.K_p:
p_x += 30
if event.key == pygame.K_g:
rectangles.append((p_x + 25, 1309, 20, 30))
display.fill((130,190,255))
pygame.draw.rect(display, (0,0,0), (p_x + 25, 1309, 20, 30), 1)
for rect in rectangles:
pygame.draw.rect(display, (0,0,0), rect)
pygame.display.update()

Can someone please tell me where am I wrong in this python code?

Here's a simple pygame code where I inserted a screen, a pink rectangle and tried moving it.
The rectangle in the pygame window isn't moving.
Which means the code inside '**' isn't working.
How do I solve that?
import pygame, sys
pygame.init()
width = 800
height = 600
pink = (244,133,227)
player_pos = [400, 300]
player_size = 50
screen = pygame.display.set_mode((width,height))
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
** if event.type == pygame.KEYDOWN:
x = player_pos[0]
y = player_pos[1]
if event.type == pygame.K_LEFT:
x -= player_size
elif event.type == pygame.K_RIGHT:
x += player_size
player_pos = [x, y]
screen.fill((0,0,0)) **
pygame.draw.rect(screen, pink, (player_pos[0], player_pos[1], player_size, player_size))
pygame.display.update()
The key is stored in the key attribute, rather then the type attribute. See pygame.event:
if event.type== pygame.K_LEFT:
if event.key == pygame.K_LEFT:
See the example:
import pygame, sys
pygame.init()
width = 800
height = 600
pink = (244,133,227)
player_pos = [400, 300]
player_size = 50
screen = pygame.display.set_mode((width,height))
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
x = player_pos[0]
y = player_pos[1]
if event.key == pygame.K_LEFT:
x -= player_size
elif event.key == pygame.K_RIGHT:
x += player_size
player_pos = [x, y]
screen.fill((0,0,0))
pygame.draw.rect(screen, pink, (player_pos[0], player_pos[1], player_size, player_size))
pygame.display.update()

key event triggers and moving objects on screen

So I am making a game with the pygame module in python. The game is Breakout. One of the mechanics of the game is move the player left and right. How am I doing this is when the user presses the left or right arrow key, the player brick moves the left or right depending on what key is pressed, but the catch is that it if the player presses and holds the left or right button; the player brick will not continue to move... My question is how do I make the player brick continue to move instead of moving once when the key button is held down?!
here is my code
import pygame
pygame.init()
#colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
#the Brick
class goodbrick:
def __init__ (self, color):
self.color_scheme=color
##################X, Y L F
self.cordinates= [20, 450, 100, 0]
def move (self, x):
self.cordinates[0]+=x
def draw (self):
pygame.draw.rect(screen, self.color_scheme, self.cordinates, 0)
#class enemyBrick:
#the ball
#pygame stuff
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("BREAKOUT")
done= False
clock = pygame.time.Clock()
#init stuff
player1= goodbrick(GREEN)
#main loop
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT:
done=True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player1.move(-1)
if event.key == pygame.K_RIGHT:
player1.move(1)
elif event.type ==pygame.KEYUP:
if event.key == pygame.K_LEFT:
player1.move(-1)
print("yup")
if event.key == pygame.K_RIGHT:
player1.move(1)
#art
screen.fill(BLACK)
player1.draw()
#screent
pygame.display.flip()
clock.tick(60)
pygame.quit()
import pygame
pygame.init()
#colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
#the Brick
class goodbrick:
def __init__ (self, color):
self.color_scheme=color
##################X, Y L F
self.cordinates= [20, 450, 100, 0]
def move (self, x):
self.cordinates[0]+=x
def draw (self):
pygame.draw.rect(screen, self.color_scheme, self.cordinates, 0)
#class enemyBrick:
#the ball
#pygame stuff
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("BREAKOUT")
done= False
clock = pygame.time.Clock()
#init stuff
player1= goodbrick(GREEN)
#main loop
change = 0
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT:
done=True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
change = -1
if event.key == pygame.K_RIGHT:
change = 1
elif event.type ==pygame.KEYUP:
if event.key == pygame.K_LEFT:
change = 0
print("yup")
if event.key == pygame.K_RIGHT:
change = 0
player1.move(change)
#art
screen.fill(BLACK)
player1.draw()
#screent
pygame.display.flip()
clock.tick(60)
pygame.quit()

Categories