diff --git a/PyGamesScripts/Turtle Road Cross game/README.md b/PyGamesScripts/Turtle Road Cross game/README.md new file mode 100644 index 000000000..bb42ddce1 --- /dev/null +++ b/PyGamesScripts/Turtle Road Cross game/README.md @@ -0,0 +1,21 @@ +## The Cross Boss! +#### An interactive single-player game where the player must help the turtle reach the other side of the road, by successfully dodging the cars. + +- The turtle can only move forward. +- Cars appear on the screen at random intervals and move along the road from right to left. +- If the turtle reaches the other side successfully, the player is promoted to the next game level. +- The difficulty level increases progressively with the increase in the speed of cars on the road. +- The game is over when a car touches the turtle. +- Built using the turtle module in python. + + +
+ + + + + + +
+ + diff --git a/PyGamesScripts/Turtle Road Cross game/bg.png b/PyGamesScripts/Turtle Road Cross game/bg.png new file mode 100644 index 000000000..3c8a1d8c9 Binary files /dev/null and b/PyGamesScripts/Turtle Road Cross game/bg.png differ diff --git a/PyGamesScripts/Turtle Road Cross game/car_manager.py b/PyGamesScripts/Turtle Road Cross game/car_manager.py new file mode 100644 index 000000000..89aadfbbc --- /dev/null +++ b/PyGamesScripts/Turtle Road Cross game/car_manager.py @@ -0,0 +1,30 @@ +from turtle import Turtle +import random +COLORS = ["firebrick", "orange", "yellow", "white", "blue", "purple", "cornflower blue", "deep pink"] +STARTING_MOVE_DISTANCE = 5 +MOVE_INCREMENT = 5 + + +class CarManager(): + def __init__(self): + self.allcars=[] + self.carspeed = STARTING_MOVE_DISTANCE + + def create_car(self): + random_chance= random.randint(1,6) + if random_chance ==1: + newcar= Turtle() + newcar.shape("square") + newcar.shapesize(1,2) + newcar.penup() + newcar.color(random.choice(COLORS)) + random_y = random.randint(-215, 215) + newcar.goto(300, random_y) + self.allcars.append(newcar) + + def move_cars(self): + for car in self.allcars: + car.backward(self.carspeed) + + def level_up(self): + self.carspeed += MOVE_INCREMENT diff --git a/PyGamesScripts/Turtle Road Cross game/main.py b/PyGamesScripts/Turtle Road Cross game/main.py new file mode 100644 index 000000000..e64e7e39c --- /dev/null +++ b/PyGamesScripts/Turtle Road Cross game/main.py @@ -0,0 +1,41 @@ +import time +from turtle import Screen +from player import Player +from car_manager import CarManager +from scoreboard import Scoreboard + + +screen = Screen() +screen.setup(width=600, height=600) +screen.bgpic("bg.png") +screen.title("Cross the road!") +screen.tracer(0) +playert= Player() +car= CarManager() +scoreboard= Scoreboard() +screen.listen() +screen.onkeypress(playert.moveup,"Up") + + +game_is_on = True +while game_is_on: + time.sleep(0.1) + screen.update() + + car.create_car() + car.move_cars() + + #detect collision with car + for c in car.allcars: + if c.distance(playert) < 25: + game_is_on= False + scoreboard.game_over() + #detect successful reach to end line + if playert.ycor()>= 260: + playert.start_pos() + car.level_up() + scoreboard.increase_level() + + + +screen.exitonclick() \ No newline at end of file diff --git a/PyGamesScripts/Turtle Road Cross game/player.py b/PyGamesScripts/Turtle Road Cross game/player.py new file mode 100644 index 000000000..5e21f8a94 --- /dev/null +++ b/PyGamesScripts/Turtle Road Cross game/player.py @@ -0,0 +1,23 @@ +from turtle import Turtle + +STARTING_POSITION = (-60, -270) +MOVE_DISTANCE = 10 +FINISH_LINE_Y = 280 + + +class Player(Turtle): + def __init__(self): + super().__init__() + self.shape("turtle") + self.color("black") + self.penup() + self.start_pos() + + def start_pos(self): + self.goto(STARTING_POSITION) + self.setheading(90) + + def moveup(self): + newy= self.ycor()+ MOVE_DISTANCE + self.goto(self.xcor(), newy) + diff --git a/PyGamesScripts/Turtle Road Cross game/scoreboard.py b/PyGamesScripts/Turtle Road Cross game/scoreboard.py new file mode 100644 index 000000000..8ad3bc12a --- /dev/null +++ b/PyGamesScripts/Turtle Road Cross game/scoreboard.py @@ -0,0 +1,26 @@ +from turtle import Turtle +FONT = ("Courier", 24, "normal") + + +class Scoreboard(Turtle): + def __init__(self): + super().__init__() + self.hideturtle() + self.penup() + self.level =1 + self.goto(-290, 230) + self.update_scoreboard() + + + def increase_level(self) : + self.level+=1 + self.update_scoreboard() + + def update_scoreboard(self): + self.clear() + self.color("black") + self.write(f"Level: {self.level}", align="left", font=FONT) + + def game_over(self): + self.goto(0,0) + self.write("GAME OVER", align="center", font= FONT) \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /