-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfalling_skies.py
168 lines (124 loc) · 3.97 KB
/
falling_skies.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Falling Skies by Mahesh Sawant
import turtle
import os
import random
import time
# Set up the screen
wn = turtle.Screen()
wn.title("Falling Skies by Mahesh Sawant")
wn.bgcolor("black")
wn.bgpic("background.gif")
wn.setup(width=800, height=600)
wn.tracer(0)
wn.register_shape("deer_left.gif")
wn.register_shape("deer_right.gif")
wn.register_shape("hunter.gif")
wn.register_shape("nut.gif")
# Score
score = 0
# Health
lives = 3
# Player
player = turtle.Turtle()
player.speed(0)
player.shape("deer_left.gif")
player.color("white")
player.penup()
player.goto(0, -250)
player.direction = "stop"
# Good things
good_things = []
for _ in range(20):
good_thing = turtle.Turtle()
good_thing.speed(0)
good_thing.shape("nut.gif")
good_thing.color("green")
good_thing.penup()
good_thing.goto(-100, 250)
good_thing.speed = random.randint(1, 2)
good_things.append(good_thing)
# Bad things
bad_things = []
for _ in range(20):
bad_thing = turtle.Turtle()
bad_thing.speed(0)
bad_thing.shape("hunter.gif")
bad_thing.color("red")
bad_thing.penup()
bad_thing.goto(100, 250)
bad_thing.speed = random.randint(1, 2)
bad_things.append(bad_thing)
# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 Lives: 3", align="center", font=("Courier", 24, "normal"))
# Functions
def go_left():
player.direction = "left"
player.shape("deer_left.gif")
def go_right():
player.direction = "right"
player.shape("deer_right.gif")
# Keyboard bindings
wn.listen()
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
# Main game loop
while True:
wn.update()
# Move the player
if player.direction == "left":
player.setx(player.xcor() - 1)
if player.direction == "right":
player.setx(player.xcor() + 1)
# Check for border collisions
if player.xcor() < -390:
player.setx(-390)
elif player.xcor() > 390:
player.setx(390)
for good_thing in good_things:
# Move the good things
good_thing.sety(good_thing.ycor() - good_thing.speed)
# Check if good things are off the screen
if good_thing.ycor() < -300:
good_thing.goto(random.randint(-300, 300), random.randint(400, 800))
# Check for collisions
if player.distance(good_thing) < 40:
# Score increases
score += 10
# Show the score
pen.clear()
pen.write("Score: {} Lives: {}".format(score, lives), align="center", font=("Courier", 24, "normal"))
# Move the good thing back to the top
good_thing.goto(random.randint(-300, 300), random.randint(400, 800))
for bad_thing in bad_things:
# Move the bad things
bad_thing.sety(bad_thing.ycor() - bad_thing.speed)
if bad_thing.ycor() < -300:
bad_thing.goto(random.randint(-300, 300), random.randint(400, 800))
if player.distance(bad_thing) < 40:
# Score increases
score -= 10
lives -= 1
# Show the score
pen.clear()
pen.write("Score: {} Lives: {}".format(score, lives), align="center", font=("Courier", 24, "normal"))
time.sleep(1)
# Move the bad things back to the top
for bad_thing in bad_things:
bad_thing.goto(random.randint(-300, 300), random.randint(400, 800))
# Check for game over
if lives == 0:
pen.clear()
pen.write("Game Over! Score: {}".format(score), align="center", font=("Courier", 24, "normal"))
wn.update()
time.sleep(5)
score = 0
lives = 3
pen.clear()
pen.write("Score: {} Lives: {}".format(score, lives), align="center", font=("Courier", 24, "normal"))