-
Notifications
You must be signed in to change notification settings - Fork 72
snake_game #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Punit-Choudhary
merged 7 commits into
Punit-Choudhary:main
from
Anmol-Sharma21:snake_game
Apr 2, 2024
Merged
snake_game #275
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ad586e4
snake_game
Anmol-Sharma21 ed372fe
made the changes
Anmol-Sharma21 7798006
removed the ds_store files
Anmol-Sharma21 a4128ba
fint the code
Anmol-Sharma21 4c01ff0
made the changes
Anmol-Sharma21 fd7c9df
removed extra files
Anmol-Sharma21 88554bf
made the changes
Anmol-Sharma21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Snake Game using Python | ||
|
||
## Description | ||
This is a classic Snake game implemented in Python using the turtle library for GUI. The game allows the player to control a snake which moves around the screen, eating food pellets and growing longer. The objective is to avoid colliding with the walls or the snake's own body while trying to eat as much food as possible to score points. | ||
|
||
## Features | ||
- Classic Snake gameplay | ||
- Simple and intuitive GUI using Turtle | ||
- Score tracking | ||
- Responsive controls | ||
|
||
## Requirements | ||
- Python 3.x | ||
- Turtle library | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
15 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from turtle import Turtle | ||
import random | ||
|
||
|
||
class Food(Turtle): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.shape("circle") | ||
self.penup() | ||
self.shapesize(stretch_len=0.5, stretch_wid=0.5) | ||
self.color("blue") | ||
self.speed("fastest") | ||
self.refresh() | ||
|
||
def refresh(self): | ||
random_x = random.randint(-280, 280) | ||
random_y = random.randint(-280, 280) | ||
self.goto(random_x, random_y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import time | ||
from turtle import Screen | ||
from snake import Snake | ||
from food import Food | ||
from scoreboard import Scoreboard | ||
|
||
# Set up the screen | ||
screen = Screen() | ||
screen.setup(width=600, height=600) | ||
screen.bgcolor("black") | ||
screen.title("Snake Game") | ||
screen.tracer(0) | ||
|
||
snake = Snake() | ||
food = Food() | ||
scoreboard = Scoreboard() | ||
|
||
screen.listen() | ||
screen.onkey(snake.up, "Up") | ||
screen.onkey(snake.down, "Down") | ||
screen.onkey(snake.left, "Left") | ||
screen.onkey(snake.right, "Right") | ||
|
||
game_is_on = True | ||
while game_is_on: | ||
screen.update() | ||
time.sleep(0.1) | ||
snake.move() | ||
|
||
if snake.head.distance(food) < 15: | ||
food.refresh() | ||
snake.extend() | ||
scoreboard.inc_score() | ||
|
||
if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280: | ||
scoreboard.reset() | ||
snake.reset() | ||
|
||
for segment in snake.segments[1:]: | ||
if snake.head.distance(segment) < 15: | ||
scoreboard.reset() | ||
snake.reset() | ||
|
||
|
||
|
||
|
||
screen.exitonclick() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from turtle import Turtle | ||
|
||
FONT = ("courier", 24, "normal") | ||
ALIGNMENT = "center" | ||
|
||
|
||
class Scoreboard(Turtle): | ||
def __init__(self): | ||
super().__init__() | ||
self.score = 0 | ||
with open("data.txt") as data: | ||
self.high_score = int(data.read()) | ||
self.color("white") | ||
self.penup() | ||
self.goto(0, 270) | ||
self.write(f"Score : {self.score} High Score : {self.high_score}", align=ALIGNMENT, font=FONT) | ||
self.hideturtle() | ||
|
||
def update_scorebaord(self): | ||
self.clear() | ||
self.write(f"Score : {self.score} High Score : {self.high_score}", align=ALIGNMENT, font=FONT) | ||
|
||
def reset(self): | ||
if self.score > self.high_score: | ||
self.high_score = self.score | ||
with open("data.txt", mode="w") as data: | ||
data.write(f"{self.high_score}") | ||
self.score = 0 | ||
self.update_scorebaord() | ||
|
||
# def game_over(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused code |
||
# self.goto(0,0) | ||
# self.write(f"GAME OVER", align=ALIGNMENT, font=FONT) | ||
|
||
def inc_score(self): | ||
self.score += 1 | ||
self.clear() | ||
self.update_scorebaord() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from turtle import * | ||
|
||
STARTING_POSITION = [(0, 0), (-20, 0), (-40, 0)] | ||
MOVE_DISTANCE = 20 | ||
UP = 90 | ||
DOWN = 270 | ||
LEFT = 180 | ||
RIGHT = 0 | ||
|
||
|
||
class Snake: | ||
|
||
def __init__(self): | ||
self.segments = [] | ||
self.create_snake() | ||
self.head = self.segments[0] | ||
|
||
def create_snake(self): | ||
for position in STARTING_POSITION: | ||
self.add_seg(position) | ||
|
||
|
||
def add_seg(self, position): | ||
seg_new = Turtle("square") | ||
seg_new.color("white") | ||
seg_new.penup() | ||
seg_new.goto(position) | ||
self.segments.append(seg_new) | ||
|
||
|
||
def reset(self): | ||
for seg in self.segments: | ||
seg.goto(1000, 1000) | ||
self.segments.clear() | ||
self.create_snake() | ||
self.head = self.segments[0] | ||
|
||
def extend(self): | ||
self.add_seg(self.segments[-1].position()) | ||
|
||
|
||
|
||
def move(self): | ||
for seg_num in range(len(self.segments) - 1, 0, -1): | ||
new_x = self.segments[seg_num - 1].xcor() | ||
new_y = self.segments[seg_num - 1].ycor() | ||
self.segments[seg_num].goto(new_x, new_y) | ||
self.head.forward(MOVE_DISTANCE) | ||
|
||
def up(self): | ||
if self.head.heading() != DOWN: | ||
self.head.setheading(UP) | ||
pass | ||
|
||
def down(self): | ||
if self.head.heading() != UP: | ||
self.head.setheading(DOWN) | ||
pass | ||
|
||
def left(self): | ||
if self.head.heading() != RIGHT: | ||
self.head.setheading(LEFT) | ||
pass | ||
|
||
def right(self): | ||
if self.head.heading() != LEFT: | ||
self.head.setheading(RIGHT) | ||
pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset high score to 0