-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacman.py
69 lines (53 loc) · 1.53 KB
/
pacman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#Pacman Ghost Algorithm - www.101computing.net/pacman-ghost-algorithm/
from math import atan, cos, sin
from processing import *
WIDTH=500
HEIGHT=500
pacman_X = 100
pacman_Y = 100
delay = 5
img = loadImage("rocketship.gif")
ghost_X = 10
ghost_Y = 10
def setup():
strokeWeight(3)
frameRate(20)
size(WIDTH,HEIGHT)
def moveGhost():
global ghost_X,ghost_Y,pacman_X,pacman_Y
fill(255,0,0)
stroke(255,255,255)
#Find out the direction (angle) the Ghost needs to move towards
#Using SOH-CAH-TOA trignometic rations
opposite=pacman_Y-ghost_Y
adjacent=pacman_X-ghost_X
angle = atan(opposite/adjacent)
if ghost_X>pacman_X:
angle=angle+180
#Use this angle to calculate the velocity vector of the Ghost
#Once again using SOH-CAH-TOA trignometic rations
velocity=3 #pixels per frame
vx = velocity * cos(angle)
vy = velocity * sin(angle)
#Apply velocity vector to the Ghost coordinates to move/translate the ghost
ghost_X = ghost_X + vx
ghost_Y = ghost_Y + vy
#Draw Ghost
ellipse( ghost_X,ghost_Y,30,30)
def movePacman():
global pacman_X, pacman_Y
fill(255,255,0)
stroke(0,0,0)
fc = environment.frameCount
#Pacman follows the mouse cursor
pacman_X += (mouse.x-pacman_X)/delay;
pacman_Y += (mouse.y-pacman_Y)/delay;
#Draw Pacman
#ellipse(pacman_X,pacman_Y,30,30)
image(img, pacman_X, pacman_Y)
def playGame():
background(50,50,150)
movePacman()
moveGhost()
draw = playGame
run()