diff --git a/pingpong/__pycache__/ball.cpython-310.pyc b/pingpong/__pycache__/ball.cpython-310.pyc new file mode 100644 index 0000000..acbf49c Binary files /dev/null and b/pingpong/__pycache__/ball.cpython-310.pyc differ diff --git a/pingpong/__pycache__/play.cpython-310.pyc b/pingpong/__pycache__/play.cpython-310.pyc new file mode 100644 index 0000000..e0c2ff1 Binary files /dev/null and b/pingpong/__pycache__/play.cpython-310.pyc differ diff --git a/pingpong/__pycache__/scoreboard.cpython-310.pyc b/pingpong/__pycache__/scoreboard.cpython-310.pyc new file mode 100644 index 0000000..feca1f0 Binary files /dev/null and b/pingpong/__pycache__/scoreboard.cpython-310.pyc differ diff --git a/pingpong/ball.py b/pingpong/ball.py new file mode 100644 index 0000000..75b244e --- /dev/null +++ b/pingpong/ball.py @@ -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 diff --git a/pingpong/main.py b/pingpong/main.py new file mode 100644 index 0000000..ed03829 --- /dev/null +++ b/pingpong/main.py @@ -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() diff --git a/pingpong/play.py b/pingpong/play.py new file mode 100644 index 0000000..3378bec --- /dev/null +++ b/pingpong/play.py @@ -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) + + diff --git a/pingpong/scoreboard.py b/pingpong/scoreboard.py new file mode 100644 index 0000000..92e8e4b --- /dev/null +++ b/pingpong/scoreboard.py @@ -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() + diff --git a/snakegame/snake_game/README.md b/snakegame/snake_game/README.md new file mode 100644 index 0000000..1a9d552 --- /dev/null +++ b/snakegame/snake_game/README.md @@ -0,0 +1,3 @@ +# snake_game +classic snake game "eat more to grow more" +a basic snake game diff --git a/snakegame/snake_game/__pycache__/food.cpython-310.pyc b/snakegame/snake_game/__pycache__/food.cpython-310.pyc new file mode 100644 index 0000000..d3bce01 Binary files /dev/null and b/snakegame/snake_game/__pycache__/food.cpython-310.pyc differ diff --git a/snakegame/snake_game/__pycache__/scorecard.cpython-310.pyc b/snakegame/snake_game/__pycache__/scorecard.cpython-310.pyc new file mode 100644 index 0000000..ab4ff8a Binary files /dev/null and b/snakegame/snake_game/__pycache__/scorecard.cpython-310.pyc differ diff --git a/snakegame/snake_game/__pycache__/snake.cpython-310.pyc b/snakegame/snake_game/__pycache__/snake.cpython-310.pyc new file mode 100644 index 0000000..ac256b8 Binary files /dev/null and b/snakegame/snake_game/__pycache__/snake.cpython-310.pyc differ diff --git a/snakegame/snake_game/check.py b/snakegame/snake_game/check.py new file mode 100644 index 0000000..b47fbc0 --- /dev/null +++ b/snakegame/snake_game/check.py @@ -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() diff --git a/snakegame/snake_game/data b/snakegame/snake_game/data new file mode 100644 index 0000000..7813681 --- /dev/null +++ b/snakegame/snake_game/data @@ -0,0 +1 @@ +5 \ No newline at end of file diff --git a/snakegame/snake_game/food.py b/snakegame/snake_game/food.py new file mode 100644 index 0000000..a035abd --- /dev/null +++ b/snakegame/snake_game/food.py @@ -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) diff --git a/snakegame/snake_game/highscore.txt b/snakegame/snake_game/highscore.txt new file mode 100644 index 0000000..e69de29 diff --git a/snakegame/snake_game/main.py b/snakegame/snake_game/main.py new file mode 100644 index 0000000..9a7abe8 --- /dev/null +++ b/snakegame/snake_game/main.py @@ -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() \ No newline at end of file diff --git a/snakegame/snake_game/scorecard.py b/snakegame/snake_game/scorecard.py new file mode 100644 index 0000000..96ba8f2 --- /dev/null +++ b/snakegame/snake_game/scorecard.py @@ -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() \ No newline at end of file diff --git a/snakegame/snake_game/snake.py b/snakegame/snake_game/snake.py new file mode 100644 index 0000000..fd2213d --- /dev/null +++ b/snakegame/snake_game/snake.py @@ -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) + +