Search code examples
pythonpygame

Pygame: Code runs but there is no hit detection with targets and crosshair


I am trying to create hit detection with the targets in my game. Currently I have drawn a crosshair and enemy targets on screen, but clicking on the targets does not move them (they are not to be deleted, they should just respawn at the top of the screen). Apologies, I am new to coding and stackoverflow. I've only included code related to the issue. Here is my code:

import pygame  
import random  

pygame.init()  
width, height = 800, 600  
screen = pygame.display.set_mode((width, height))  
pygame.display.set_caption('Shooter Game')  # caption not necessary  
pygame.mouse.set_visible(False)  

cross_hair = pygame.image.load('crosshair.png')

enemyImg = []  
enemyX = []  
enemyY = []  
enemyX_change = []  
enemyY_change = []  
numberOfEnemies = 6  

for i in range(numberOfEnemies):    
    enemyImg.append(pygame.image.load('enemy.png'))  
    enemyX.append(random.randint(0, 800))  
    enemyY.append(random.randint(50, 80))  
    enemyX_change.append(1)  
    enemyY_change.append(40)  

def enemy(x, y, i):  
    screen.blit(enemyImg[i], (x, y))

running = True  
while running:

    screen.fill((128, 128, 128))  
    screen.blit(background, (0, 0))  

    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
            running = False  
        if event.type == pygame.MOUSEMOTION:  
            mouseX = event.pos[0]  
            mouseY = event.pos[1]  
        if event.type == pygame.MOUSEBUTTONDOWN:  
            pygame.mixer.Channel(0).play(shootSound)  
            for something in range(numberOfEnemies):  
                theImg = enemyImg[something]  
                rectangle = theImg.get_rect()  
                pos = pygame.mouse.get_pos()  
                if pos[0] > rectangle.topleft[0] and pos[0] < rectangle.bottomright[0] \  
                        and pos[1] > rectangle.topleft[1] and pos[1] < rectangle.bottomright[1]:  
                    ####THE ISSUE IS THE LINES ABOVE THIS ##### 
                    enemyX = random.randint(0, 800)  
                    enemyY = random.randint(50, 150)  
                    score_value += 1  
                    pygame.mixer.Channel(2).play(targetImpact)  

    pos = pygame.mouse.get_pos()  
    screen.blit(cross_hair, pos)  
    show_score(textX, textY)  
    pygame.display.flip()  

Solution

  • A pygame.Surface has no location. Note, when you blit the Surface Then you have to specify the location.
    The location of the pygame.Rect which is returned by get_rect() is (0, 0). You have to set a location, for instance by a keyword argument:

    theImg = enemyImg[something]  
    rectangle = theImg.get_rect(topleft = (enemyX[something], enemyY[something]))
    

    If you want to change the position of the the enemy, then you have to set (enemyX[something], enemyY[something]) rather than (enemyX , enemyY `):

    enemyX[something] = random.randint(0, 800)  
    enemyY[something] = random.randint(50, 150)  
    

    Example code:

    for something in range(numberOfEnemies):  
    
        theImg = enemyImg[something]  
        rectangle = theImg .get_rect(topleft = (enemyX[something], enemyY[something])) 
        
        pos = pygame.mouse.get_pos()  
        if rectangle.collidepoint(pos):  
                
            enemyX[something] = random.randint(0, 800)  
            enemyY[something] = random.randint(50, 150)  
            score_value += 1  
            pygame.mixer.Channel(2).play(targetImpact)