-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
200 lines (178 loc) · 6.55 KB
/
game.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
"""
This simple example is used for the line-by-line tutorial
that comes with pygame. It is based on a 'popular' web banner.
Note there are comments here, but for the full explanation,
follow along in the tutorial.
"""
import asset
import formicarium
import math
import pygame
import random
FPS = 60
NUM_ANTS = 30
NUM_FOOD = 10
SCREEN_X = 1024
SCREEN_Y = 768
def getDistance(a, b):
dist = math.hypot(a[0]-b[0], a[1]-b[1])
if dist < 0:
dist *= -1
return dist
def moveToTarget(a, b, vel):
ax = a[0]
ay = a[1]
if a[0] < b[0]:
ax += vel
if a[0] > b[0]:
ax -= vel
if a[1] < b[1]:
ay += vel
if a[1] > b[1]:
ay -= vel
return [ax, ay]
def moveToRandom(a, random_bias, vel):
ax = a[0]
ay = a[1]
if random.randrange(0, 100) > random_bias:
ax += vel
else:
ax -= vel
if random.randrange(0, 100) > random_bias:
ay += vel
else:
ay -= vel
return [ax, ay]
def main():
"""this function is called when the program starts.
it initializes everything it needs, then runs in
a loop until the function returns."""
#Initialize Everything
pygame.init()
screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y))
pygame.display.set_caption('LD23 a tiny world')
pygame.mouse.set_visible(0)
pygame.key.set_repeat(1, 10)
#Create The Backgound
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
formic = formicarium.Formicarium(background)
#Put Text On The Background, Centered
if pygame.font:
font = pygame.font.Font(None, 36)
text = font.render("press space to shuffle food", 1, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width()/2)
formic.draw()
background.blit(text, textpos)
#Display The Background
screen.blit(background, (0, 0))
pygame.display.flip()
#Prepare Game Objects
clock = pygame.time.Clock()
ants = []
for i in xrange(NUM_ANTS):
ants.append(asset.Entity())
ants[i].set_anim('ant.png', num=3, frametime=100.0, colorkey=pygame.Color(255, 255, 255))
ants[i].p = [SCREEN_X/2., SCREEN_Y/2.]
ants[i].home = 0
ants[i].target = [-1, -1]
ants[i].life = 200
asset.get_image('apple.png', -1)
food = []
for i in xrange(NUM_FOOD):
food.append(asset.Entity())
food[i].set_image('apple.png')
food[i].p = random.randrange(SCREEN_X), random.randrange(SCREEN_Y)
food[i].life = random.randrange(15)
asset.get_image('ant_hill.png', -1)
home = asset.Entity()
home.set_image('ant_hill.png')
home.p = SCREEN_X/2., SCREEN_Y/2.
home.target = [-1, -1]
#Main Loop
currentTime = pygame.time.get_ticks()
newTime = 0.0
frameTime = 0.0
accumulator = 0.0
dt = 1000.0/60.0 #60 fps
dtTime = 0.0 #total time for this frame, for animations
going = True
while going:
#clock.tick(1.0/100.0)
#Handle Input Events
newTime = pygame.time.get_ticks()
frameTime = newTime - currentTime
currentTime = newTime
accumulator += frameTime
dtTime = 0.0
#loop for smooth delta input, use dtTime for animations after the while loop
while accumulator > dt:
accumulator -= dt
dtTime += dt
for event in pygame.event.get():
if event.type == pygame.QUIT:
going = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
going = False
elif event.key == pygame.K_SPACE:
for i in xrange(NUM_FOOD):
food[i].p = random.randrange(SCREEN_X), random.randrange(SCREEN_Y)
#Draw Everything
screen.blit(background, (0, 0))
#uncommenting these two lines will let you move an infinite foodsource around and control the ants
#food[0].p = pygame.mouse.get_pos()
#food[0].life = 1000
home.draw(screen, dtTime)
for i in xrange(NUM_FOOD):
food[i].draw(screen, dtTime)
#swarm test
for i in xrange(NUM_ANTS):
if ants[i].home == 0:
for j in xrange(NUM_FOOD):
if getDistance(ants[i].p, food[j].p) < 130 and ants[i].target[0] == -1:
ants[i].p = moveToTarget(ants[i].p, food[j].p, 0.01 * dtTime)
if getDistance(ants[i].p, food[j].p) < 20:
if food[j].life < 0:
food[j].p = random.randrange(SCREEN_X), random.randrange(SCREEN_Y)
food[j].life = random.randrange(100)
ants[i].target = [-1, -1]
ants[i].home = 1
else:
food[j].life -= 1
ants[i].home = 1
ants[i].target = food[j].p
if ants[i].target[0] != -1:
ants[i].p = moveToTarget(ants[i].p, ants[i].target, 0.1 * dtTime)
if getDistance(ants[i].p, ants[i].target) < 10 and ants[i].home == 0:
#ants[i].home = 1
ants[i].target = [-1, -1]
else:
#ants[i].p = moveToRandom(ants[i].p, 50, 0.1 * dtTime)
distx = random.randrange(10, SCREEN_X/2.)
disty = random.randrange(10, SCREEN_Y/2.)
ants[i].target = ants[i].p[0] + random.randrange(-distx, distx), ants[i].p[1] + random.randrange(-disty, disty)
if ants[i].home == 1:
ants[i].p = moveToTarget(ants[i].p, home.p, 0.1 * dtTime)
if getDistance(ants[i].p, home.p) < 10:
ants[i].home = 0
ants[i].life = 200
ant_target = ants[i].target
if home.target[0] != -1:
ants[i].target = home.target
if ants[i].target[0] != -1:
home.target = ant_target
if ants[i].home != 1:
ants[i].life -= 1
if ants[i].life < 0 and ants[i].target[0] == -1:
ants[i].home = 1
if getDistance(ants[i].p, home.p) > 25:
ants[i].draw(screen, dtTime)
pygame.display.flip()
pygame.quit()
#Game Over
#this calls the 'main' function when this script is executed
if __name__ == '__main__':
main()