Python 我如何通过Pygame 2.5让两个单独的图像互相追逐和跟随?
我使用Python25编码两幅图像,在屏幕上互相追逐。一个是猫,另一个是鼠标图像。我可以让他们互相追逐,但他们的路线不同。我怎样才能做到他们互相跟踪,最终可能让猫吃掉鼠标,让程序结束?这是我的密码:Python 我如何通过Pygame 2.5让两个单独的图像互相追逐和跟随?,python,pygame,Python,Pygame,我使用Python25编码两幅图像,在屏幕上互相追逐。一个是猫,另一个是鼠标图像。我可以让他们互相追逐,但他们的路线不同。我怎样才能做到他们互相跟踪,最终可能让猫吃掉鼠标,让程序结束?这是我的密码: #Agustin Loomis #'Cat & Mouse Chaser Game' #Last Modification 4/20/14 #Cat char chases Mouse char import pygame #import essentials import sys fro
#Agustin Loomis
#'Cat & Mouse Chaser Game'
#Last Modification 4/20/14
#Cat char chases Mouse char
import pygame #import essentials
import sys
from pygame.locals import*
pygame.init() #initialize pygame
#color setup
white = (255,255,255)
black = (0,0,0)
red = (255, 0 , 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
cyan = (0,255,255)
purple = (255, 0 , 255)
FPS = 30 #assign Frame Per Second to the value of 30
fpsTime = pygame.time.Clock() #assign 'fpsTime' to pygame time clock
setDisplay = pygame.display.set_mode((500,500)) #set the display screen to 500, 500
pygame.display.set_caption('Cat & Mouse Chaser Game')
imgCat = pygame.image.load('include/NyanCat.jpg') #load img for the char cat
imgx1 = 0 #assign img coordinates
imgy1 = 10
imgMouse = pygame.image.load('include/run-with-the-mice.gif') #load imf for the Mouse char
imgx2 = 10 #assign img coordinates
imgy2 = 200
pixChange = 5 #assign variable pixel change to value of 5
movement = 'down' #assign movement constant to down
while True: #while true set display screen to black
setDisplay.fill(black)
if movement == 'down':
imgy1 += pixChange
imgy2 += pixChange
if imgy1 > 400:
movement = 'right'
if imgy2 > 400:
movement = 'right'
elif movement == 'right':
imgx1 += pixChange
imgx2 += pixChange
if imgx1 > 400:
movement = 'up'
if imgx2 > 400:
movement = 'up'
elif movement == 'up':
imgy1 -= pixChange
imgy2 -= pixChange
if imgy1 < 10:
movement = 'left'
if imgy2 < 10:
movement = 'left'
elif movement == 'left':
imgx1 -= pixChange
imgx2 -= pixChange
if imgx1 < 40:
movement = 'down'
if imgx2 < 40:
movement = 'down'
setDisplay.blit(imgCat, (imgx1,imgy1))
setDisplay.blit(imgMouse, (imgx2, imgy2))
for event in pygame.event.get():
print event
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsTime.tick(FPS)
#阿古斯丁·卢米斯
#“猫和老鼠追逐游戏”
#最后修改日期:2014年4月20日
#猫追老鼠
导入pygame#导入基本要素
导入系统
从pygame.locals导入*
pygame.init()#初始化pygame
#颜色设置
白色=(255255)
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,0255)
黄色=(255,255,0)
青色=(0255255)
紫色=(255,0255)
FPS=30#将每秒帧数指定为30
fpsTime=pygame.time.Clock()#将“fpsTime”分配给pygame时间时钟
setDisplay=pygame.display.set_模式((500500))#将显示屏设置为500500
pygame.display.set_标题('Cat&Mouse Chaser Game')
imgCat=pygame.image.load('include/NyanCat.jpg')#为char cat加载img
imgx1=0#分配img坐标
imgy1=10
imgMouse=pygame.image.load('include/runwiththemices.gif')#为鼠标字符加载imf
imgx2=10#指定img坐标
imgy2=200
pixChange=5#将可变像素变化赋值为5
移动=‘向下’#将移动常数指定给向下
while True:#while True将显示屏设置为黑色
setDisplay.fill(黑色)
如果移动==“向下”:
imgy1+=pixChange
imgy2+=pixChange
如果imgy1>400:
运动=‘右’
如果imgy2>400:
运动=‘右’
elif移动==‘右’:
imgx1+=pixChange
imgx2+=pixChange
如果imgx1>400:
移动=‘向上’
如果imgx2>400:
移动=‘向上’
elif移动==“向上”:
imgy1-=pixChange
imgy2-=pixChange
如果imgy1小于10:
移动=‘左’
如果imgy2<10:
移动=‘左’
elif移动==“左”:
imgx1-=pixChange
imgx2-=pixChange
如果imgx1<40:
移动=‘向下’
如果imgx2<40:
移动=‘向下’
setDisplay.blit(imgCat,(imgx1,imgy1))
setDisplay.blit(imgMouse,(imgx2,imgy2))
对于pygame.event.get()中的事件:
打印事件
如果event.type==退出:
pygame.quit()
sys.exit()
pygame.display.update()
fpsTime.tick(FPS)
您可以通过查找x或y方向上哪个位置差最远,并以如下方式移动来找到猫的方向:
dx = imgx2 - imgx1
dy = imgy2 - imgy1
if abs(dx) > abs(dy):
if dx < 0:
catMovement = 'right'
else:
catMovement = 'left'
else:
if dy < 0:
catMovement = 'down'
else:
catMovement = 'up'
您还可以对关键事件使用pygame.KEYDOWN。最后,如果你想让猫杀死老鼠,你必须在每一帧比较猫和老鼠的位置。在这里,我检查x和y的差异是否都小于10像素,您可以根据图像的大小进行更改
if abs(imgx2 - imgx1) < 10 and abs(imgy2 - imgy1) < 10:
print "Mouse Caught!!!"
如果abs(imgx2-imgx1)<10且abs(imgy1-imgy1)<10:
打印“鼠标被抓住!!!”
下面是一个适用于我的示例代码(我没有图像,所以我将它们更改为正方形)
#阿古斯丁·卢米斯
#“猫和老鼠追逐游戏”
#最后修改日期:2014年4月20日
#猫追老鼠
导入pygame#导入基本要素
导入系统
从pygame.locals导入*
输入数学
pygame.init()#初始化pygame
#颜色设置
白色=(255255)
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,0255)
黄色=(255,255,0)
青色=(0255255)
紫色=(255,0255)
FPS=30#将每秒帧数指定为30
fpsTime=pygame.time.Clock()#将“fpsTime”分配给pygame时间时钟
setDisplay=pygame.display.set_模式((500500))#将显示屏设置为500500
pygame.display.set_标题('Cat&Mouse Chaser Game')
#imgCat=pygame.image.load('include/NyanCat.jpg')#为char cat加载img
imgCat=pygame.Surface((10,10));imgCat.填充((255,0,0))
imgx1=0#分配img坐标
imgy1=400
#imgMouse=pygame.image.load('include/runwiththemices.gif')#为鼠标字符加载imf
imgMouse=pygame.Surface((10,10));imgMouse.fill((0255,0))
imgx2=10#指定img坐标
imgy2=200
pixChange=5#将可变像素变化赋值为5
catMovement=‘向下’#将移动常数指定给向下
mouseMovement='右'
def move():#用于移动猫和鼠标的函数
全球鼠标移动
全局imgx1、imgy1、imgx2、imgy2
如果mouseMovement=='down':
imgy1+=pixChange
如果imgy1>400:
mouseMovement='右'
elif mouseMovement==‘右’:
imgx1+=pixChange
如果imgx1>400:
mouseMovement='up'
elif mouseMovement==“向上”:
imgy1-=pixChange
如果imgy1小于10:
mouseMovement='left'
elif mouseMovement==“左”:
imgx1-=pixChange
如果imgx1<40:
mouseMovement='down'
如果catMovement==“向下”:
imgy2+=pixChange
dx=imgx2-imgx1
dy=imgy2-imgy1
elif catMovement=='right':
imgx2+=pixChange
dx=imgx2-imgx1
dy=imgy2-imgy1
elif catMovement==‘向上’:
imgy2-=pixChange
dx=imgx2-imgx1
dy=imgy2-imgy1
elif catMovement==“左”:
imgx2-=pixChange
dx=imgx2-imgx1
dy=imgy2-imgy1
如果abs(dx)>abs(dy):
如果dx<0:
catMovement=‘右’
其他:
catMovement=‘左’
其他:
如果dy<0:
catMovement=‘向下’
其他:
catMovement=‘向上’
开始=错误
while True:#while True将显示屏设置为黑色
setDisplay.fill(黑色)
如果启动:
移动()
如果abs(imgx2-imgx1)<10且abs(imgy2-imgy1)<10:#如果猫和鼠标之间的距离小于10
打印“鼠标被抓住!!!”
打破
setDisplay.blit(imgCat,(imgx1,imgy1))
setDisplay.blit(imgMouse,(imgx2,imgy2))
对于pygame.event.get()中的事件:
如果event.type==退出
if abs(imgx2 - imgx1) < 10 and abs(imgy2 - imgy1) < 10:
print "Mouse Caught!!!"
#Agustin Loomis
#'Cat & Mouse Chaser Game'
#Last Modification 4/20/14
#Cat char chases Mouse char
import pygame #import essentials
import sys
from pygame.locals import*
import math
pygame.init() #initialize pygame
#color setup
white = (255,255,255)
black = (0,0,0)
red = (255, 0 , 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
cyan = (0,255,255)
purple = (255, 0 , 255)
FPS = 30 #assign Frame Per Second to the value of 30
fpsTime = pygame.time.Clock() #assign 'fpsTime' to pygame time clock
setDisplay = pygame.display.set_mode((500,500)) #set the display screen to 500, 500
pygame.display.set_caption('Cat & Mouse Chaser Game')
#imgCat = pygame.image.load('include/NyanCat.jpg') #load img for the char cat
imgCat = pygame.Surface((10,10));imgCat.fill((255,0,0))
imgx1 = 0 #assign img coordinates
imgy1 = 400
#imgMouse = pygame.image.load('include/run-with-the-mice.gif') #load imf for the Mouse char
imgMouse = pygame.Surface((10,10));imgMouse.fill((0,255,0))
imgx2 = 10 #assign img coordinates
imgy2 = 200
pixChange = 5 #assign variable pixel change to value of 5
catMovement = 'down' #assign movement constant to down
mouseMovement = 'right'
def move(): #function for moving the cat and mouse
global mouseMovement,catMovement
global imgx1,imgy1,imgx2,imgy2
if mouseMovement == 'down':
imgy1 += pixChange
if imgy1 > 400:
mouseMovement = 'right'
elif mouseMovement == 'right':
imgx1 += pixChange
if imgx1 > 400:
mouseMovement = 'up'
elif mouseMovement == 'up':
imgy1 -= pixChange
if imgy1 < 10:
mouseMovement = 'left'
elif mouseMovement == 'left':
imgx1 -= pixChange
if imgx1 < 40:
mouseMovement = 'down'
if catMovement == 'down':
imgy2 += pixChange
dx = imgx2 - imgx1
dy = imgy2 - imgy1
elif catMovement == 'right':
imgx2 += pixChange
dx = imgx2 - imgx1
dy = imgy2 - imgy1
elif catMovement == 'up':
imgy2 -= pixChange
dx = imgx2 - imgx1
dy = imgy2 - imgy1
elif catMovement == 'left':
imgx2 -= pixChange
dx = imgx2 - imgx1
dy = imgy2 - imgy1
if abs(dx) > abs(dy):
if dx < 0:
catMovement = 'right'
else:
catMovement = 'left'
else:
if dy < 0:
catMovement = 'down'
else:
catMovement = 'up'
if started:
move()
if abs(imgx2 - imgx1) < 10 and abs(imgy2 - imgy1) < 10: #if the distance between the cat and the mouse is less than 10
print "Mouse Caught!!!"
break
setDisplay.blit(imgCat, (imgx1,imgy1))
setDisplay.blit(imgMouse, (imgx2, imgy2))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN: #start if any mouse button is pressed down
started = True
pygame.display.update()
fpsTime.tick(FPS)