-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (55 loc) · 1.64 KB
/
main.py
File metadata and controls
67 lines (55 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from time import sleep
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard
# setting up the screen and initial game setup
screen = Screen()
screen.setup(width=600, height=600)
screen.title("Turtle Crossing Game created by YJ-928")
screen.bgcolor("black")
screen.tracer(0)
# creating player obj from Player class
player = Player()
# creating scoreboard obj from ScoreBoard class
scoreboard = Scoreboard()
# creating car_manager obj from CarManager class
car_manager = CarManager()
# to listen and move player_turtle when
# user presses the up key in keyboard
screen.listen()
screen.onkey(player.move, "Up")
# variable as delay for creating new cars
count = 0
def move_and_create_cars():
"""To randomly gen new cars using CarManager class
and move them from right to left continously"""
global count
# to continously move cars
car_manager.move_car()
# for creating random new cars after loop runs 6 times
if count == 6:
# to create new car
car_manager.create_car()
count = 0
count += 1
# loop condition
game_is_on = True
# loop to run the game
while game_is_on:
screen.update()
sleep(0.1)
move_and_create_cars()
# detect turtle collision with cars
for car in car_manager.all_cars:
if player.distance(car) < 27:
scoreboard.gameover()
game_is_on = False
# detect player win, if turtle reaches top side of screen
if player.ycor() > 260:
sleep(2)
player.reset()
scoreboard.level_up()
car_manager.car_speed_up()
# to avoid screen crashing
screen.exitonclick()