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

Rocket vs. Ghosts Game 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)
43 views3 pages

Rocket vs. Ghosts Game 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

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

Sandbox | CMU CS Academy


1 [Link]='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 [Link](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 [Link](ghostShapes):
41 [Link]=False
42 [Link]+=10
43 if ([Link]>=500):
44 Label('You Win',200,200,size=50,bold=True,fill='white')
45 [Link]=False
46 [Link]=False
47 [Link]=False
48 [Link]=False
about:blank 1/3
4/29/25, 1:04 PM about:blank
49 [Link]='black'
50 for ghostEyes in ghostTwo:
51 if [Link](ghostEyes):
52 [Link]=False
53 [Link]=False
54 [Link]+=10
55
56
57 #done by karthik
58 bullet=Group()
59 def drawBullet():
60 bullets=Group(Rect([Link]+60,[Link],5,5,fill='yellow'))
61 [Link](bullets)
62
63 #done by stevie
64 def onKeyPress(key):
65 spaceGun(key)
66 [Link]+=90
67 def onKeyHold(keys):
68 if ('up'in keys):
69 [Link]-=6.5
70 elif ('down'in keys):
71 [Link]+=6.5
72 if [Link]<=20:
73 [Link]=350
74 elif [Link]>=380:
75 [Link]=50
76 if ('left'in keys):
77 [Link]-=1.5
78 elif ('right'in keys):
79 [Link]+=1.5
80 if [Link]>=210:
81 [Link]=100
82 elif [Link]<=70:
83 [Link]=150
84
85 stars()
86 #done by stevie
87 def onStep():
88 [Link]+=10
89 [Link]-=3
90 if [Link]<=80:
91 [Link]=300
92 [Link]-=2.3
93 [Link]-=2.3
94
95 #done by karthik
96 [Link](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 [Link](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 [Link](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([Link](400,400000),[Link](100,3000))
118 [Link]=[Link](colors)
119
120 [Link]()
121 [Link]()
122
123 score=Label(0,300,20,size=40,fill='white')

about:blank 3/3

Common questions

Powered by AI

The code efficiently handles rendering and management of numerous ghost and star elements by using the concept of grouping. It utilizes groups to manage multiple graphical objects as single entities, which allows for easier manipulation and rendering . This includes creating ghost and star groups and moving them as collective units, reducing computational overhead by streamlining the number of individual rendering calls needed . The use of random positioning and coloring for ghosts further adds variety without additional complexity, making the rendering efficient and visually dynamic .

The color variations of the ghosts, which include blue, green, red, white, gray, pink, and purple, add visual interest and variety, making the game aesthetically pleasing and engaging to play . This diversity in ghost colors prevents monotony, enhances the player’s visual experience, and maintains engagement by presenting a dynamic and lively game environment . Such visual elements are crucial in keeping players interested and invested in game play .

The game code ensures challenge and engagement by incorporating a scoring system where the score increases by 10 points every time a bullet hits a ghost, promoting player activity . The game dynamically increases difficulty as players must also manage the movement of ghosts and rockets using arrow keys while shooting bullets using the spacebar, thus requiring quick reflexes and strategic thinking . Additionally, the winning condition is set at a 500-point threshold, providing a clear goal and enhancing player engagement through a sense of achievement .

Visual elements like stars and ghosts are dynamically managed through the game loop and event handlers. Stars are instantiated as a group and move horizontally across the screen, resetting their position when they reach a threshold, thus creating a continuous background animation . Ghosts are generated in large numbers, with their movement also being handled dynamically as they move from right to left, and their visibility and scoring are managed when they collide with bullets . This dynamic management creates an engaging visual and interactive experience .

The developers likely considered performance optimization by using efficient rendering techniques, such as grouping graphical elements to minimize rendering calls and collisions checks . Utilizing transformations like scaling and rotating graphics to maintain consistency without overloading the processing capabilities may have been considered to enhance performance . Additionally, setting a high maximum shape count and randomizing ghost positions, which minimizes repetitive computational tasks, ensures smoother gameplay across different hardware by distributing processing over multiple frames .

The game code uses conditional logic to determine the endgame scenario by checking the player's score within the `spaceGun` function. When the score reaches 500, a 'You Win' label is displayed, and all interactive elements like the rocket and the ghosts become invisible, effectively ending the game . This clear condition provides a defined conclusion to gameplay, ensuring players have a tangible goal to work towards, which is crucial for maintaining engagement and satisfaction .

The rocket's movement mechanics significantly contribute to the gameplay experience by allowing players to maneuver freely within the game's space using arrow keys, providing a sense of control and engagement . This mobility is crucial for dodging enemy ghosts and positioning the rocket strategically for shooting, which adds a layer of skill and strategy to the gameplay. Limiting the rocket's movement within specified bounds keeps the focus on interaction with other game elements and maintains the game's challenge .

The code utilizes event-driven strategies to handle user input for game control. The `onKeyPress` function detects when the spacebar is pressed to shoot bullets . The `onKeyHold` function continuously checks for arrow key inputs to move the rocket, with specified adjustments for the rocket's position to enable smooth and reactive control . These functions create an interactive environment where users can dynamically influence the game state through keyboard inputs, enhancing the gaming experience .

The spaceGun function is pivotal for player interaction as it triggers the creation and firing of bullets when the spacebar is pressed . It interacts with other game elements by checking collisions between bullets and ghost shapes, making ghost shapes invisible and incrementing the score if a ghost is hit . The function is critical for progression and gameplay, as destroying ghosts is directly tied to scoring and reaching the win condition .

The code ensures synchronization of bullet graphics and interactions by linking the bullet creation and movement to the `spaceGun` function triggered by the spacebar press . Each bullet's movement is managed within the `onStep` function, which incrementally adjusts their position, ensuring they move quickly and smoothly, providing immediate feedback to player actions . This tight coupling between keyboard actions and visual feedback creates responsive gameplay, essential for maintaining player engagement .

You might also like