Question

using pygame: 1. - Implement a simple application in which an image of your choice chases...

using pygame: 1. - Implement a simple application in which an image of your choice chases after the mouse cursor. Initially the image starts moving in the direction (0,0). During each frame, the object looks at the current location of the cursor using pygame.mouse.get_pos() and updates it's direction to be direction = .9*direction + v where v is a vector of length 10 that points from the center of your image to the mouse position.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: The code is for Python 3. Please indent the code as shown in the screenshots. Place all the files in the same folder to avoid locations issues.

Program screenshots:

Sample output:

Code to copy:

#Import the required packages.

import pygame

import sys

#Initialize the module.

pygame.init()

#Set the length and width of the window.

display_width = 1240

display_height = 720

gameDisplay = pygame.display.set_mode((display_width,display_height))

#Set the title of the window.

pygame.display.set_caption('Chase Game')

#Hide the cursor.

pygame.mouse.set_visible(False)

#Declare the required variables.

crashed = False

clock = pygame.time.Clock()

white = (255,255,255)

x = 0

y = 0

x_change = 0

y_change = 0

#Load the images.

dog = pygame.image.load('dog.jpg')

gameover = pygame.image.load('gameover.jpeg')

cat = pygame.image.load('cat.jpg')

#Define the function to move the image.

def modify(x,y):

    #Get the coordinates of the cursor.

    cursor = pygame.mouse.get_pos()

    m = 0

    ynew = 0

    #If the dog images catches the cat image.

    if int(x) == int(cursor[0]) and int(y) == int(cursor[1]):

        #Display the game over message in the middle of the screen.

        gameDisplay.fill(white)

        w, h = gameover.get_size()

        gameDisplay.blit(gameover, (display_width/2 - w/2, display_height/2 - h/2))

        pygame.display.update()

        clock.tick(60)

        #Close the window.

        pygame.quit()

        sys.exit()

       

    #If the x coordinate of the mouse does not match the

    # x coordinate of the image.

    if not int(cursor[0]) == int(x):

        #Compute the slope of the distance between dog and cat.

        m = (cursor[1]-y)/(cursor[0] - x)

        b = cursor[1] - m*cursor[0]

        if(cursor[0] > x):

            ynew = (m*(x+1) + b)

            x_change = 1

        else:

            ynew = (m*(x-1) + b)

            x_change = -1

   

    #If the x coordinate of the mouse is equal to the

  # x coordinate of the image.

    else:

       

        #Set the y coordinate in the respective direction.

        if cursor[1] < y:

            ynew = ynew - 1

        else:

            ynew = ynew + 1

        x_change = 0

    #Update and return the coordinates.

    x += x_change

    y = ynew

    return x,y

#Start the loop to play the game.

while True:

    #Break the loop if the user closes the window.

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            pygame.quit()

            sys.exit()

    #Fill the background.

    gameDisplay.fill(white)

    #Display the dog image.

    w, h = dog.get_size()

    gameDisplay.blit(dog,(x-w/2,y-h/2))

    #Get the cursor coodinates and display the

    # cat image.

    mosx, mosy = pygame.mouse.get_pos()

    w, h = cat.get_size()

    gameDisplay.blit(cat,(mosx-w/2,mosy-h/2))

    pygame.display.update()

    clock.tick(60)

    #Call the modify function to check the

    # location of the images and store the

  # updated coordinates.

    x,y = modify(x,y)

dog.jpg:

gameover.jpeg:

cat.jpg:

Add a comment
Know the answer?
Add Answer to:
using pygame: 1. - Implement a simple application in which an image of your choice chases...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT