diff --git a/Snake_Game/data.txt b/Snake_Game/data.txt new file mode 100644 index 0000000..e440e5c --- /dev/null +++ b/Snake_Game/data.txt @@ -0,0 +1 @@ +3 \ No newline at end of file diff --git a/Snake_Game/food.py b/Snake_Game/food.py new file mode 100644 index 0000000..06783ae --- /dev/null +++ b/Snake_Game/food.py @@ -0,0 +1,18 @@ +from turtle import Turtle +from random import Random +class Food(Turtle): + + def __int__(self): + super().__int__() + + def create_food(self): + self.shape("turtle") + self.color("purple") + + self.penup() + self.shapesize(stretch_len=0.5,stretch_wid=0.5) + self.refresh() + + def refresh(self): + random = Random() + self.goto(random.randint(-290, 290), random.randint(-290, 250)) diff --git a/Snake_Game/main.py b/Snake_Game/main.py new file mode 100644 index 0000000..cbac7a9 --- /dev/null +++ b/Snake_Game/main.py @@ -0,0 +1,42 @@ +from turtle import Turtle,Screen +from snake import Snake +from food import Food +from scoreboard import Scoreboard +import time + +screen=Screen() +food=Food() +screen.setup(600,600) +screen.title("My Snake Game") +screen.bgcolor("black") +snake=Snake() + +screen.tracer(0) +screen.listen() + +screen.onkey(snake.up,key="Up") +screen.onkey(snake.down,key="Down") +screen.onkey(snake.right,key="Left") +screen.onkey(snake.left,key="Right") +game_on=True +food.create_food() +scoreboard=Scoreboard() +while game_on: + screen.update() + time.sleep(.2) + snake.move() + if snake.turtle_list[0].distance(food)<15: + food.refresh() + snake.increase_size() + scoreboard.increase_score() + 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: + game_on=False + scoreboard.restart() + scoreboard.game_over() + + for tail in snake.turtle_list[1:]: + if snake.turtle_list[0].distance(tail) < 10 : + game_on=False + scoreboard.restart() + scoreboard.game_over() +screen.exitonclick() diff --git a/Snake_Game/scoreboard.py b/Snake_Game/scoreboard.py new file mode 100644 index 0000000..59606f4 --- /dev/null +++ b/Snake_Game/scoreboard.py @@ -0,0 +1,31 @@ +from turtle import Turtle + +class Scoreboard(Turtle): + def __init__(self): + super().__init__() + with open("data.txt") as file: + self.highest_score=int(file.read()) + self.total=0 + self.color("white") + self.penup() + self.goto(0,260) + self.hideturtle() + self.update_score() + def update_score(self): + self.clear() + self.write(f"SCORE = {self.total} HIGH SCORE = {self.highest_score}", align="center", font=("Courier", 14, "normal")) + def game_over(self): + self.goto(0,0) + self.color("yellow") + self.write("Game Over",align='center',font=("Courier", 14, "normal")) + def increase_score(self): + self.total+=1 + self.update_score() + + def restart(self): + if self.total>self.highest_score: + self.highest_score=self.total + with open("data.txt",mode="w") as file: + file.write(f"{self.total}") + self.total=0 + self.update_score() diff --git a/Snake_Game/snake.py b/Snake_Game/snake.py new file mode 100644 index 0000000..1af712c --- /dev/null +++ b/Snake_Game/snake.py @@ -0,0 +1,47 @@ +from turtle import Turtle +from food import Food + +class Snake: + + def __init__(self): + self.turtle_list = [] + self.x = 0 + self.extend((0, 0)) + self.create_snake() + + + def create_snake(self): + for _ in range(0, 2): + self.extend(self.turtle_list[-1].position()) + def extend(self,position): + tim = Turtle() + tim.penup() + tim.setpos(position) + self.x -= 20 + tim.color("white") + tim.shape("square") + self.turtle_list.append(tim) + def increase_size(self): + self.extend(self.turtle_list[-1].position()) + def move(self): + for seg_each in range(len(self.turtle_list) - 1, 0, -1): + self.turtle_list[seg_each].goto(self.turtle_list[seg_each - 1].position()) + self.turtle_list[0].forward(20) + def reset(self): + for _ in self.turtle_list: + _.color("black") + self.turtle_list.clear() + self.extend((0, 0)) + self.create_snake() + def up(self): + if self.turtle_list[0].heading()!=270: + self.turtle_list[0].setheading(90) + def down(self): + if self.turtle_list[0].heading()!=90: + self.turtle_list[0].setheading(270) + def left(self): + if self.turtle_list[0].heading()!=180: + self.turtle_list[0].setheading(0) + def right(self): + if self.turtle_list[0].heading()!=0: + self.turtle_list[0].setheading(180) \ No newline at end of file diff --git a/car_manager.py b/car_manager.py new file mode 100644 index 0000000..14e70f1 --- /dev/null +++ b/car_manager.py @@ -0,0 +1,27 @@ +from turtle import Turtle +import random +COLORS = ["red", "orange", "yellow", "green", "blue", "purple"] +STARTING_MOVE_DISTANCE = 4 +MOVE_INCREMENT = 5 + +class CarManager: + def __init__(self): + self.all_cars = [] + self.speed=STARTING_MOVE_DISTANCE + def create_car(self): + cool=random.randint(1,7) + if cool==1: + tim=Turtle() + tim.penup() + tim.shape("square") + tim.shapesize(stretch_wid=1,stretch_len=2) + tim.color(random.choice(COLORS)) + tim.goto(300,random.randint(-250,250)) + tim.setheading(180) + self.all_cars.append(tim) + + def move_all(self): + for _ in self.all_cars: + _.forward(self.speed) + def increase_speed(self): + self.speed+=MOVE_INCREMENT \ No newline at end of file diff --git a/ping_pong/ball.py b/ping_pong/ball.py new file mode 100644 index 0000000..91af8d1 --- /dev/null +++ b/ping_pong/ball.py @@ -0,0 +1,27 @@ +from turtle import Turtle + +class Ball(Turtle): + def __init__(self): + super().__init__() + self.xmove = 10 + self.ymove = 10 + self.penup() + self.shape("circle") + self.color("white") + + + def move(self): + new_x=self.xcor()+self.xmove + new_y = self.ycor() + self.ymove + self.goto(new_x,new_y) + + def bounce(self): + self.ymove *= -1 + + def bounce_pad(self): + self.xmove *= -1 + def refresh(self): + self.goto(0,0) + self.xmove *=-1 + self.ymove *=-1 + diff --git a/ping_pong/main.py b/ping_pong/main.py new file mode 100644 index 0000000..6da7883 --- /dev/null +++ b/ping_pong/main.py @@ -0,0 +1,54 @@ +from turtle import Turtle,Screen +from paddle import Paddle +from ball import Ball +from scoreboard import Scoreboard +import time +screen=Screen() +tim=Turtle() +k=.1 +is_ = True +scoreboard=Scoreboard() +screen.tracer(0) +screen.setup(900,600) +screen.bgcolor("black") +screen.title("PING PONG") +paddle_r=Paddle() +paddle_l=Paddle() +ball=Ball() +screen.listen() +paddle_r.create_bars((420,0)) +paddle_l.create_bars((-420,0)) +game_on=True +def resume(): + if ball.xcor() > 460: + scoreboard.total_l += 1 + if ball.xcor() < -460: + scoreboard.total_r += 1 + scoreboard.update() + tim.clear() + global k + k = .1 + ball.refresh() + + +while game_on: + scoreboard.update() + time.sleep(k) + screen.update() + screen.onkey(fun=paddle_r.up,key="Up") + screen.onkey(fun=paddle_l.up, key="w") + screen.onkey(fun=paddle_r.down,key="Down") + screen.onkey(fun=paddle_l.down, key="s") + ball.move() + if ball.ycor()>279 or ball.ycor()<-275: + ball.bounce() + if ball.xcor()>390 and ball.distance(paddle_r)<50 or ball.xcor()<-390 and ball.distance(paddle_l)<50: + ball.bounce_pad() + k/=1.2 + if ball.xcor()>460 or ball.xcor()<-460: + tim.color("white") + tim.hideturtle() + tim.write("Press Space to Resume",align="center",font=("Algerian",30,"normal")) + screen.onkey(fun=resume, key="space") + +screen.exitonclick() diff --git a/ping_pong/paddle.py b/ping_pong/paddle.py new file mode 100644 index 0000000..1529c30 --- /dev/null +++ b/ping_pong/paddle.py @@ -0,0 +1,23 @@ +from turtle import Turtle,Screen + +class Paddle(Turtle): + + def __init__(self): + super().__init__() + + def create_bars(self,position): + self.penup() + self.color("white") + self.shape("square") + self.shapesize(stretch_len=1, stretch_wid=6) + self.goto(position) + + def down(self): + y_cor=self.ycor()-30 + self.goto(self.xcor(),y_cor) + + def up(self): + y_cor = self.ycor() + 30 + self.goto(self.xcor(), y_cor) + + diff --git a/ping_pong/scoreboard.py b/ping_pong/scoreboard.py new file mode 100644 index 0000000..4b004f4 --- /dev/null +++ b/ping_pong/scoreboard.py @@ -0,0 +1,15 @@ +from turtle import Turtle + +class Scoreboard(Turtle): + def __init__(self): + super().__init__() + self.total_r=0 + self.total_l=0 + self.penup() + self.hideturtle() + self.color("white") + self.goto(0,250) + + def update(self): + self.clear() + self.write(f"{self.total_l} {self.total_r}", align="center", font=("Courier", 26, "normal")) diff --git a/player.py b/player.py new file mode 100644 index 0000000..fad6730 --- /dev/null +++ b/player.py @@ -0,0 +1,20 @@ +from turtle import Turtle +STARTING_POSITION = (0, -280) +MOVE_DISTANCE = 10 +FINISH_LINE_Y = 280 + +class Player(Turtle): + def __init__(self): + super().__init__() + self.color("black") + self.penup() + self.goto(STARTING_POSITION) + self.shape("turtle") + self.setheading(90) + + def move_up(self): + self.forward(MOVE_DISTANCE) + def move_down(self): + self.backward(MOVE_DISTANCE) + def startOver(self): + self.goto(STARTING_POSITION) diff --git a/scoreboard.py b/scoreboard.py new file mode 100644 index 0000000..56c729b --- /dev/null +++ b/scoreboard.py @@ -0,0 +1,22 @@ +FONT = ("Courier", 20, "bold") +from turtle import Turtle + +class Scoreboard(Turtle): + def __init__(self): + super().__init__() + + self.level=1 + self.color("black") + self.hideturtle() + self.penup() + self.goto(-280,260) + self.update() + def update(self): + self.write(f"LEVEL={self.level}", align="left", font=FONT) + def level_inc(self): + self.level+=1 + self.clear() + self.update() + def gameover(self): + self.goto(0,0) + self.write(f"Game Over", align="center", font=FONT) \ No newline at end of file diff --git a/turtle crossing/main.py b/turtle crossing/main.py new file mode 100644 index 0000000..53ff115 --- /dev/null +++ b/turtle crossing/main.py @@ -0,0 +1,31 @@ +import time +from turtle import Screen,Turtle +from player import Player +from car_manager import CarManager +from scoreboard import Scoreboard + +screen = Screen() +screen.setup(width=600, height=600) +screen.tracer(0) +player=Player() +game_is_on = True +screen.listen() +car_manager = CarManager() +scoreboard=Scoreboard() +while game_is_on: + car_manager.create_car() + time.sleep(0.1) + car_manager.move_all() + for _ in car_manager.all_cars: + if _.distance(player)<20: + scoreboard.gameover() + game_is_on=False + screen.onkey(key="Up",fun=player.move_up) + screen.onkey(key="Down",fun=player.move_down) + + if player.ycor()>=280: + player.startOver() + car_manager.increase_speed() + scoreboard.level_inc() + screen.update() +screen.exitonclick()