我正在使用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 票数: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'

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

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)

  ask by kingLoomis translate from so

未解决问题?本站智能推荐: