how to randomly generate spikes in pygame which are constantly moving down - python

im building a game in pygame in which a hot air balloon is constantly going up and spikes are coming down and hot air balloon has to dodge them. However i dont know how to randomly generate nicely spaced spikes which constantly moving downwards im having issue with randomly generating downward moving spikes.
here is the code:
import pygame
import random
import math
pygame.init()
clock = pygame.time.Clock()
screenwid = 400
screenhigh = 500
screen = pygame.display.set_mode((screenwid, screenhigh))
grey = (240,240,240)
black = (0,0,0)
initvel = 3
player_x = 100
player_y = 250
spikex= -100
spikey = -100
xchange = 0
ychange = 0
baloon = pygame.image.load('C:/Users/aliab/Downloads/hotair balloom.png')
baloon = pygame.transform.scale(baloon,(300, 300))
spike = pygame.image.load('C:/Users/aliab/Downloads/spikes.png')
spike = pygame.transform.scale(spike,(300, 300))
bg = pygame.image.load('E:/bgparacute.jpg')
def balloon(x,y):
screen.blit(baloon,(x,y ))
def spikee(x,y):
screen.blit(spike,(x,y ))
y = 0
run = True
while run:
rel_y = y % bg.get_rect().height
screen.blit(bg,(0,rel_y - bg.get_rect().height))
if rel_y < screenhigh:
screen.blit(bg, (0, rel_y))
y +=1
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xchange = -initvel
ychange = 0
if event.key == pygame.K_RIGHT:
xchange = initvel
ychange = 0
if event.key == pygame.K_UP:
ychange = -initvel
xchange = 0
if event.key == pygame.K_DOWN:
ychange = initvel
xchange = 0
if spikey == 100:
print('hihiohug')
spikex = random.randint(0, 400)
spikey = random.randint(-20, 10)
spikey += 2
player_x += xchange
player_y += ychange
balloon(player_x,player_y)
spikee(spikex, spikey)
clock.tick(60)
pygame.display.update()
pygame.quit()

I tried out your program. If you are looking to only have a few spikes on your screen, theoretically, you could just individually define the spikes and then randomly generate their starting position as in your code with some simple tweaks.
import pygame
import random
import math
pygame.init()
clock = pygame.time.Clock()
screenwid = 400
screenhigh = 500
screen = pygame.display.set_mode((screenwid, screenhigh))
grey = (240,240,240)
black = (0,0,0)
initvel = 2
player_x = 100
player_y = 250
spikex1 = -100
spikey1 = -100
spikex2 = -40
spikey2 = -100
xchange = 0
ychange = 0
baloon = pygame.image.load('Balloon.png')
baloon = pygame.transform.scale(baloon,(240, 240))
spike1 = pygame.image.load('Mine.png') # Multiple spike definitions (very simplistic)
spike1 = pygame.transform.scale(spike1,(30, 30))
spike2 = pygame.image.load('Mine.png')
spike2 = pygame.transform.scale(spike2,(30, 30))
bg = pygame.image.load('background-day.png')
def balloon(x,y):
screen.blit(baloon,(x,y ))
def spikee(x,y):
screen.blit(spike1,(x,y ))
screen.blit(spike2,(x,y ))
y = 0
run = True
while run:
rel_y = y % bg.get_rect().height
screen.blit(bg,(0,rel_y - bg.get_rect().height))
if rel_y < screenhigh:
screen.blit(bg, (0, rel_y))
y +=1
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xchange = -initvel
ychange = 0
if event.key == pygame.K_RIGHT:
xchange = initvel
ychange = 0
if event.key == pygame.K_UP:
ychange = -initvel
xchange = 0
if event.key == pygame.K_DOWN:
ychange = initvel
xchange = 0
if event.type == pygame.KEYUP: # Allow for a little more control of the balloon
xchange = 0
ychange = 0
if spikey1 == 280:
print('hihiohug')
spikex1 = random.randint(0, 300)
spikey1 = random.randint(-100, 10) # Adjust the random start above the screen for the spike
if spikey2 == 280:
print('hihiohug')
spikex2 = random.randint(0, 300)
spikey2 = random.randint(-100, 10) # Adjust the random start above the screen for the spike
spikey1 += 1
spikey2 += 1
player_x += xchange
player_y += ychange
balloon(player_x,player_y)
spikee(spikex1, spikey1)
spikee(spikex2, spikey2)
clock.tick(60)
pygame.display.update()
pygame.quit()
This produced randomly generated spikes (mines in my test).
However, you might want to define an array of spikes that are randomly placed just off of the display. As an example of that, I am including a link to GitHub for you to pull down a simple game I call "Submarine".
Submarine Game
It is an arcade like game where the submarine tries to avoid hitting underwater mines. It effectively is running in a horizontal fashion as opposed to your vertical action, but the underlying premises are alike. It has an array set up for storing underwater mine images that then appear on the screen.
Give that a try and see if that moves you forward in your game.

Related

How do I continuously loop KEYDOWN in pygame?

I'm new to python/pygame and I can't figure this out. Whenever I press and hold a key it won't loop the KEYDOWN. Also, if I hold the key on my keyboard down and move the mouse at the same time, it seems to move continuously.
Can someone tell me what I'm doing wrong?
import pygame
import random
pygame.init()
#Colors
white = 255, 255, 255
black = 0, 0, 0
back_color = 48, 255, 124
light_color = 34, 155, 78
#Display W/H
display_width = 800
display_height = 600
#X/Y
x_axis = 400
y_axis = 580
Block_size = 20
x_int = 0
y_int = 0
ON = True
Window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Game')
#On Loop
while ON == True:
#Screen Event
for Screen in pygame.event.get():
#Quit Screen
if Screen.type == pygame.QUIT:
pygame.quit()
exit()
#Quit Full Screen
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_q:
pygame.quit()
exit()
#Full Screen !!!!!!!! EDIT THIS !!!!!!!!
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_1:
pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)
if Screen.key == pygame.K_2:
pygame.display.set_mode((display_width, display_height))
#Player Movement K DOWN
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_d:
x_int = 20
if Screen.key == pygame.K_a:
x_int = -20
#Player Movement K UP
if Screen.type == pygame.KEYUP:
if Screen.key == pygame.K_d or Screen.key == pygame.K_a:
x_int = 0
x_axis += x_int
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
quit()
I have improved your code. You have placed the screen update (drawing the screen) portion in the events loop whereas it should be in the while loop. The code I have mode is a bit complex but works as expected. Why it is complex? When the key is held down, then the events list is empty (you can print the events). I have also made the block not to go out of the screen. The speed of the block was high so I decreased it to 10.
import pygame
import random
pygame.init()
#Colors
white = 255, 255, 255
black = 0, 0, 0
back_color = 48, 255, 124
light_color = 34, 155, 78
#Display W/H
display_width = 800
display_height = 600
#X/Y
x_axis = 400
y_axis = 580
global x_int
Block_size = 20
x_int = 0
y_int = 0
ON = True
Window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Game')
global topass_a,topass_d
x_int,topass_a,topass_d = 0,0,0
#On Loop
while ON:
#Screen Event
events = pygame.event.get()
def on_a_press():
global topass_a,x_int
x_int = -10
topass_a = 1
def on_d_press():
global topass_d,x_int
x_int = 10
topass_d = 1
if len(events) == 0:
if topass_a == 1:on_a_press()
if topass_d == 1:on_d_press()
for Screen in events:
if Screen.type == pygame.QUIT:
pygame.quit()
exit()
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_q:
pygame.quit()
exit()
if Screen.key == pygame.K_1:
pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)
if Screen.key == pygame.K_2:
pygame.display.set_mode((display_width, display_height))
if Screen.key == pygame.K_d or topass_d == 1:
on_d_press()
if Screen.key == pygame.K_a or topass_a == 1:
on_a_press()
if Screen.type == pygame.KEYUP:
if Screen.key == pygame.K_d or Screen.key == pygame.K_a:
x_int = 0
topass_a = 0
topass_d = 0
x_axis += x_int
x_int = 0
if x_axis < 0:x_axis=0
elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
You can further improve the code as you need.
Edit:
Why complex? Easy things come first. I have realized that there is no need to track the keys. pygame.key.get_pressed() returns the pressed key. Here is a smaller , better and improved code. I have also implemented the w and s (y_axis) keys.
import pygame
import random
pygame.init()
#Colors
white = 255, 255, 255
black = 0, 0, 0
back_color = 48, 255, 124
light_color = 34, 155, 78
#Display W/H
display_width = 800
display_height = 600
#X/Y
x_axis = 400
y_axis = 580
Block_size = 20
x_int = 0
y_int = 0
ON = True
Window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Game')
while ON:
events = pygame.event.get()
for Screen in events:
if Screen.type == pygame.QUIT:
pygame.quit()
exit()
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_q:
pygame.quit()
exit()
if Screen.key == pygame.K_1:
pygame.display.set_mode((display_width, display_height),pygame.FULLSCREEN)
if Screen.key == pygame.K_2:
pygame.display.set_mode((display_width, display_height))
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
x_int = -10
if keys[pygame.K_d]:
x_int = 10
# keys controlling y axis, you can remove these lines
if keys[pygame.K_w]:
y_int = -10
if keys[pygame.K_s]:
y_int = 10
#x_axis......
x_axis += x_int
x_int = 0
if x_axis < 0:x_axis=0
elif x_axis >= display_width-Block_size:x_axis = display_width-Block_size
#y axis
y_axis += y_int
y_int = 0
if y_axis < 0:y_axis=0
elif y_axis >= display_height-Block_size:y_axis = display_height-Block_size
#updaing screen
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
You only receive pygame.KEYDOWN when the key is first pressed - not while it is held down. The simple solution is to only draw while the key is down (ie. when x_int != 0)
#On Loop
while ON == True:
#Screen Event
for Screen in pygame.event.get():
# <Removed not relevant code for brevity>
#Player Movement K DOWN
if Screen.type == pygame.KEYDOWN:
if Screen.key == pygame.K_d:
x_int = 20
if Screen.key == pygame.K_a:
x_int = -20
#Player Movement K UP
if Screen.type == pygame.KEYUP:
if Screen.key == pygame.K_d or Screen.key == pygame.K_a:
x_int = 0
# Render only happens if x_int is not zero
# (Need to add code to force render first time)
if x_int:
x_axis += x_int
Window.fill((back_color))
Player = pygame.draw.rect(Window, light_color, [x_axis, y_axis, Block_size, Block_size])
pygame.display.update()
As the program grows and gets more complex, you'll need better logic for when to render, but this will get you started.

pygame - make a stickman jump using arrow keys but goes no higher than 10 units

the stickman I have is able to move and all, using the arrow keys, and but, when the up arrow key is pressed, I want him to only be able to go about 10 units every time the key is pressed, no matter how long it is pressed for. In other words I want him to jump, and have a limit to how high he can jump. I've tried a couple things but nothing has worked.
import pygame
def drawMan(screen,x,y):
#head
pygame.draw.ellipse(screen,BLACK,[0+x,0+y,10,10], 0)
#body
pygame.draw.line(screen,BLACK,[4+x,17+y],[4+x,7+y], 2)
#legs
pygame.draw.line(screen,BLACK,[4+x,17+y],[9+x,27+y], 2)
pygame.draw.line(screen,BLACK,[4+x,17+y],[-1+x,27+y], 2)
#arms
pygame.draw.line(screen,BLACK,[4+x,7+y],[8+x,17+y], 2)
pygame.draw.line(screen,BLACK,[4+x,7+y],[0+x,17+y], 2)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BORDER = (100,100,100)
pygame.init()
size = (800, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Jump")
done = False
clock = pygame.time.Clock()
pygame.mouse.set_visible(1)
xCoord = 11
yCoord = 463
xSpeed = 0
ySpeed = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xSpeed = -3
if event.key == pygame.K_RIGHT:
xSpeed = 3
if event.key == pygame.K_UP:
ySpeed = -3
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
xSpeed = 0
if event.key == pygame.K_RIGHT:
xSpeed = 0
if event.key == pygame.K_UP:
ySpeed = 3
if xCoord >= 780:
xSpeed = 0
xCoord -= 1
elif xCoord <= 13:
xSpeed = 0
xCoord += 1
elif yCoord > 465:
ySpeed = 0
yCoord -= 1
elif yCoord <= 13:
ySpeed = 0
yCoord += 1
else:
xCoord += xSpeed
yCoord += ySpeed
screen.fill(WHITE)
pygame.draw.line(screen, BORDER, [0,0],[800,0], 20)
pygame.draw.line(screen, BORDER, [0,0],[0,500], 20)
pygame.draw.line(screen, BORDER, [0,500],[800,500], 20)
pygame.draw.line(screen, BORDER, [800,500],[800,0], 20)
drawMan(screen,xCoord,yCoord)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Set a variable that allows him to jump. If the variable isn't True, then the jump key does nothing.
When he jumps, switch the variable to False. Don't reset it until he hits the ground again.
Pseudocode:
IF INPUT = "jump" AND can_jump == True THEN
can_jump = False
player.jump()
END IF
IF player.y == 0 and can_jump == False THEN
can_jump = True
END IF
dudee i know it is so late but i found out the answer for this and it is a simple thing
import pygame as game
import keyboard
game.init()
dis=game.display.set_mode((1280,720))
state=0
simpclr=(24,24,24)
white=(255,255,255)
posx=5
posy=5
a=0
jcd=1
clock=game.time.Clock()
while not state:
for event in game.event.get():
if event.type==game.QUIT:
state=1
dis.fill(white)
pl=game.Rect(posx,posy,20,20)
game.draw.rect(dis,simpclr,pl)
game.display.flip()
posy+=2
if keyboard.is_pressed('d'):
posx+=5
if jcd<10:
if keyboard.is_pressed(" "):
posy-=10
jcd+=1
if jcd<50 and jcd>=10:
jcd+=1
if jcd==50:
jcd=1
if posx==1230:
posx-=5
if posy==100:
posy-=5
game.display.flip()
clock.tick(60)
game.quit()

Pygame obstacles not being generated more than once

So right now I am learning pygame and I'm using sentdex's tutorials. On I believe his 6th pygame tutorial he showed us how to generate more than just one block. I added the code, but it didin't work. I don't understand why it didn't work? Could you please tell me what I'm doing wrong and how to fix it?
Here's the code:
import pygame
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
window_width = 800
window_height = 600
gameDisplay = pygame.display.set_mode((window_width,window_height))
pygame.display.set_caption('MyFirstGame')
carImg = pygame.image.load("racecar.png")
clock = pygame.time.Clock()
def blocks(block_x, block_y, block_width, block_height, color):
pygame.draw.rect(gameDisplay, color, [block_x, block_y, block_width, block_height])
def crash():
print("YOUR GARBAGE! Press C to play again or Q to quit.")
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit
quit()
gameExit = True
if event.key == pygame.K_c:
gameLoop()
print("User has decided to play again")
def gameLoop():
# Defining variables to draw and move car
car_x = 350
car_y = 400
car_x_change = 0
car_w = 100
car_h = 100
# Defining variables to draw blocks
block_x = random.randrange(0, window_width)
block_y = -600
block_speed = 7
block_width = 100
block_height = 100
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:
car_x_change += -5
if event.key == pygame.K_RIGHT:
car_x_change += 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change = 0
if event.key == pygame.K_RIGHT:
x_change = 0
car_x += car_x_change
gameDisplay.fill(white)
# blocks((block_x,block_y,[block_width,block_height,black))
blocks(block_x, block_y, block_width, block_height, black)
block_y += block_speed
gameDisplay.blit(carImg, (car_x,car_y))
if car_x >= window_width:
gameExit = True
crash()
if car_x <= 0:
gameExit = True
crash()
if car_y > window_height:
block_y = 0 - block_height
block_x = random.randrange(0,window_width-block_width)
pygame.display.update()
clock.tick(30)
gameLoop()
pygame.quit
quit()
One pair block_x, block_y can't be use to draw many blocks.
You have to create list with pairs (x,y) for all blocks which you want to draw. You have to use for loops to create blocks (pairs (x,y)), to move them, and to draw them.
BTW: running gameloop() inside crash() create recursion which is not prefered method.
My version with more information in comments # changed
import pygame
import random
# changed: better organized code
# --- constants --- (UPPERCASE_NAMES)
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# --- functions --- (lowercase_names)
def blocks(points, width, height, color):
# changed: use `for` to draw all blocks
for x, y in points:
pygame.draw.rect(display, color, [x, y, width, height])
def crash():
print("YOUR GARBAGE! Press C to play again or Q to quit.")
# changed: return `True/False` insted of executing gameloop()
# because it was not good idea
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
return True # True = quit
if event.key == pygame.K_c:
print("User has decided to play again")
return False # false = continue
def gameloop():
# --- objects ---
car_image = pygame.image.load("racecar.png")
# Defining variables to draw and move car
car_x = 350
car_y = 400
car_x_change = 0
car_w = 100
car_h = 100
# BTW: you could use `pygame.Rect` to keep position and size
# car_rect = car_image.get_rect(x=350, y=450)
# Defining variables to draw blocks
block_speed = 7
block_width = 100
block_height = 100
# changed: create list with points x,y
pairs = []
for _ in range(10):
x = random.randrange(0, WINDOW_WIDTH)
y = -block_height
pairs.append( (x, y) )
# --- mainloop ---
clock = pygame.time.Clock()
gameExit = False
while not gameExit:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
car_x_change -= 5
elif event.key == pygame.K_RIGHT:
car_x_change += 5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
car_x_change += 5 # changed: wrong variable, use +=
elif event.key == pygame.K_RIGHT:
car_x_change -= 5 # changed: wrong variable, use -=
# --- updates ---
# changed: all moves before draws
# move car
car_x += car_x_change
# changed: one `if` for both crashes (`car_x`)
if car_x < 0 or car_x >= WINDOW_WIDTH:
# changed: use value from crash to set gameExit
# and to move car to start position
gameExit = crash()
if not gameExit:
car_x = 350
car_y = 400
# changed: use `for` to move all points
# and to check if they need new random place
# move blocks
moved_pairs = []
for x, y in pairs:
y += block_speed
if y > WINDOW_HEIGHT:
y = -block_height
x = random.randrange(0, WINDOW_WIDTH - block_width)
moved_pairs.append( (x, y) )
pairs = moved_pairs
# --- draws ---
# changed: all draws after moves
display.fill(WHITE)
blocks(pairs, block_width, block_height, BLACK)
display.blit(car_image, (car_x, car_y))
pygame.display.update()
# --- FPS ---
clock.tick(30)
# --- main ---
pygame.init()
display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('MyFirstGame')
gameloop()
pygame.quit() # changed: forgot ()

Python sprite not moving when key is held down

I'm working on the basics of a game right now, but for some reason I cannot get my sprite to move. I know that it's registering when I'm pressing the key down, but the sprite for some reason just stays still.
import pygame
import sys
from pygame.locals import *
import time
pygame.init()
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("RADICAL")
screen.fill(black)
imga = pygame.image.load('coina.png')
imgb = pygame.image.load('coinb.png')
sound = pygame.mixer.Sound('coin.mp3')
FPS = 80
imgx = 10
imgy = 10
pixMove = 10
steps = 0
x1 = 0
y1 = 0
keystate = pygame.key.get_pressed()
GameOver = False
while not GameOver:
screen.fill(white)
points = (steps)
font = pygame.font.SysFont(None, 30)
text = font.render('Score: '+str(points), True, black)
screen.blit(text, (0,0))
if steps % 2 == 0:
screen.blit(imga, (imgx, imgy))
else:
screen.blit(imgb, (imgx, imgy))
for event in pygame.event.get():
print event
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if keystate[pygame.K_UP]:
y1 -= pixMove
elif keystate[pygame.K_DOWN]:
y1 += pixMove
elif keystate[pygame.K_LEFT]:
x1 -= pixMove
elif keystate[pygame.K_RIGHT]:
x1 += pixMove
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x1 = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y1 = 0
steps +=1
if event.type == K_SPACE:
sound.play()
time.sleep(1)
sound.stop()
pygame.display.update()
fpsTime.tick(FPS)
Generally you blit() images on to the screen (pygame.display.set_mode()), for the changes to to reflect on the screen we call pygame.display.update(), In your case the game never comes the statement, which is out of while loop.

Pygame - shooting bullets

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

Categories

Resources