如何使2个单独的图像相互追逐并通过Pygame 2.5互相跟随?

时间:2014-04-21 03:34:44

标签: python pygame

我正在使用Python 25编码2个图像在屏幕周围互相追逐。一个是猫,另一个是鼠标图像。我可以让他们互相追逐,但他们不会保持相同的路线。我如何将它们带到彼此跟随的地方,并最终让猫吃掉char鼠标并使程序结束?这是我的代码:

#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)

1 个答案:

答案 0 :(得分:1)

您可以通过找出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'

你必须为猫的移动和鼠标的移动制作单独的变量,除非你改变它们的速度,猫总是会相对快速地抓住鼠标。

要使鼠标事件触发动作,您可以编写类似

的内容
started = False
while True:
    if started:
        #do movement stuff

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            started = True

您还可以将pygame.KEYDOWN用于关键事件。最后,如果你想让猫杀死鼠标,你必须每帧比较猫和鼠标的位置。在这里,我检查x和y的差异是否小于10像素,你可以根据图像的大小来改变它。

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'

开始=假 而True:#while true将显示屏设置为黑色     setDisplay.fill(黑色)

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)