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
|
import pygame
import time
import os
import sys
from pygame import K_ESCAPE
from pygame.locals import *
from random import randint
from tree import Tree
from ranger import Ranger
from cutter import Cutter
pygame.init()
global window_h, window_w, trees
# Background
bg_color = [255, 255, 255]
screen = pygame.display.set_mode( (0,0) )
pygame.display.set_caption("Ranger")
window_h, window_w = screen.get_size ()
# Tree
plant_time = 10
trees = []
start_time = time.time()
fps = 60
frame_time = 1.0/fps
# Ranger
unit = 1
ranger_speed = 4
ranger = Ranger(window_h, window_w, ranger_speed)
# Cutter
cutter_time = 50
tree_cutting_time = 50
cutter_speed = 4
cutter = []
# Draw background
screen.fill(bg_color)
# Draw everything on top of that
number_trees = 80
while number_trees != 0:
trees.append(Tree((randint(0,window_h), randint(0,window_w))) )
number_trees -= 1
for arbol in trees:
arbol.sprout_time = 200
score = 0
while True:
# Time tracking
current_time = time.time()
if current_time - start_time <= frame_time:
continue
start_time = current_time
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
ranger.input(event)
keyboard = pygame.key.get_pressed()
if keyboard[K_ESCAPE]:
exit()
if ranger.plant == True:
ranger.plant = False
if ranger.new_plant == False:
new_plant_position = ranger.position
ranger.new_plant = True
if ranger.new_plant == True:
plant_time += 1
#trees.append(Tree( new_plant_position ) )
if plant_time > 50:
trees.append(Tree( new_plant_position ) )
plant_time = 0
ranger.new_plant = False
# Update ranger position based on events
ranger.update()
for treecutter in cutter:
treecutter.update()
# Draw background
screen.fill(bg_color)
ranger.draw(screen)
for treecutter in cutter:
treecutter.draw(screen)
# Ranger kills tree cutters
for cortador in cutter:
if ranger.collides_with(cortador):
cutter.remove(cortador)
# Draw trees or grow them
for arbol in trees:
if arbol.sprout_time < 200:
arbol.sprout_time += 1
arbol.draw(screen)
# Tree cutters kills trees
for arbol in trees:
for cortador in cutter:
if cortador.collides_with(arbol):
cortador.cut_tree = True
arbol.draw_alert(screen)
cortador.cut_time += 1
arbol.cutting_time -=1
if cortador.cut_tree == True and cortador.cut_time == 100:
trees.remove(arbol)
cortador.cut_time = 0
cortador.cut_tree = False
if cutter_time == 300:
cutter.append( Cutter(window_h, window_w, cutter_speed, 0) )
cutter_time = 0
cutter_time += 1
## SCORE IS DRAWN AFTER EVERYTHING ELSE
# Score is equal to amount of trees
for arbol in trees:
score += 1
# Score text
score_text = "Air: " + str(score)
font = pygame.font.Font(None, 36)
text = font.render( score_text , 1, (0, 0, 0, 0))
screen.blit(text, (0, 0))
# Score updates after being shown
score = 0
pygame.display.update()
|