-
-
Notifications
You must be signed in to change notification settings - Fork 264
Added Road cross turtle game in pygame scripts folder #990
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
PyGamesScripts/Turtle Road Cross game/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
<table> | ||
<tr> | ||
<td> | ||
<img src="https://user-images.githubusercontent.com/73184612/128293281-5492671c-211a-4970-b8a9-b3c34b7e61c4.png"> </img> | ||
</td> | ||
<td> | ||
<img src="https://user-images.githubusercontent.com/73184612/128293303-330929fb-1e87-4836-aae9-2849122eeb58.png"> </img> | ||
</td> | ||
</tr> | ||
</table> | ||
|
Binary file added
PyGamesScripts/Turtle Road Cross game/bg.png
30 changes: 30 additions & 0 deletions
PyGamesScripts/Turtle Road Cross game/car_manager.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
41 changes: 41 additions & 0 deletions
PyGamesScripts/Turtle Road Cross game/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
23 changes: 23 additions & 0 deletions
PyGamesScripts/Turtle Road Cross game/player.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
26 changes: 26 additions & 0 deletions
PyGamesScripts/Turtle Road Cross game/scoreboard.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.