ProgramCode
ProgramCode
about:blank 1/3
4/9/25, 10:05 AM about:blank
53 Circle(220, 232, 2, fill='salmon')
54 ])
55
#code so that if the pig is clicked on above 250 it gets wings (if statemen
56
t)
57 if y<250:
58 parts.append(Arc(240,212,52,30,85,100,fill = 'white'))
59
60 #creates a pig group with the body parts
61 pig=Group(*parts)
62 pig.centerX= x
63 pig.centerY = y
64 pigs.add(pig)
65
66 #code so that the pigs randomly move around the canvas
67 dx = choice([-4,4]) *randrange(1,5)
68 dy = choice([-4,4]) *randrange(1,5)
69 pigSpeeds.append([dx,dy])
70
71
72
73 #Creates a new pig after one disappears
74 #partner and I co-wrote this
75 def drawNewPigs():
76 pigX=[92, 292, 337,158]
77 pigY=[347, 347, 287,283]
78 directions = ['right', 'left', 'right', 'left']
79 for i in range(len(pigX)):
80 createPig(pigX[i], pigY[i], direction = directions[i])
81
82 #call function.
83 drawNewPigs()
84
85 #controls the pointer
86 def onMouseMove(mouseX,mouseY):
87 pointer.centerX = mouseX
88 pointer.centerY = mouseY
89
90
91
92
93 #function so that everytime "space" is clicked over a pig, it disappears and ad
ds to the score
94 #partner and I co-wrote this
95 def onKeyPress(key):
96 for i,pig in enumerate(pigs.children):
97
if key == 'space' and pig.visible and pig.hits(pointer.centerX, pointe
98
r.centerY):
99 pig.visible = False
100 scoreBoard.value +=1
101
102 newX = randrange(50,350)
103 newY = randrange(150,350)
104 createPig(newX,newY, direction = choice(['left','right']))
105
about:blank 2/3
4/9/25, 10:05 AM about:blank
106
107 #function to animate the pigs, used ChatGPT
108 def onStep():
109 for i, pig in enumerate(pigs):
110 if pig.visible:
111 dx,dy = pigSpeeds[i]
112 pig.centerX +=dx
113 pig.centerY += dy
114 if pig.left<= 0 or pig.right>=400:
115 pigSpeeds[i][0] *= -1
116 if pig.top <= 0 or pig.bottom >= 400:
117 pigSpeeds[i][1] *= -1
about:blank 3/3