user2817437
user2817437

Reputation: 79

Chasing Sprites in Pygame

I have two sprites, a cat and a mouse, and I'm trying to make the cat chase the mouse. The cat should move towards the mouse and try to "catch" it no matter where the mouse is. If the mouse is above the cat, the cat will move up to try to "catch" it. The program runs and I can move the mouse with my arrow keys, but the cat won't move. Here is my code:

from pygame import *

size_x = 800
size_y = 600

class Object:
    def disp(self, screen):
        screen.blit(self.sprite, self.rect)

class Cat(Object):
    def __init__(self):
        self.sprite = image.load("cat.bmp")
        self.rect = self.sprite.get_rect()
        self.rect.centerx = size_x / 2
        self.rect.centery = size_y / 2
        self.move_x = 0
        self.move_y = 0
        def cycle(self):
#           self.rect.centerx = 500 - abs(self.count)
#           self.count += 2
#           if self.count > 400:
#               self.count = -400
            self.rect.centerx += self.move_x
            if self.rect.centerx < 0:
                self.rect.centerx = 800

#           self.rect.centery = 500 - abs(self.count)
#           self.count += 2
#           if self.count > 400:
#               self.count = -400
            self.rect.centery += self.move_y
            if self.rect.centery < 0:
                self.rect.centery = 800

    def chase(self, mouse):

        #These should move the cat towards the mouse.

        #If the cat is to the left of the mouse, this should move the cat right.
        if self.rect.centerx < mouse.rect.centerx:
            self.move_x += 3

        #If the cat is to the right of the mouse, this should move the cat left. 
        elif self.rect.centerx > mouse.rect.centerx:
            self.move_x -= 3

        #If the cat is above the mouse, this should move the cat down.    
        if self.rect.centery < mouse.rect.centery:
            self.move_y += 3

        #If the cat is below the mouse, this should move the cat up.     
        elif self.rect.centery > mouse.rect.centery:
            self.move_y -= 3


class Mouse(Object):
    def __init__(self):
        self.sprite = image.load("mouse.bmp")
        self.rect = self.sprite.get_rect()
        self.rect.centerx = 100
        self.rect.centery = 100
        self.count = 0
        self.move_x = 0
        self.move_y = 0

    def checkwith(self, otherrect):
        if self.rect.colliderect(otherrect):
            exit()

    def cycle(self):
#       self.rect.centerx = 500 - abs(self.count)
#       self.count += 2
#       if self.count > 400:
#           self.count = -400
        self.rect.centerx += self.move_x
        if self.rect.centerx < 0:
            self.rect.centerx = 800

#       self.rect.centery = 500 - abs(self.count)
#       self.count += 2
#       if self.count > 400:
#           self.count = -400
        self.rect.centery += self.move_y
        if self.rect.centery < 0:
            self.rect.centery = 800

    def right(self):
        self.move_x += 10

    def left(self):
        self.move_x -= 10

    def up(self):
        self.move_y -= 10

    def down(self):
        self.move_y += 10

    def stop_x(self):
        self.move_x = 0

    def stop_y(self):
        self.move_y = 0

init()
screen = display.set_mode((size_x, size_y))
c = Cat()
m = Mouse()
clock = time.Clock()

while True:
    for e in event.get():
        if e.type == QUIT:
            quit()
        if e.type == KEYDOWN:
            if e.key == K_RIGHT:
                m.right()
            elif e.key == K_LEFT:
                m.left()
            elif e.key == K_UP:
                m.up()
            elif e.key == K_DOWN:
                m.down()
        if e.type == KEYUP:
            if e.key == K_RIGHT or e.key == K_LEFT:
                m.stop_x()
            if e.key == K_UP or e.key == K_DOWN:
                m.stop_y()

    c.chase(m)
    m.cycle()
    screen.fill((255,255,255))
    m.disp(screen)
    c.disp(screen)
    display.flip()
    clock.tick(60)  

Upvotes: 0

Views: 840

Answers (2)

Junuxx
Junuxx

Reputation: 14261

Your Cat class doesn't update its rect, like you do for the Mouse in Mouse.cycle().

Just copy-paste the cycle() method to the Cat class, and add c.cycle() to your main loop.

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304175

Shouldn't you have

self.rect.centerx += self.move_x

somewhere in the Cat class? (same for y of course)

Upvotes: 0

Related Questions