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>您的Cat类不会更新它的<code>rect</code>,就像您在<code>Mouse.cycle()</code>中为鼠标所做的那样。在</p> <p>只需将<code>cycle()</code>方法复制粘贴到Cat类,并将<code>c.cycle()</code>添加到主循环中。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
jupyter运行一个旧的pytorch版本
5 回答
Jupyter运行不同版本的卸载库?
4 回答
Jupyter运行指定的键盘快捷键
1 回答
Jupyter通过.local文件“逃逸”virtualenv。我该如何缓解这种情况?
9 回答
Jupyter重新加载自定义样式
10 回答
Jupyter错误:“没有名为Jupyter_core.paths的模块”
8 回答
jupyter错误:无法在随机林中将决策树视为png
6 回答
Jupyter错误'内核似乎已经死亡,它将自动重新启动'为一个给定的代码块
4 回答
Jupyter错误地用阿拉伯语和字母数字元素显示Python列表
2 回答
Jupyter隐藏数据帧索引,但保留原始样式
1 回答
Jupyter集线器:启动器中出现致命错误。。。系统找不到指定的文件
5 回答
Jupyther中相同值的相同哈希,但导出到Bigquery时不相同
7 回答
Jupy上Python的读/写访问问题
6 回答
jupy上没有模块cv
2 回答
Jupy上的排序错误
1 回答
Jupy中bqplot图形的紧凑布局
7 回答
Jupy中matplotlib plot的连续更新
10 回答
Jupy中Numpy函数的文档
4 回答
Jupy中Pandas的自动完成问题
4 回答
jupy中Qt后端的Matplotlib动画
6 回答