Python中文
首页
教程
问答
标签
搜索
登录
注册
在皮格姆追逐精灵
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有两个精灵,一只猫和一只老鼠,我想让猫追老鼠。无论老鼠在哪里,猫都应该朝着老鼠移动,并试图“抓住”它。如果老鼠在猫的上方,猫就会上移试图“抓住”它。程序运行,我可以用箭头键移动鼠标,但猫不会动。这是我的代码:</p> <pre><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) </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>你不应该吗</p> <pre><code>self.rect.centerx += self.move_x </code></pre> <p>在<code>Cat</code>类中的某个地方?(当然,<code>y</code>也一样)</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Python中两个字典的交集
1 回答
python中两个字符串上的异或操作数?
5 回答
Python中两个字符串中的类似句子
3 回答
Python中两个字符串之间的Hamming距离
4 回答
python中两个字符串之间的匹配模式
2 回答
python中两个字符串之间的按位或
7 回答
python中两个字符串之间的数据(字节)切片
5 回答
python中两个字符串之间的模式
6 回答
python中两个字符串作为子字符串的区别
9 回答
Python中两个字符串元组的比较
6 回答
Python中两个字符串列表中的公共字符串
8 回答
python中两个字符串的Anagram测试
1 回答
Python中两个字符串的正则匹配
9 回答
python中两个字符串的笛卡尔乘积
2 回答
Python中两个字符串相似性的比较
7 回答
python中两个字符串语义相似度的求法
5 回答
Python中两个字符置换成固定长度的字符串,每个字符的数目相等
9 回答
Python中两个对数方程之间的插值和平滑数据
9 回答
Python中两个对象之间的And/Or运算符
9 回答
python中两个嵌套字典中相似键的和值
9 回答