Additions to Aim Trainer game

Added a collide function to be able to click the targets

def collide(self, x, y):
        '''creates the collisions with the mouse and the circle'''
        dis = math.sqrt((x - self.x)**2 + (y - self.y)**2)
        return dis <= self.size

Added ANOTHER draw function that updates the screen every frame so that the program doesn’t slow down

def draw(win, targets):
    '''wipes the screen, draws the objects, and updates the display'''
    win.fill(BG_COLOR)
    
    for target in targets:
        target.draw(win)
        
    pygame.display.update()

additional code for collide in main

if event.type == TARGET_EVENT:
               x = random.randint(TARGET_PADDING, WIDTH - TARGET_PADDING) 
               y = random.randint(TARGET_PADDING, HEIGHT - TARGET_PADDING)
               target = Target(x, y)
               targets.append(target)
if event.type == pygame.MOUSEBUTTONDOWN:
                click = True
                clicks += 1
            
        for target in targets:
            target.update()

            if target.size <= 0:
                targets.remove(target)
                misses += 1
            
            if click and target.collide(*mouse_pos):
                targets.remove(target)
                target_pressed += 1
            
            if misses >= LIVES:
                pass # end game

Aim Trainer game

I’m taking a small break with the Zelda game for now because it’s making me a little burnt out and it’s becoming slightly repetitive so I decided to start something new. I will go back to it though.

the ‘Target” class:

class Target:
    MAX_SIZE = 30
    GROWTH_RATE = 0.2
    COLOR = "red"
    SECOND_COLOR = "white"
    
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.size = 0
        self.grow = True
        
    def update(self):
        '''Udpates the target's size when it grows or shrinks'''
        if self.size + self.GROWTH_RATE >=self.MAX_SIZE:
            self.grow = False
            
        if self.grow:
            self.size += self.GROWTH_RATE
        else:
            self.size -= self.GROWTH_RATE
            
    def draw(self, win):
        '''draws a circle on the screen'''
        pygame.draw.circle(win, self.COLOR, (self.x, self.y), self.size)
        pygame.draw.circle(win, self.SECOND_COLOR, (self.x, self.y), self.size * 0.8)
        pygame.draw.circle(win, self.COLOR, (self.x, self.y), self.size * 0.6)
        pygame.draw.circle(win, self.SECOND_COLOR, (self.x, self.y), self.size * 0.4)

This creates the target. The target will and stop at a certain size and then shrink down to zero.

The update() function updates the size when the target grows and shrinks.

the draw() function creates the circles for the target. There are four circles because if we drew one it would just be on big circle growing and shrinking on the screen.

The “Main” loop

# Main Loop
def main():
    run = True
    
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                break
            
    pygame.quit()

This is the main loop of the program. All it does right now is that it allows you to quit the program.

You’re probably wondering why there isn’t much to this. The reason is 1. It’s career day so that made me loose time and 2. I wanted to make a post of the simple stuff I had for now before I added anything that would be long and tedious.