kingLoomis
kingLoomis

Reputation: 1

How do I make 2 separate images chase each other and follow each other via Pygame 2.5?

I am using Python 25 coding 2 images to chase each other around the screen. One is a cat and the other image is a mouse image. I can get them to chase each other but they do not stay on the same course. How do I make it to where they follow each other and possibly eventually have the cat eat the char mouse and make the program end? Here is my code:

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

Upvotes: 0

Views: 504

Answers (1)

KSab
KSab

Reputation: 592

You can find the cat's direction by finding which positional difference in either x or y is farthest and moving with something like:

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'

You will have to make separate variables for the cat's movement and the mouse's movement, and the cat will always catch the mouse relatively quickly unless you change their speeds.

To make a mouse event trigger the motion, you can write something like

started = False
while True:
    if started:
        #do movement stuff

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

You can also use pygame.KEYDOWN for key events. And finally if you want the cat to kill the mouse, you have to compare the positions of the cat and the mouse every frame. Here I check to see if both the x and the y difference are less then 10 pixels, and you can change this depending on the sizes of your images.

if abs(imgx2 - imgx1) < 10 and abs(imgy2 - imgy1) < 10:
    print "Mouse Caught!!!"

Here is an example code that works for me (I didnt have images so I changed them into squares)

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

started = False while True: #while true set display screen to black 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)

Upvotes: 1

Related Questions