-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreet.py
179 lines (126 loc) · 5.2 KB
/
Street.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
#!/usr/bin/env python
import sys
from random import random,randint,choice
import pygame
from pygame.locals import *
from time import sleep
import Pnj
import Physics
import Wall
import numpy as np
GRID = np.array([
[1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
NUMBER_OF_PNJ = 1
grayColor = pygame.Color(128,128,128)
blackColor = pygame.Color(255,255,255)
balckGrayColor = pygame.Color(200,200,200)
whiteColor = pygame.Color(0,0,0)
greenColor = pygame.Color(0,255,0)
class Street:
def __init__(self, width=600, height=600):
"""Initialize pygame."""
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width, self.height))
pygame.display.set_caption('Street')
def draw_direction_line(self, pnj):
"""Given a fish sprite, draw a line of motion using xVel and yVel."""
startX = pnj.rect[0]
startY = pnj.rect[1]
endX = (pnj.rect[0] + 2*pnj.xVel)
endY = (pnj.rect[1] + 2*pnj.yVel)
pygame.draw.line(self.screen, blackColor, (startX, startY), (endX, endY), 3)
def load_sprites(self):
"""Load all of the fish sprites."""
self.pnj_group = pygame.sprite.Group()
'''
for i in range(1):
self.pnj_group.add(Pnj.Pnj(rect=pygame.Rect(15*30, 18*30, 10, 10), color=whiteColor, finish= (0,10)))
for i in range(2,20):
self.pnj_group.add(Pnj.Pnj(rect=pygame.Rect(14*30, i*30, 10, 10), color=whiteColor, finish= (0,10)))
for i in range(2,20):
self.pnj_group.add(Pnj.Pnj(rect=pygame.Rect(10*30, i*30, 10, 10), color=whiteColor, finish= (0,10)))
for i in range(2,20):
self.pnj_group.add(Pnj.Pnj(rect=pygame.Rect(18*30, i*30, 10, 10), color=whiteColor, finish= (0,10)))
for i in range(2,20):
self.pnj_group.add(Pnj.Pnj(rect=pygame.Rect(16*30, i*30, 10, 10), color=whiteColor, finish= (0,10)))
for i in range(2,20):
self.pnj_group.add(Pnj.Pnj(rect=pygame.Rect(15*30, i*30, 10, 10), color=whiteColor, finish= (0,10)))
for i in range(2,20):
self.pnj_group.add(Pnj.Pnj(rect=pygame.Rect(13*30, i*30, 10, 10), color=whiteColor, finish= (0,10)))
'''
L = [(0,8),(0,7)]
for i in range(150):
nb = choice(L)
self.pnj_group.add(Pnj.Pnj(rect=pygame.Rect(randint(70,590), randint(10,300), 10, 10), color=whiteColor, finish= nb))
L = [(0,11),(0,12)]
for i in range(150):
nb = choice(L)
self.pnj_group.add(Pnj.Pnj(rect=pygame.Rect(randint(70,590), randint(300,590), 10, 10), color=whiteColor, finish= nb))
def load_wall(self):
wall = []
for i in range(len(GRID)):
for j in range(len(GRID)):
if GRID[i][j] == 1:
wall.append((i,j))
self.wall_group = pygame.sprite.Group()
for i in wall:
self.wall_group.add(Wall.Wall(rect=pygame.Rect(i[0]*30, i[1]*30, 30, 30), color=balckGrayColor))
def main_loop(self):
"""The main loop for drawing into the street."""
fpsClock = pygame.time.Clock()
self.load_sprites()
self.load_wall()
#arrival = pygame.sprite.Sprite.__init__()
while True:
self.screen.fill(grayColor)
pygame.draw.rect(self.screen,greenColor,(0*30,11*30,2*30,2*30))
pygame.draw.rect(self.screen,greenColor,(0*30,7*30,2*30,2*30))
self.wall_group.draw(self.screen)
self.pnj_group.draw(self.screen)
# Update the pnj velocities
for pnj in self.pnj_group.sprites():
pnj.update_velocity(street=self)
# Move pnj
for pnj in self.pnj_group.sprites():
pnj.swim(street=self)
# Draw direction arrows
'''
for pnj in self.pnj_group.sprites():
self.draw_direction_line(pnj)
'''
# Check for all colisions among exit and pnj
#spriteHitList = pygame.sprite.groupcollide(self.end, self.pnj_group, False, True, collided=physics.fish_collision)
# Go through a list of all Event objects that happened since the last get()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# Draw new window to the screen.
pygame.display.update()
fpsClock.tick(30) # Wait long enough so fps <= 30.
def main():
street = Street()
street.main_loop()
if __name__ == "__main__":
main()