I am trying to make a simple shooting game in python using pygame. My game's logic is simple, A target appears for three seconds in a random location and then disappear, In that time limit we have to move the pointer towards the target and hit space. If the Coordinates of bot of them match then we'll score a point. Here's the code
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
disp = pygame.display
win = disp.set_mode((450, 450))
disp.set_caption("Shooter")
#-----------------------------------------------------------------------------------------#
pointer = pygame.image.load('sprites/sprite0.png')
target = pygame.image.load('sprites/sprite1.png')
ptrx = 225
ptry = 225
locx = random.randint(0, 29)
locy = random.randint(0, 29)
points = 0
shot = False
green = (000, 255, 000)
array = [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
165, 180, 195, 210, 225, 240, 255, 270, 285, 300,
310, 330, 345, 360, 375, 390, 105, 420, 435]
score = 0
#-----------------------------------------------------------------------------------------#
def showTargetInRandomLoaction():
global locx
global locy
global array
win.blit(target, (array[locx], array[locy]))
# Here's the problem
def shoot():
global shot
shot = True
checkIfBulletHasHitTheTarget()
def checkIfBulletHasHitTheTarget():
global score
if shot == True:
bulletPos = (ptrx, ptry)
targPos = (array[locx], array[locy])
if bulletPos == targPos:
print("target hit success !")
score += 1
print(f"Your Score - {score}")
return True
else:
print("target hit uncess !")
return False
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
win.fill(green)
global ptrx
global ptry
global shot
global array
showTargetInRandomLoaction()
key = pygame.key.get_pressed()
left = key[pygame.K_LEFT]
right= key[pygame.K_RIGHT]
up = key[pygame.K_UP]
down = key[pygame.K_DOWN]
space = key[pygame.K_SPACE]
win.blit(pointer, (ptrx, ptry))
if left and ptrx >= 0:
ptrx -= 15
if right and ptrx <= 435:
ptrx += 15
if up and ptry >= 0:
ptry -= 15
if down and ptry <= 435:
ptry += 15
if space:
shot = True
shoot()
disp.update()
#----------------------------------------------------------------------------------------#
if __name__ == '__main__':
main()