Pygame - shooting bullets - python

I have problem with my code, I started to make 2D tank game, the problem is the shooting bullets from the tank position.. Here's my code, check it out, I'm trying to figure it out like 3-4 hours.. I hope someone knows how to do it, thanks!:)
by the way, sorry for the creepy code, I'm new to Pygame :)
import pygame
pygame.init()
#---WINDOW----
display_width = 1000
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('powpypow')
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
background = pygame.image.load('background.png')
tank1 = pygame.image.load('tank1.png')
tankfire = pygame.image.load('tankfire.png')
shot = pygame.image.load('shot.png')
clock = pygame.time.Clock()
def tank(x,y):
gameDisplay.blit(tank1, (x,y))
x = (display_width * 0.10)
y = (display_height * 0.58)
x_change = 0
tank_width = 73
#---GAME LOOP----
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
x_change = -10
elif event.key == pygame.K_d:
x_change = 10
elif event.key == pygame.K_SPACE:
tank1 = pygame.image.load('tankfire.png')
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_SPACE:
tank1 = pygame.image.load('tank1.png')
x_change = 0
if x >= display_width - tank_width or x <= 0:
x = 0
if x > display_width / 2:
x = 0
x += x_change
gameDisplay.blit(background, (0,0))
tank(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()

If you are going to want to render bullets, you are going to need to keep track of them as they move across the screen. This will require some sort of list or group of bullets that you iterate through and draw, one at a time.
I recommend you have a look at a tutorial to help you switch to using Sprites in a group, rather than blitting each image onto the screen individually. You will find that it is much easier to track each object and adjust its position.
The Chimp tutorial from the Pygame website is pretty good: https://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html

Related

Pygame dinosaur image keeps duplicating

I'm trying to make a dinosaur game in python (like the offline dino game in chrome).
I want the dino to jump when space is pressed but when I press it,not only the image of dino is duped but it also doesn't come back.
import pygame
import time
pygame.init()
displayWidth = 700
displayHeight = 350
gameDisplay = pygame.display.set_mode((displayWidth,displayHeight))
pygame.display.set_caption("Dino-Run")
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
dinoimg = pygame.image.load("dino.png")
def dino(x,y):
gameDisplay.blit(dinoimg,(x,y))
def gameloop():
gameExit = False
x = (displayWidth * 0.005)
y = (displayHeight * 0.75)
y_change = 0
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
y_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
y_change = 0
y += y_change
dino(x,y)
pygame.display.update()
clock.tick(60)
Could someone please tell me how can I prevent the dino to dupe each time space is pressed and for the dino to come back to the ground.
You have to override everything before drawing new things to the screen.
Add this at the beginning of your loop:
gameDisplay.fill(color)

Pygame Blitting Error

I am relatively new in python and started coding in pygame. For some reason, when I am rendering some images on a surface, it doesn't blit the images. But when I minimize it and then open it again, it works fine. Is there any fix to this problem? Thanks in adavance.
Non-fullscreen error: https://youtu.be/q_2FT8OD7O4
Fullscreen error: https://youtu.be/9XKnhtN5s5k
My code, just in case if something's wrong in it:
import pygame
displayWidth = 1920
displayHeight = 1080
pygame.init()
gameDisplay = pygame.display.set_mode((displayWidth,displayHeight),pygame.FULLSCREEN)
pygame.display.set_caption('Road Fighters')
clock = pygame.time.Clock()
car = pygame.image.load('Car.png')
grassLeft = pygame.image.load('Grass Left.png')
grassRight = pygame.image.load('Grass Right.png')
blank = pygame.image.load('Left.png')
lining = pygame.image.load('Lining.png')
miniMap = pygame.image.load('Progress.png')
miniCar = pygame.image.load('Small Car.png')
road = pygame.image.load('Road.png')
def car(x,y):
gameDisplay.blit(car, (x,y))
def draw():
gameDisplay.blit(blank,(0,0))
gameDisplay.blit(miniMap,(670,0))
gameDisplay.blit(grassLeft,(720,0))
gameDisplay.blit(lining,(1020,0))
gameDisplay.blit(road,(1030,0))
gameDisplay.blit(lining,(1610,0))
gameDisplay.blit(grassRight,(1620,0))
def gameloop():
while True:
for event in pygame.event.get():
## if event.type == 2 or event.type == 12:
## pygame.quit()
## quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: dx = -3
elif event.key == pygame.K_RIGHT: dx = 3
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT: dx += 3
elif event.key == pygame.K_RIGHT: dx += -3
draw()
gameloop()
You need to add a pygame.display.flip() to your game loop.

How to make player image face according to the key pressed?

I am trying to make the character face the way the player presses the key on pygame in python I had been researching for a while and couldn't find what I can do. Basically I want the face or the direction of the character to face what the player press and for this game I only need left and right. Can someone give me so help? (This is what I have so far.)
import pygame
from pygame_functions import *
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("The Adventure of the Excorist's Meter")
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
# Set positions of graphics
background_position = [0, 0]
# Load and set up graphics.
background_image = pygame.image.load("skyline_background.jpg").convert()
crashed = False
character_img = pygame.image.load('character_left.png')
def player(x,y):
gameDisplay.blit(character_img, (x,y))
x = (display_width * 0.45)
y = (display_height * 0.65)
x_change = 0
player_speed = 0
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
# Copy image to screen:
gameDisplay.blit(background_image, background_position)
player(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
Use two images in two variables character_left and character_right and assing correct image - ie. character_img = character_left - when you change direction.
You can load two different images or use pygame.transform.flip to create second (flipped) image.
Code could look like this (I didn't test it).
import pygame
# --- constants --- (UPPER_CASE_NAMES)
DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
FPS = 30
# --- classes --- (CamelCaseNames)
# empty
# --- functions --- (lower_case_names)
# empty
# --- main --- (lower_case_names)
# - init -
pygame.init()
gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption("The Adventure of the Excorist's Meter")
# - objects -
background_image = pygame.image.load("skyline_background.jpg").convert()
background_position = [0, 0]
player_image_left = pygame.image.load('character_left.png').convert()
player_image_right = pygame.image.load('character_right.png').convert()
player_image = player_image_left
player_speed = 0
player_x = DISPLAY_WIDTH * 0.45
player_y = DISPLAY_HEIGHT * 0.65
player_x_change = 0
# - mainloop -
clock = pygame.time.Clock()
crashed = False
while not crashed:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x_change = -5
player_image = player_image_left
elif event.key == pygame.K_RIGHT:
player_x_change = 5
player_image = player_image_right
if event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
player_x_change = 0
# - updates -
player_x += player_x_change
# - draws -
gameDisplay.blit(background_image, background_position)
gameDisplay.blit(player_image, (player_x, player_y))
pygame.display.update()
clock.tick(FPS)
# - end -
pygame.quit()

Pygame moving object /image repeat itself [duplicate]

This question already has answers here:
Pygame how to fix 'trailing pixels'?
(4 answers)
Closed 5 years ago.
I'am building a simple game using pygame ,the function of this game is to control an image and make it move up/down left/right but i got this problem, the image repeats itself when moving down or aside
import pygame
pygame.init()
display_width = 1080
display_height = 1080
screen = pygame.display.set_mode((display_width,display_height))
Clock = pygame.time.Clock()
pygame.display.set_caption('DEMO')
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
screen.fill(white)
face=pygame.image.load('H:\\brain.jpg')
def brain(x,y):
screen.blit(face,(x,y))
x1 =(display_width*0.5)
y1 =(display_height*0.5)
x_change =0
y_change =0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif event.key == pygame.K_DOWN:
y_change = 5
elif event.key == pygame.K_UP:
y_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
y_change = 0
x1 += x_change
y1 += y_change
print(event)
brain(x1,y1)
pygame.display.update()
Clock.tick(100)
pygame.quit()
quit()
You have to blit your background at every iteration of the while-loop:
while True:
for event in pygame.event.get():
#get keyboard input
screen.fill((255, 255, 255)) #or, you can draw a background image
x1 += x_change
y1 += y_change
print(event)
brain(x1,y1)
pygame.display.update()
You need to blank out the old image, as well as drawing the new one. What you see is the residue: the portion of the previous images that wasn't covered by the white (background color) of the new image.
One easy way to handle this is to expand your drawn image to include a white border at least as large as the size of the motion.

Window boundaries in pygame

Basically I am trying to add some boundaries to a small game I am making, I have a sprite which can only move left to right and I am trying to prevent it from it from moving off the game window, if the sprite reaches the edge they can go no further and can then go in the opposite direction.
My current code:
tankSprite = pygame.image.load('Sprites/Tank.png') #fixed apostrophe
tankSprite_width = 28
def tank(x,y):
gameWindow.blit(tankSprite, (x,y))
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
gameExit = False
gameMove = True
while not gameExit and gameMove == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = False
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFTor event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameWindow.fill(white)
tank(x,y)
if x > display_width - tankSprite_width or x < 0:
gameMove = False
elif x < display_width - tankSprite_width or x > 0:
gameMove = True
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
My current code partially works, when the sprite reaches the edge the solution closes itself rather than letting me go back. If I remove:
pygame.quit()
quit()
Then the sprite just stops and I can't move it back. In a project I did a while back I used something like:
if event.key == pygame.K_LEFT and sprite.x < 590:
sprite.x += 5
The above code worked well from what I can remember but I can't seem to figure it out for my project now, can anyone help me out please?
I wish to make it so that the sprite can't go past the screen border, when the player reaches the border they either stay or go back in the opposite direction. Thanks
p.s. Sorry about the dodgy formatting
Just split your if-statement and check the condition for each direction separately:
tankSprite = pygame.image.load('Sprites/Tank.png')
tankSprite_width = 28
def tank(x,y):
gameWindow.blit(tankSprite, (x,y))
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
gameExit = False
gameMove = True
while not gameExit and gameMove == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = False
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFTor event.key == pygame.K_RIGHT:
x_change = 0
#valid move, add x_change
if((x+x_change)> 0 and (x+x_change)<(display_width - tankSprite_width):
x += x_change
gameWindow.fill(white)
tank(x,y)
#deleted stuff here
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
With this method, you don't need to even use the gameMove variable anymore.
You could also get rid of the x_change variable entirely by simply applying the changes directly to x instead.
Also, I think you may have meant gameExit = True under the if statement if event.type == pygame.QUIT:, as it makes sense to exit when event pygame.QUIT is triggered.

Categories

Resources