1

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()  
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
choya37
  • 13
  • 2
  • It sort of seems like you need general advice on how to debug your code, which stack overflow can't really help you with. It seems like you've already identified where the error might be-- if there's no error output, it's likely something to do with the complex boolean logic you've got there. – Maus Jul 15 '20 at 19:20

2 Answers2

3

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) 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • When I replaced the line with: rectangle = theImg.get_rect(topleft = (enemyX[something], enemyY[something])) it now gives me the error: 'int' object is not subscriptable – choya37 Jul 16 '20 at 01:26
  • @choya37 I've extended the answer. – Rabbid76 Jul 16 '20 at 04:28
2

Rabbid76 has answered the issue. I am not addressing that. I just want to add a suggestion to simplify something else I noticed, that you may want to use AFTER you have made the fix Rabbid76 provided.

The pygame Rect class has a method to detect if a point is colliding with the rect. You may want to use the collidepoint() method instead of the if pos[0] > rectangle.topleft[0] and ... that you are currently using:

                if pos[0] > rectangle.topleft[0] and pos[0] < rectangle.bottomright[0] \  
                        and pos[1] > rectangle.topleft[1] and pos[1] < rectangle.bottomright[1]:  

can be simplified to:

                if rectangle.collidepoint(pos):  

Glenn Mackintosh
  • 2,765
  • 1
  • 10
  • 18