1

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()
NJB's Codings
  • 17
  • 1
  • 9
  • Can you be a bit more direct about what you tried in order to make the object disappear? Just dumping the whole code isn't that effective to get help. [ask] – lhd Apr 25 '20 at 09:43

1 Answers1

1

I recommend to use a timer event. Use pygame.time.set_timer() to repeatedly create an USEREVENT. e.g.:

time_delay = 3000 # 3 seconds
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, time_delay)

Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case pygame.USEREVENT+1 is the event id for the timer event.

Get the events in the event loop and define a new random position when the timer_event occurs:

def main():
    global locx, locy

    time_delay = 3000 # 3 seconds
    timer_event = pygame.USEREVENT + 1
    pygame.time.set_timer(timer_event, time_delay)

    while True:
        for event in pygame.event.get():

            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == timer_event:
                locx = random.randint(0, 29)
                locy = random.randint(0, 29)

            # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174