0% found this document useful (0 votes)
21 views3 pages

CSP CPT Code

The document contains a Python code for a game developed in CMU CS Academy, featuring a rocket that can move and shoot bullets at ghosts. The game includes functionalities such as scoring points for hitting ghosts, displaying a win message, and managing the positions of various game elements. The code also incorporates random ghost generation and handles user input for controlling the rocket's movement and shooting actions.

Uploaded by

karthikayanavolu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

CSP CPT Code

The document contains a Python code for a game developed in CMU CS Academy, featuring a rocket that can move and shoot bullets at ghosts. The game includes functionalities such as scoring points for hitting ghosts, displaying a win message, and managing the positions of various game elements. The code also incorporates random ghost generation and handles user input for controlling the rocket's movement and shooting actions.

Uploaded by

karthikayanavolu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/29/25, 1:04 PM about:blank

Sandbox | CMU CS Academy


1 app.background='black'
#the ghost and rocket drawing code was adapted from CMU CS Academy Lessons 3.1.
2
2
3 #and 3.1.4
4 #done by stevie
5 import random
6 bigStars=Group()
7 def stars():
8 Stars=Group(Star(80, 90, 2, 100, fill='yellow'),
9 Star(360, 175, 2, 100, fill='yellow'),
10 Star(220, 145, 2, 100, fill='yellow'),
11 Star(322, 50, 2, 100, fill='yellow'),
12 Star(30, 290, 2, 100, fill='yellow'))
13 bigStars.add(Stars)
14
15 #code from CMU 3.1.4 Rocket
16 #done by karhtik
17 rocket = Group(
Polygon(198*0.45, 145*0.45, 202*0.45, 145*0.45, 200*0.45, 120*0.45, fill='c
18 rimson'),
Polygon(176*0.45, 273*0.45, 155*0.45, 350*0.45, 190*0.45, 335*0.45, fill='c
19
rimson'),
20 Polygon(224*0.45, 273*0.45, 245*0.45, 350*0.45, 210*0.45, 335*0.45, fill='c
rimson'),
Oval(200*0.45, 250*0.45, 50*0.45, 200*0.45, fill=gradient('gainsboro', 'dar
21
kGrey', start='right')),
RegularPolygon(200*0.45, 150*.6, 10*.6, 3, fill=gradient('red', 'crimson',
22
start='top')),
23 Rect(188*0.45, 340*0.45, 25*0.45, 10*0.45, fill='slateGrey'),
24 Line(188*0.45, 340*0.45, 212*0.45, 340*0.45, fill='gold'),
25 Circle(200*0.45, 210*0.45, 15*0.45, border='grey', borderWidth=4,
26 fill=gradient('powderBlue', 'lightCyan', 'powderBlue',
27 start='right-top'))
28 )
29
30
31 #press spacebar to shoot bullets
32 #score goes up everytime the bullet hits a ghost
33 #up down left right arrow keys to move the rocket
34 #done by Stevie
35 def spaceGun(key):
36 if key == 'space':
37 drawBullet()
38 for ghostShapes in ghost:
39 #
40 if bullet.hitsShape(ghostShapes):
41 ghostShapes.visible=False
42 score.value+=10
43 if (score.value>=500):
44 Label('You Win',200,200,size=50,bold=True,fill='white')
45 rocket.visible=False
46 ghost.visible=False
47 ghostTwo.visible=False
48 bullet.visible=False
about:blank 1/3
4/29/25, 1:04 PM about:blank
49 app.background='black'
50 for ghostEyes in ghostTwo:
51 if bullet.hitsShape(ghostEyes):
52 ghostEyes.visible=False
53 ghostShapes.visible=False
54 score.value+=10
55
56
57 #done by karthik
58 bullet=Group()
59 def drawBullet():
60 bullets=Group(Rect(rocket.centerX+60,rocket.centerY,5,5,fill='yellow'))
61 bullet.add(bullets)
62
63 #done by stevie
64 def onKeyPress(key):
65 spaceGun(key)
66 rocket.rotateAngle+=90
67 def onKeyHold(keys):
68 if ('up'in keys):
69 rocket.centerY-=6.5
70 elif ('down'in keys):
71 rocket.centerY+=6.5
72 if rocket.centerY<=20:
73 rocket.centerY=350
74 elif rocket.centerY>=380:
75 rocket.centerY=50
76 if ('left'in keys):
77 rocket.centerX-=1.5
78 elif ('right'in keys):
79 rocket.centerX+=1.5
80 if rocket.centerX>=210:
81 rocket.centerX=100
82 elif rocket.centerX<=70:
83 rocket.centerX=150
84
85 stars()
86 #done by stevie
87 def onStep():
88 bullet.centerX+=10
89 bigStars.centerX-=3
90 if bigStars.centerX<=80:
91 bigStars.centerX=300
92 ghost.centerX-=2.3
93 ghostTwo.centerX-=2.3
94
95 #done by karthik
96 app.setMaxShapeCount(500000000)
97 ghost=Group()
98 ghostTwo=Group()
99 def ghostEnemy(x,y):
100 #from CMU 3.1.2 ghost colors
101 ghostShapes = Group(
102 Circle((x+100)*0.2, (y+50)*0.2, (100)*0.2),
103 Rect(x*0.2, (y+50)*0.2, 200*0.2, 100*0.2),
104 Circle((x+25)*0.2, (y+150)*0.2, (25)*0.2),
about:blank 2/3
4/29/25, 1:04 PM about:blank
105 Circle((x+175)*0.2, (y+150)*0.2, 25*0.2)
106 )
107 ghost.add(ghostShapes)
108
109 ghostEyes=Group(Circle((x+50)*0.2, (y+50)*0.2, 20*0.2),
110 Circle((x+150)*0.2, (y+50)*0.2, 20*0.2))
111 ghostTwo.add(ghostEyes)
112
113
114 colors = ['blue','green','red','white','gray','pink','purple']
115 #done by karthik
116 for i in range(0,200):
117 ghostEnemy(random.randint(400,400000),random.randint(100,3000))
118 ghost.fill=random.choice(colors)
119
120 ghostTwo.toBack()
121 ghost.toBack()
122
123 score=Label(0,300,20,size=40,fill='white')

about:blank 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy