在皮格姆追逐精灵

2024-03-29 05:17:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)  

Tags: thekeyrectselfsizemoveifdef
2条回答

你不应该吗

self.rect.centerx += self.move_x

Cat类中的某个地方?(当然,y也一样)

您的Cat类不会更新它的rect,就像您在Mouse.cycle()中为鼠标所做的那样。在

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

相关问题 更多 >