ROYAL OAK INTERNATIONAL SCHOOL
NEW PALAM VIHAR, SECTOR-110, GURGAON
Practical File
For
2024-2025 Examination
As a part of the Computer Science
Submitted by Submitted to
Name Ms. Ambika Gaur
Roll no.
XII
CERTIFICATE
This is to certify that Cadet ____________________
Roll No:____________ has successfully completed the Practical
Work entitled in the subject Computer Science (083) laid down
in the regulations of CBSE for the purpose of Practical
Examination in Class XII
External Examiner Principal Internal Examiner
Ambika Gaur
INDEX
TEACHER’S
S.NO. PROGRAM
SIGN.
1 Certificate
2 Acknowledgement
3 Introduction
4 Hardware & software specs
5 Flow chart
6 Code input
7 Output
8 Bibliography
ACKNOWLEDGEMENT
Apart from the efforts of me, the success of any project depends largely on the
encouragement and guidelines of many others. I take this opportunity to
express my gratitude to the people who have been instrumental in the
successful completion of this project.
I express deep sense of gratitude to almighty God for giving me strength for
the successful completion of the project.
I express my heartfelt gratitude to my parents for constant encouragement
while carrying out this project.
I gratefully acknowledge the contribution of the individuals who contributed in
bringing this project up to this level, who continues to look after me despite my
flaws,
MEENAKSHI PANDITA ma’am, Who has been continuously motivating and
extending their helping hand to us.
I am overwhelmed to express my thanks to PRATIMA SINGH ma’am for
providing me an infrastructure and moral support while carrying out this project
in the school.
My sincere thanks to AMBIKA GAUR ma’am , Master In-charge, A guide,
Mentor all the above a friend, who critically reviewed my project and helped in
solving each and every problem, occurred during implementation of the project
The guidance and support received from all the members who contributed and
who are contributing to this project, was vital for the success of the project. I
am grateful for their constant support and help.
INTRODUCTION
This Caterpillar Game is a really fun and interactive game made
in Python using the `turtle` graphics library.
In this game, you can control a caterpillar eating green leaves on
a bright yellow background. Each time it eats a leaf, the caterpillar
grows longer and moves more swiftly, making the game much
tougher and exciting. The whole thing is to munch your way
through as many leaves as possible while avoiding the sides of
the screen.
One of the best parts of this game is its scorekeeping system. It
does not only show your current score but also keeps track of the
highest score you've gotten in previous games. High scores are
saved in a CSV file, so even when you close the game and open
it again, your scores will still be there. You can also input your
username to make it more personal and see how well you are
doing.
The controls are super easy! You can use the arrow keys to move
the caterpillar and hit the spacebar to start a new game.
Gameplay is designed to be easy to understand, but it's full of
random leaf placement and rising challenges that keep things
interesting. You can see your current score and best score right
on the screen, updating as you play.
This project really shows what Python can do in terms of making
graphical apps. It combines programming logic, user interaction,
and file saving in a fun way. Whether you just want to have fun or
learn something new, the Caterpillar Game is a great way to
check out what's possible with Python programming.
Hardware
and
Soft ware
• HARDWARE REQUIREMENTS
1. DESKTOP COMPUTER / LAPTOP
2. MOBILE PHONE
• SOFTWARE REQUIREMENTS
1. Python (Latest Version)
2. MySQL
3. MySQL-Connector-Python
FLOW-CHART
Code input
import turtle as t
import random as rd
import csv
import os
# Initialize game variables
CSV_FILE = "caterpillar_highscores.csv"
high_score = 0
game_started = False
# Turtle setup
def setup_turtle_screen():
global caterpillar, leaf, text_turtle, score_turtle,
high_score_turtle
t.bgcolor('yellow')
caterpillar = t.Turtle()
caterpillar.shape('square')
caterpillar.color('red')
caterpillar.speed(0)
caterpillar.penup()
caterpillar.hideturtle()
leaf = t.Turtle()
leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
t.register_shape('leaf', leaf_shape)
leaf.shape('leaf')
leaf.color('green')
leaf.penup()
leaf.hideturtle()
leaf.speed(0)
text_turtle = t.Turtle()
text_turtle.write('Press SPACE to start', align='center',
font=('Arial', 16, 'bold'))
text_turtle.hideturtle()
score_turtle = t.Turtle()
score_turtle.hideturtle()
score_turtle.speed(0)
high_score_turtle = t.Turtle()
high_score_turtle.hideturtle()
high_score_turtle.speed(0)
# Get username
username = input("Enter your username: ")
# Initialize CSV
def initialize_csv():
"""Initialize the CSV file and load the high score."""
global high_score
if not os.path.exists(CSV_FILE):
with open(CSV_FILE, mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Username", "Leaves Eaten"])
else:
with open(CSV_FILE, mode="r") as file:
reader = csv.DictReader(file)
for row in reader:
high_score = max(high_score, int(row["Leaves Eaten"]))
# Save score to CSV
def save_score(leaves_eaten):
"""Save the username and number of leaves eaten to the CSV
file."""
global high_score
high_score = max(high_score, leaves_eaten)
with open(CSV_FILE, mode="a", newline="") as file:
writer = csv.writer(file)
writer.writerow([username, leaves_eaten])
# Check if outside window
def outside_window():
left_wall = -t.window_width() / 2
right_wall = t.window_width() / 2
top_wall = t.window_height() / 2
bottom_wall = -t.window_height() / 2
(x, y) = caterpillar.pos()
outside = x < left_wall or x > right_wall or y < bottom_wall or y
> top_wall
return outside
# Display Game Over
def game_over():
caterpillar.color('yellow')
leaf.color('yellow')
t.penup()
t.hideturtle()
t.write('GAME OVER!', align='center', font=('Arial', 30, 'normal'))
# Display score
def display_score(current_score):
score_turtle.clear()
score_turtle.penup()
x = (t.window_width() / 2) - 50
y = (t.window_height() / 2) - 50
score_turtle.setpos(x, y)
score_turtle.write(str(current_score), align='right', font=('Arial',
40, 'bold'))
# Display high score
def display_high_score():
high_score_turtle.clear()
high_score_turtle.penup()
x = -(t.window_width() / 2) + 50
y = (t.window_height() / 2) - 50
high_score_turtle.setpos(x, y)
high_score_turtle.write(f"High Score: {high_score}", align='left',
font=('Arial', 20, 'bold'))
# Place the leaf
def place_leaf():
leaf.hideturtle()
leaf.setx(rd.randint(-200, 200))
leaf.sety(rd.randint(-200, 200))
leaf.showturtle()
# Start game
def start_game():
global game_started
if game_started:
return
game_started = True
score = 0
text_turtle.clear()
caterpillar_speed = 2
caterpillar_length = 3
caterpillar.shapesize(1, caterpillar_length, 1)
caterpillar.showturtle()
display_score(score)
display_high_score()
place_leaf()
while True:
caterpillar.forward(caterpillar_speed)
if caterpillar.distance(leaf) < 20:
place_leaf()
caterpillar_length += 1
caterpillar.shapesize(1, caterpillar_length, 1)
caterpillar_speed += 1
score += 10 # Award 10 points for each leaf eaten
display_score(score)
display_high_score()
if outside_window():
game_over()
save_score(score)
break
# Movement controls
def move_up():
if caterpillar.heading() == 0 or caterpillar.heading() == 180:
caterpillar.setheading(90)
def move_down():
if caterpillar.heading() == 0 or caterpillar.heading() == 180:
caterpillar.setheading(270)
def move_left():
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
caterpillar.setheading(180)
def move_right():
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
caterpillar.setheading(0)
# Initialize game and bind keys
initialize_csv()
# Setup turtle screen after username input
setup_turtle_screen()
# Bind keys and start listening
t.onkey(start_game, 'space')
t.onkey(move_up, 'Up')
t.onkey(move_right, 'Right')
t.onkey(move_down, 'Down')
t.onkey(move_left, 'Left')
t.listen()
# Start the main loop
t.mainloop()
outputs
Program asked user for username
User entered name
Asked user to start game
User playing game
Game ending upon touching wall
CSV file created and stored data
Data stored in CSV file
BIBLIOGRAPHY
1. CLASS 11 & 12 SUMITA ARORA C.S BOOK
2. GOOGLE
3. BLACKBOX AI
4. WIKIPEDIA
5. PYTHON (www.python.com)
6. MYSQL (www.mysql.com)