Skip to content

Commit 13016b3

Browse files
authored
Merge pull request #15 from Rohit-R2000/main
added games in python language
2 parents c9c420e + 54f0998 commit 13016b3

File tree

13 files changed

+358
-0
lines changed

13 files changed

+358
-0
lines changed

Snake_Game/data.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3

Snake_Game/food.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from turtle import Turtle
2+
from random import Random
3+
class Food(Turtle):
4+
5+
def __int__(self):
6+
super().__int__()
7+
8+
def create_food(self):
9+
self.shape("turtle")
10+
self.color("purple")
11+
12+
self.penup()
13+
self.shapesize(stretch_len=0.5,stretch_wid=0.5)
14+
self.refresh()
15+
16+
def refresh(self):
17+
random = Random()
18+
self.goto(random.randint(-290, 290), random.randint(-290, 250))

Snake_Game/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from turtle import Turtle,Screen
2+
from snake import Snake
3+
from food import Food
4+
from scoreboard import Scoreboard
5+
import time
6+
7+
screen=Screen()
8+
food=Food()
9+
screen.setup(600,600)
10+
screen.title("My Snake Game")
11+
screen.bgcolor("black")
12+
snake=Snake()
13+
14+
screen.tracer(0)
15+
screen.listen()
16+
17+
screen.onkey(snake.up,key="Up")
18+
screen.onkey(snake.down,key="Down")
19+
screen.onkey(snake.right,key="Left")
20+
screen.onkey(snake.left,key="Right")
21+
game_on=True
22+
food.create_food()
23+
scoreboard=Scoreboard()
24+
while game_on:
25+
screen.update()
26+
time.sleep(.2)
27+
snake.move()
28+
if snake.turtle_list[0].distance(food)<15:
29+
food.refresh()
30+
snake.increase_size()
31+
scoreboard.increase_score()
32+
if snake.turtle_list[0].xcor()>290 or snake.turtle_list[0].xcor()<-290 or snake.turtle_list[0].ycor()>290 or snake.turtle_list[0].ycor()<-290:
33+
game_on=False
34+
scoreboard.restart()
35+
scoreboard.game_over()
36+
37+
for tail in snake.turtle_list[1:]:
38+
if snake.turtle_list[0].distance(tail) < 10 :
39+
game_on=False
40+
scoreboard.restart()
41+
scoreboard.game_over()
42+
screen.exitonclick()

Snake_Game/scoreboard.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from turtle import Turtle
2+
3+
class Scoreboard(Turtle):
4+
def __init__(self):
5+
super().__init__()
6+
with open("data.txt") as file:
7+
self.highest_score=int(file.read())
8+
self.total=0
9+
self.color("white")
10+
self.penup()
11+
self.goto(0,260)
12+
self.hideturtle()
13+
self.update_score()
14+
def update_score(self):
15+
self.clear()
16+
self.write(f"SCORE = {self.total} HIGH SCORE = {self.highest_score}", align="center", font=("Courier", 14, "normal"))
17+
def game_over(self):
18+
self.goto(0,0)
19+
self.color("yellow")
20+
self.write("Game Over",align='center',font=("Courier", 14, "normal"))
21+
def increase_score(self):
22+
self.total+=1
23+
self.update_score()
24+
25+
def restart(self):
26+
if self.total>self.highest_score:
27+
self.highest_score=self.total
28+
with open("data.txt",mode="w") as file:
29+
file.write(f"{self.total}")
30+
self.total=0
31+
self.update_score()

Snake_Game/snake.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from turtle import Turtle
2+
from food import Food
3+
4+
class Snake:
5+
6+
def __init__(self):
7+
self.turtle_list = []
8+
self.x = 0
9+
self.extend((0, 0))
10+
self.create_snake()
11+
12+
13+
def create_snake(self):
14+
for _ in range(0, 2):
15+
self.extend(self.turtle_list[-1].position())
16+
def extend(self,position):
17+
tim = Turtle()
18+
tim.penup()
19+
tim.setpos(position)
20+
self.x -= 20
21+
tim.color("white")
22+
tim.shape("square")
23+
self.turtle_list.append(tim)
24+
def increase_size(self):
25+
self.extend(self.turtle_list[-1].position())
26+
def move(self):
27+
for seg_each in range(len(self.turtle_list) - 1, 0, -1):
28+
self.turtle_list[seg_each].goto(self.turtle_list[seg_each - 1].position())
29+
self.turtle_list[0].forward(20)
30+
def reset(self):
31+
for _ in self.turtle_list:
32+
_.color("black")
33+
self.turtle_list.clear()
34+
self.extend((0, 0))
35+
self.create_snake()
36+
def up(self):
37+
if self.turtle_list[0].heading()!=270:
38+
self.turtle_list[0].setheading(90)
39+
def down(self):
40+
if self.turtle_list[0].heading()!=90:
41+
self.turtle_list[0].setheading(270)
42+
def left(self):
43+
if self.turtle_list[0].heading()!=180:
44+
self.turtle_list[0].setheading(0)
45+
def right(self):
46+
if self.turtle_list[0].heading()!=0:
47+
self.turtle_list[0].setheading(180)

car_manager.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from turtle import Turtle
2+
import random
3+
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
4+
STARTING_MOVE_DISTANCE = 4
5+
MOVE_INCREMENT = 5
6+
7+
class CarManager:
8+
def __init__(self):
9+
self.all_cars = []
10+
self.speed=STARTING_MOVE_DISTANCE
11+
def create_car(self):
12+
cool=random.randint(1,7)
13+
if cool==1:
14+
tim=Turtle()
15+
tim.penup()
16+
tim.shape("square")
17+
tim.shapesize(stretch_wid=1,stretch_len=2)
18+
tim.color(random.choice(COLORS))
19+
tim.goto(300,random.randint(-250,250))
20+
tim.setheading(180)
21+
self.all_cars.append(tim)
22+
23+
def move_all(self):
24+
for _ in self.all_cars:
25+
_.forward(self.speed)
26+
def increase_speed(self):
27+
self.speed+=MOVE_INCREMENT

ping_pong/ball.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from turtle import Turtle
2+
3+
class Ball(Turtle):
4+
def __init__(self):
5+
super().__init__()
6+
self.xmove = 10
7+
self.ymove = 10
8+
self.penup()
9+
self.shape("circle")
10+
self.color("white")
11+
12+
13+
def move(self):
14+
new_x=self.xcor()+self.xmove
15+
new_y = self.ycor() + self.ymove
16+
self.goto(new_x,new_y)
17+
18+
def bounce(self):
19+
self.ymove *= -1
20+
21+
def bounce_pad(self):
22+
self.xmove *= -1
23+
def refresh(self):
24+
self.goto(0,0)
25+
self.xmove *=-1
26+
self.ymove *=-1
27+

ping_pong/main.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from turtle import Turtle,Screen
2+
from paddle import Paddle
3+
from ball import Ball
4+
from scoreboard import Scoreboard
5+
import time
6+
screen=Screen()
7+
tim=Turtle()
8+
k=.1
9+
is_ = True
10+
scoreboard=Scoreboard()
11+
screen.tracer(0)
12+
screen.setup(900,600)
13+
screen.bgcolor("black")
14+
screen.title("PING PONG")
15+
paddle_r=Paddle()
16+
paddle_l=Paddle()
17+
ball=Ball()
18+
screen.listen()
19+
paddle_r.create_bars((420,0))
20+
paddle_l.create_bars((-420,0))
21+
game_on=True
22+
def resume():
23+
if ball.xcor() > 460:
24+
scoreboard.total_l += 1
25+
if ball.xcor() < -460:
26+
scoreboard.total_r += 1
27+
scoreboard.update()
28+
tim.clear()
29+
global k
30+
k = .1
31+
ball.refresh()
32+
33+
34+
while game_on:
35+
scoreboard.update()
36+
time.sleep(k)
37+
screen.update()
38+
screen.onkey(fun=paddle_r.up,key="Up")
39+
screen.onkey(fun=paddle_l.up, key="w")
40+
screen.onkey(fun=paddle_r.down,key="Down")
41+
screen.onkey(fun=paddle_l.down, key="s")
42+
ball.move()
43+
if ball.ycor()>279 or ball.ycor()<-275:
44+
ball.bounce()
45+
if ball.xcor()>390 and ball.distance(paddle_r)<50 or ball.xcor()<-390 and ball.distance(paddle_l)<50:
46+
ball.bounce_pad()
47+
k/=1.2
48+
if ball.xcor()>460 or ball.xcor()<-460:
49+
tim.color("white")
50+
tim.hideturtle()
51+
tim.write("Press Space to Resume",align="center",font=("Algerian",30,"normal"))
52+
screen.onkey(fun=resume, key="space")
53+
54+
screen.exitonclick()

ping_pong/paddle.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from turtle import Turtle,Screen
2+
3+
class Paddle(Turtle):
4+
5+
def __init__(self):
6+
super().__init__()
7+
8+
def create_bars(self,position):
9+
self.penup()
10+
self.color("white")
11+
self.shape("square")
12+
self.shapesize(stretch_len=1, stretch_wid=6)
13+
self.goto(position)
14+
15+
def down(self):
16+
y_cor=self.ycor()-30
17+
self.goto(self.xcor(),y_cor)
18+
19+
def up(self):
20+
y_cor = self.ycor() + 30
21+
self.goto(self.xcor(), y_cor)
22+
23+

ping_pong/scoreboard.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from turtle import Turtle
2+
3+
class Scoreboard(Turtle):
4+
def __init__(self):
5+
super().__init__()
6+
self.total_r=0
7+
self.total_l=0
8+
self.penup()
9+
self.hideturtle()
10+
self.color("white")
11+
self.goto(0,250)
12+
13+
def update(self):
14+
self.clear()
15+
self.write(f"{self.total_l} {self.total_r}", align="center", font=("Courier", 26, "normal"))

0 commit comments

Comments
 (0)