Skip to content

hey there added a snake game hoping for a merge #19

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added pingpong/__pycache__/ball.cpython-310.pyc
Binary file not shown.
Binary file added pingpong/__pycache__/play.cpython-310.pyc
Binary file not shown.
Binary file added pingpong/__pycache__/scoreboard.cpython-310.pyc
Binary file not shown.
24 changes: 24 additions & 0 deletions pingpong/ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.speed("slowest")
self.shape("circle")
self.color("white")
self.xmove= 10
self.ymove= 10
self.move_speed =0.1
def move(self):
newx= self.xcor()+self.xmove
newy= self.ycor()+self.ymove
self.goto(newx,newy)
def reflect(self):
self.ymove *= -1
def bounce(self):
self.xmove *= -1
self.move_speed *= 0.9
def refresh(self):
self.goto(0,0)
self.move_speed = 0.1
self.xmove *= -1
48 changes: 48 additions & 0 deletions pingpong/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from turtle import Turtle ,Screen
from play import Game
from ball import Ball
from scoreboard import Score
import time
screen = Screen()
paddle1 = Game((350,0))
paddle2 = Game((-350,0))
ball = Ball()
score = Score()
screen.tracer(0)

paddle1.goto(350,0)
paddle2.goto(-350,0)

screen.setup(width=800,height=600)
screen.bgcolor("black")
screen.title("PONG")


screen.listen()
if paddle1.xcor()<390 or paddle2.ycor()<-350:
screen.onkey(fun=paddle1.goup,key="Up")
screen.onkey(fun=paddle1.godown,key="Down")
screen.onkey(fun=paddle2.goup,key="w")
screen.onkey(fun=paddle2.godown,key="s")

game_on = True
while game_on:
time.sleep(ball.move_speed)
ball.move()
if ball.ycor()>280 or ball.ycor()<-280:
ball.reflect()
if ball.distance(paddle1)<40 or ball.distance(paddle2)<40:
ball.bounce()

if ball.xcor()>400 :
ball.refresh()
score.goto(-300, 250)
score.currentleftscr()
if ball.xcor() < -400:
ball.refresh()
score.goto(300, 250)
score.currentrightscr()
screen.update()


screen.exitonclick()
18 changes: 18 additions & 0 deletions pingpong/play.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from turtle import Turtle , Screen

class Game(Turtle) :
def __init__(self,position):
super().__init__()
self.penup()
self.color("white")
self.shape("square")
self.shapesize(4, 1)
self.goto(position)
def goup(self):
newy = self.ycor()+20
self.goto(self.xcor(),newy)
def godown (self):
newy = self.ycor()-20
self.goto(self.xcor(),newy)


28 changes: 28 additions & 0 deletions pingpong/scoreboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from turtle import Turtle
class Score(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.hideturtle()
self.color("white")
self.lscr=0
self.rscr=0


self.updatescr()

def updatescr(self):
self.goto(-100, 200)
self.write(f"{self.lscr}", True, align="left", font=("arial", 24, "normal"))
self.goto(100, 200)
self.write(f"{self.rscr}", True, align="right", font=("arial", 24, "normal"))

def currentleftscr(self):
self.lscr+=1
self.clear()
self.updatescr()
def currentrightscr(self):
self.rscr+=1
self.clear()
self.updatescr()

3 changes: 3 additions & 0 deletions snakegame/snake_game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# snake_game
classic snake game "eat more to grow more"
a basic snake game
Binary file not shown.
Binary file not shown.
Binary file not shown.
38 changes: 38 additions & 0 deletions snakegame/snake_game/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from turtle import Turtle , Screen
import time

screen= Screen()

screen.setup(width = 600,height=600)
screen.bgcolor("black")
screen.title("my game")
screen.tracer(0)
# ye humne body bana li
body = [(0,0),(-20,0),(-40,0)]
segment=[]
for parts in body:
moti = Turtle()
moti.color("white")
moti.shape("square")
moti.penup()
moti.goto(parts)
segment.append(moti)


game_on = True
while game_on :
screen.update()
time.sleep(0.1)
# ab snake ko turn karane ki liye hum ise ek tarike se link karenge iske co_ordinate se
for seg in range (len(segment)-1,0,-1):
new_xpos= segment[seg-1].xcor()
new_ypos= segment[seg-1].ycor()
segment[seg].goto(new_xpos,new_ypos)
# ab hum first part ko control kar rahe hai aur pura snake control ho raha hai
segment[0].forward(20)
segment[0].left(90)




screen.exitonclick()
1 change: 1 addition & 0 deletions snakegame/snake_game/data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
18 changes: 18 additions & 0 deletions snakegame/snake_game/food.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from turtle import Turtle
import random
# ab hum turtle class inherit karenge
class Food(Turtle):

def __init__(self):
super().__init__()
self.shape("circle")
self.color("blue")
self.newfood()
self.penup()
self.shapesize(stretch_len=0.5,stretch_wid=0.5)
self.speed("fastest")
def newfood(self):
self.penup()
xcor= random.randint(-280,280)
ycor= random.randint(-280,280)
self.goto(xcor,ycor)
Empty file.
49 changes: 49 additions & 0 deletions snakegame/snake_game/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from turtle import Turtle , Screen
import time
from snake import Snake
from food import Food
from scorecard import Score
screen= Screen()
score=Score()

screen.setup(width = 600,height=600)
screen.bgcolor("black")
screen.title("my game")
screen.tracer(0)

snake =Snake()
food =Food()
screen.listen()
screen.onkey(snake.up,"Up")
screen.onkey(snake.down,"Down")
screen.onkey(snake.left,"Left")
screen.onkey(snake.right,"Right")

game_on = True
while game_on :
screen.update()
time.sleep(0.1)
snake.move()
#now to detect the collision
if snake.head.distance(food)<15:
food.newfood()
snake.extend()
score.currentscore()
# collision with the tail
if snake.head.xcor()>280 or snake.head.xcor()<-280 or snake.head.ycor()>280 or snake.head.ycor()<-280:
score.renew()
snake.new()


# collision with the tail
for i in snake.segment[1:]:
if i == snake.head:
pass
elif snake.head.distance(i)<10:
score.renew()
snake.new()




screen.exitonclick()
36 changes: 36 additions & 0 deletions snakegame/snake_game/scorecard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from turtle import Turtle
ALIGNMENT="center"
FONT=('joker', 24, 'normal')

class Score(Turtle):
def __init__(self):

self.scoreval =0

with open("data", mode="r") as file:
self.highscore = int(file.read())


# file= open("data")
# data=file.read()
# self.data=int(data)
super().__init__()
self.color("white")
self.penup()
self.goto(x=0, y=250)
self.hideturtle()
self.updatescore()
def updatescore(self):
self.clear()
self.write(f"SCORE:{self.scoreval} HIGH SCORE:{self.highscore}", align=ALIGNMENT, font=FONT)
def currentscore(self):
self.scoreval+=1
self.clear()
self.updatescore()
def renew(self):
if self.scoreval>self.highscore:
self.highscore = self.scoreval
with open("data", mode="w") as change:
change.write(str(self.scoreval))
self.scoreval=0
self.updatescore()
55 changes: 55 additions & 0 deletions snakegame/snake_game/snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ab hum snake ki class banaenge jo humar acoe tidy kar degi
from turtle import Turtle
BODY = [(0, 0), (-20, 0), (-40, 0)]
STEP = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
SHAPE="square"
COLOR="white"
class Snake :
def __init__(self):
self.segment=[]
self.createsnake()
self.head = self.segment[0]

def createsnake(self):
for parts in BODY:
self.addseg(parts)
def new(self):
for i in self.segment:
i.goto(700,700)
self.segment.clear()
self.createsnake()
self.head = self.segment[0]
def addseg(self,parts):
# is wale function se segment add ho rahe hai
moti = Turtle()
moti.color(COLOR)
moti.shape(SHAPE)
moti.penup()
moti.goto(parts)
self.segment.append(moti)
def extend(self):
self.addseg(self.segment[-1].position())
def move(self):
for seg in range(len(self.segment) - 1, 0, -1):
new_xpos = self.segment[seg - 1].xcor()
new_ypos = self.segment[seg - 1].ycor()
self.segment[seg].goto(new_xpos, new_ypos)
self.head.forward(STEP)
def up(self):
if self.head.heading() != DOWN:
self.head.setheading(UP)
def down(self):
if self.head.heading() != UP:
self.head.setheading(DOWN)
def left(self):
if self.head.heading() != RIGHT:
self.head.setheading(LEFT)
def right(self):
if self.head.heading() != LEFT:
self.head.setheading(RIGHT)