0% found this document useful (0 votes)
2 views

ProgramCode

The document outlines a code for a simple game where players score points by clicking on pigs that randomly move around the screen. It includes functions for creating pigs, controlling their movement, and updating the score when pigs are clicked. The game features a graphical interface with a background, score display, and interactive elements controlled by mouse and keyboard inputs.

Uploaded by

writetoishaan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ProgramCode

The document outlines a code for a simple game where players score points by clicking on pigs that randomly move around the screen. It includes functions for creating pigs, controlling their movement, and updating the score when pigs are clicked. The game features a graphical interface with a background, score display, and interactive elements controlled by mouse and keyboard inputs.

Uploaded by

writetoishaan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/9/25, 10:05 AM about:blank

Sandbox | CMU CS Academy


1 #Directions:
2 #Press space while hovering over pig, adds a point to the score
3
4 #background
5 app.background = gradient('blue', 'black', 'black', start = 'top')
6
7 #land
8 Rect(0, 300, 400, 400, fill='green')
9
10 #pointer
11 pointer = Circle(5, 5, 12, fill='lightBlue')
12
13
14 #Score
15 scoreBoard = Label(int('0'),320,50, size = 30, fill = 'white')
16
17 #list of speeds
18 pigSpeeds = []
19
20 #Group for pigs
21 pigs = Group()
22
23 #student developed function to draw one pig
24 #partner and I co-wrote this
25 def createPig(x,y,direction ='right'):
26
27 #sets color for pig body part
28 color = 'hotPink'
29
30 #list for the pig body parts
31 parts=[]
32
33 #define positions of the pigs legs
34 leg_positions = [(217,252,227,252), (252,252,262,252)]
35
36 #loops through leg positions and add legs to the list
37 #iteration
38 for x1, y1, x2, y2 in leg_positions:
parts.append(Line(x1,y1,x2,y2, fill = color, lineWidth = 11, dashes =
39 (4,2)))
40
41 #body
42 parts.append(Oval(212, 212, 55, 45, fill=color, align='left-top'))
43 parts.append(Oval(207, 217, 8, 4, rotateAngle=-20, fill='pink'))
44 parts.append(Oval(227, 217, 8, 3, rotateAngle=20, fill='pink'))
45
46 #details of the pig
47 parts.extend([
48 Oval(217, 227, 25, 30, fill=color),
49 Circle(212, 222, 3, border='white'),
50 Circle(222, 222, 3, border='white'),
51 Oval(217, 232, 15, 10, fill='pink'),
52 Circle(214, 232, 2, fill='salmon'),

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

You might also like