在Pygame中追逐精灵

时间:2013-11-07 00:27:46

标签: python pygame

我有两个精灵,一只猫和一只老鼠,我正试图让猫追逐老鼠。无论鼠标在哪里,猫都应朝着鼠标移动并试图“抓住”它。如果鼠标位于猫的上方,猫会向上移动以试图“抓住”它。程序运行,我可以用我的箭头键移动鼠标,但猫不会移动。这是我的代码:

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)  

2 个答案:

答案 0 :(得分:1)

您的Cat类不会像rect中的鼠标一样更新其Mouse.cycle()

只需将cycle()方法复制粘贴到Cat类,然后将c.cycle()添加到主循环中。

答案 1 :(得分:0)

你不应该

self.rect.centerx += self.move_x

Cat班的某个地方? (当然y也相同)