0

This is the code for the start of an aim based game i am trying to make for a school project. For context the game should work where a target appears somewhere on the screen and when clicked it moves to a different position. This part works i have it so the image blits to a different position but the rectangle of the image that i am using seems to stay in the same place and doesn't move with the image this means that my collisions don't work so the target only moves once.

I need the Xpos and the Ypos of the rectangle to move to the same position as the new image so that i can see if the cross hair is inside the new target area but the ways i have tried haven't seemed to work. Any help would be appreciated.

import pygame
import random
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()

class Targets(pygame.Surface):
    def __init__(self, image, xpos=0, ypos=0):
        self.image = image
        self.xpos = xpos
        self.ypos = ypos
        self.width =  image.get_width()
        self.height = image.get_height()
        self.rect = pygame.Rect(xpos, ypos, image.get_width(), image.get_height())


def init():
    global target_obj
    target = pygame.image.load("target.png").convert_alpha()
    target_obj = Targets(target, xpos= random.randint(0, 672), ypos= random.randint(0, 672))


def run():
    global done
    done = False
    while not done:
        check_events()
        update()
        clock.tick(60)      

def check_events():
    global done
    global target_obj

    for event in pygame.event.get():
        mouse_pos = pygame.mouse.get_pos()

        if event.type == pygame.QUIT:
            done = True
            pygame.quit()
            quit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if target_obj.rect.collidepoint(mouse_pos):
                    target_obj.xpos = random.randint(0, 672)
                    target_obj.ypos = random.randint(0, 672)
                    screen.blit(target_obj.image, (target_obj.xpos, target_obj.ypos))
                    pygame.display.update()
                    print("Clicked")


        if event.type == pygame.MOUSEMOTION:
            if target_obj.rect.collidepoint(mouse_pos):
                print("collision")




def update():
    global ImageList
    screen.fill((255, 255, 255))

    #Update the screen with drawing
    screen.blit(target_obj.image, (target_obj.xpos, target_obj.ypos))

    pygame.display.update() 

if __name__=="__main__":
    init()
    run()

EDIT

This was fixed by changing

 screen.blit(target_obj.image, (target_obj.xpos, target_obj.ypos)) 

to

 screen.blit(target_obj.image, target_obj.rect) 

and changing

 target_obj.xpos = random.randint(0, 672)
 target_obj.ypos = random.randint(0, 672) 

to

target_obj.rect.x = random.randint(0, 672)
target_obj.rect.y = random.randint(0, 672) 
  • Why not just use the `rect` object to position the image? You can do `screen.blit(target_obj.image, target_obj.rect)` and moving the rect is can be done `target_obj.rect.x = random.randint(0, 672)`. You also have many [other useful methods for the rect](https://www.pygame.org/docs/ref/rect.html) – Ted Klein Bergman Oct 03 '17 at 11:51
  • And don't inherit from `pygame.Surface`. It's not doing anything for you in the code anyway. You probably meant to inherit from [`pygame.sprite.Sprite`](https://www.pygame.org/docs/tut/SpriteIntro.html) – Ted Klein Bergman Oct 03 '17 at 11:55
  • Thanks will give this a try – Ruari Kerr Oct 03 '17 at 13:32
  • Thank you this worked really appreciated – Ruari Kerr Oct 03 '17 at 20:23

0 Answers0