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

Posted

技术标签:

【中文标题】如何通过 Pygame 2.5 让 2 个单独的图像相互追逐并相互跟随?【英文标题】:How do I make 2 separate images chase each other and follow each other via Pygame 2.5? 【发布时间】:2014-04-21 03:34:44 【问题描述】:

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

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

【问题讨论】:

如果可能的话,让我知道如何使用鼠标单击而不是自己运行它来使其开始运行。 您是否希望他们的运动被限制为一次只能向上或向右上下,或者他们是否应该能够以匀速移动任何方向。另外,当您说彼此跟随时,您的意思是一个跟随另一个吗? 是的,只有向下、向左、向右、向上……基本上让 cat 字符在屏幕上追逐鼠标字符,就像参数追逐一样。我还想让它们在我单击一个键时开始移动,如果可能的话让猫“杀死”鼠标字符并关闭程序。 【参考方案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'

开始 = 假 while 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)

【讨论】:

你能把它们放在一个 pde 中吗? 非常感谢 KSab 的帮助!我如何给你一个好的帮助率?我是这个地方的新手。 不客气,如果您认为答案足够正确,您可以将其选为正确(会出现一个小复选标记)。您也可以投票赞成任何您认为好的答案。 (看起来你可能已经这样做了)。如果您需要任何帮助以使其正常工作,请随时再次发表评论。

以上是关于如何通过 Pygame 2.5 让 2 个单独的图像相互追逐并相互跟随?的主要内容,如果未能解决你的问题,请参考以下文章

在 4 个单独的图上绘制每个评级的组平均值

如何在 Pygame 中加载动画 GIF 并获取所有单独的帧?

Pygame - 在保持单独功能的同时创建具有相同功能的多行

如何让 Kivy 1.9.1 或 1.9.2 在 OSX 10.12.2 上使用 SDL2 而不是 pygame?

如何让函数返回 2 个单独的值? [复制]

转载pygame安装