File simulatie/Button.py added (mode: 100644) (index 0000000..afbccc9) |
|
1 |
|
# -*- coding: cp1252 -*- |
|
2 |
|
#/usr/bin/env python |
|
3 |
|
|
|
4 |
|
import pygame |
|
5 |
|
|
|
6 |
|
from pygame.locals import * |
|
7 |
|
|
|
8 |
|
pygame.init() |
|
9 |
|
|
|
10 |
|
class Button: |
|
11 |
|
def create_button(self, surface, color, x, y, text): |
|
12 |
|
surface = self.draw_button(surface, color, x, y) |
|
13 |
|
surface = self.write_text(surface, text, x, y) |
|
14 |
|
self.rect = pygame.Rect(x,y, 50, 200) |
|
15 |
|
self.isGreen = False |
|
16 |
|
return surface |
|
17 |
|
|
|
18 |
|
def write_text(self, surface, text, x, y): |
|
19 |
|
font_size = int(50//len(text)) |
|
20 |
|
myFont = pygame.font.SysFont("Calibri", font_size) |
|
21 |
|
myText = myFont.render(text, 1, (255,255,255)) |
|
22 |
|
surface.blit(myText, ((x+50/2) - myText.get_width()/2, (y+200/2) - myText.get_height()/2)) |
|
23 |
|
return surface |
|
24 |
|
|
|
25 |
|
def draw_button(self, surface, x, y): |
|
26 |
|
for i in range(1,10): |
|
27 |
|
s = pygame.Surface((50+(i*2),200+(i*2))) |
|
28 |
|
s.fill(color) |
|
29 |
|
alpha = (255/(i+2)) |
|
30 |
|
if alpha <= 0: |
|
31 |
|
alpha = 1 |
|
32 |
|
s.set_alpha(alpha) |
|
33 |
|
pygame.draw.rect(s, color, (x-i,y-i,50+i,200+i), 0) |
|
34 |
|
surface.blit(s, (x-i,y-i)) |
|
35 |
|
pygame.draw.rect(surface, (50,150,50), (x,y,50,200), 0) |
|
36 |
|
pygame.draw.rect(surface, (190,190,190), (x,y,50,200), 1) |
|
37 |
|
return surface |
|
38 |
|
|
|
39 |
|
def pressed(self, mouse): |
|
40 |
|
if mouse[0] > self.rect.topleft[0] and mouse[1] > self.rect.topleft[1] and mouse[0] < self.rect.bottomright[0] and mouse[1] < self.rect.bottomright[1]: |
|
41 |
|
print "Some button was pressed!" |
|
42 |
|
return True |
|
43 |
|
else: |
|
44 |
|
return False |
|
45 |
|
|
|
46 |
|
def toggleLight(self): |
|
47 |
|
self.isGreen = !self.isGreen |
|
48 |
|
if isGreen: |
|
49 |
|
pygame.draw.rect(surface, (50,150,50), (x,y,50,200), 0) |
File simulatie/Buttons.py added (mode: 100644) (index 0000000..55c09be) |
|
1 |
|
# -*- coding: cp1252 -*-
|
|
2 |
|
#/usr/bin/env python
|
|
3 |
|
#Simon H. Larsen
|
|
4 |
|
#Buttons
|
|
5 |
|
#Project startet: d. 26. august 2012
|
|
6 |
|
import pygame
|
|
7 |
|
from pygame.locals import *
|
|
8 |
|
pygame.init()
|
|
9 |
|
class Button:
|
|
10 |
|
def create_button(self, surface, color, x, y, length, height, width, text, text_color):
|
|
11 |
|
surface = self.draw_button(surface, color, length, height, x, y, width)
|
|
12 |
|
surface = self.write_text(surface, text, text_color, length, height, x, y)
|
|
13 |
|
self.rect = pygame.Rect(x,y, length, height)
|
|
14 |
|
return surface
|
|
15 |
|
|
|
16 |
|
def write_text(self, surface, text, text_color, length, height, x, y):
|
|
17 |
|
font_size = int(length//len(text))
|
|
18 |
|
myFont = pygame.font.SysFont("Calibri", font_size)
|
|
19 |
|
myText = myFont.render(text, 1, text_color)
|
|
20 |
|
surface.blit(myText, ((x+length/2) - myText.get_width()/2, (y+height/2) - myText.get_height()/2))
|
|
21 |
|
return surface
|
|
22 |
|
|
|
23 |
|
def draw_button(self, surface, color, length, height, x, y, width):
|
|
24 |
|
for i in range(1,10):
|
|
25 |
|
s = pygame.Surface((length+(i*2),height+(i*2)))
|
|
26 |
|
s.fill(color)
|
|
27 |
|
alpha = (255/(i+2))
|
|
28 |
|
if alpha <= 0:
|
|
29 |
|
alpha = 1
|
|
30 |
|
s.set_alpha(alpha)
|
|
31 |
|
pygame.draw.rect(s, color, (x-i,y-i,length+i,height+i), width)
|
|
32 |
|
surface.blit(s, (x-i,y-i))
|
|
33 |
|
pygame.draw.rect(surface, color, (x,y,length,height), 0)
|
|
34 |
|
pygame.draw.rect(surface, (190,190,190), (x,y,length,height), 1)
|
|
35 |
|
return surface
|
|
36 |
|
|
|
37 |
|
def pressed(self, mouse):
|
|
38 |
|
if mouse[0] > self.rect.topleft[0]:
|
|
39 |
|
if mouse[1] > self.rect.topleft[1]:
|
|
40 |
|
if mouse[0] < self.rect.bottomright[0]:
|
|
41 |
|
if mouse[1] < self.rect.bottomright[1]:
|
|
42 |
|
print "Some button was pressed!"
|
|
43 |
|
return True
|
|
44 |
|
else: return False
|
|
45 |
|
else: return False
|
|
46 |
|
else: return False
|
|
47 |
|
else: return False
|
File simulatie/Example.py added (mode: 100644) (index 0000000..f2a4c76) |
|
1 |
|
# -*- coding: cp1252 -*-
|
|
2 |
|
#/usr/bin/env python
|
|
3 |
|
#Simon H. Larsen
|
|
4 |
|
#Buttons.py - example
|
|
5 |
|
#Project startet: d. 28. august 2012
|
|
6 |
|
#Import pygame modules and Buttons.py(it must be in same dir)
|
|
7 |
|
import pygame, Buttons
|
|
8 |
|
from pygame.locals import *
|
|
9 |
|
|
|
10 |
|
#Initialize pygame
|
|
11 |
|
pygame.init()
|
|
12 |
|
|
|
13 |
|
class Button_Example:
|
|
14 |
|
def __init__(self):
|
|
15 |
|
self.main()
|
|
16 |
|
|
|
17 |
|
#Create a display
|
|
18 |
|
def display(self):
|
|
19 |
|
self.screen = pygame.display.set_mode((650,370),0,32)
|
|
20 |
|
pygame.display.set_caption("Buttons.py - example")
|
|
21 |
|
|
|
22 |
|
#Update the display and show the button
|
|
23 |
|
def update_display(self):
|
|
24 |
|
self.screen.fill((30,144,255))
|
|
25 |
|
#Parameters: surface, color, x, y, length, height, width, text, text_color
|
|
26 |
|
self.Button1.create_button(self.screen, (107,142,35), 225, 135, 200, 100, 0, "Example", (255,255,255))
|
|
27 |
|
pygame.display.flip()
|
|
28 |
|
|
|
29 |
|
|
|
30 |
|
#Run the loop
|
|
31 |
|
def main(self):
|
|
32 |
|
self.Button1 = Buttons.Button()
|
|
33 |
|
self.display()
|
|
34 |
|
while True:
|
|
35 |
|
self.update_display()
|
|
36 |
|
for event in pygame.event.get():
|
|
37 |
|
if event.type == pygame.QUIT:
|
|
38 |
|
pygame.quit()
|
|
39 |
|
elif event.type == MOUSEBUTTONDOWN:
|
|
40 |
|
if self.Button1.pressed(pygame.mouse.get_pos()):
|
|
41 |
|
print "Give me a command!"
|
|
42 |
|
|
|
43 |
|
if __name__ == '__main__':
|
|
44 |
|
obj = Button_Example()
|
File simulatie/pyg.py changed (mode: 100644) (index 749b178..08f55a2) |
... |
... |
import trafficModel as tm |
9 |
9 |
import connection |
import connection |
10 |
10 |
|
|
11 |
11 |
vehicles = list() |
vehicles = list() |
|
12 |
|
lights = list() |
12 |
13 |
|
|
13 |
14 |
pygame.init() |
pygame.init() |
14 |
15 |
|
|
|
... |
... |
screen = pygame.display.set_mode((800,800)) |
16 |
17 |
screen.fill((255,255,255)) |
screen.fill((255,255,255)) |
17 |
18 |
bg = pygame.transform.scale(pygame.image.load("./PlattegrondV5.png"), (800,800)) |
bg = pygame.transform.scale(pygame.image.load("./PlattegrondV5.png"), (800,800)) |
18 |
19 |
|
|
|
20 |
|
class Light(object): |
|
21 |
|
def __init__(self, lightIndex): |
|
22 |
|
self.lamp = pygame.image.load("light_color.png") |
|
23 |
|
#self.lamp_tint = self.lamp() |
|
24 |
|
self.rect = self.lamp.get_rect() |
|
25 |
|
|
|
26 |
|
print("Light " + str(lightIndex) + " -> " + str(tm.lightsPos[lightIndex])) |
|
27 |
|
|
|
28 |
|
self.rect.x = tm.lightsPos[lightIndex]["position"][0] |
|
29 |
|
self.rect.y = tm.lightsPos[lightIndex]["position"][1] |
|
30 |
|
self.lightIndex = lightIndex |
|
31 |
|
|
|
32 |
|
def lightCheck(self): |
|
33 |
|
self.lamp_tint = self.lamp.copy() |
|
34 |
|
#if tm.lights[self.lightIndex] == 0: # ROOD |
|
35 |
|
# self.lamp_tint.fill((250, 150, 150), None, pygame.BLEND_RGB_MULT) |
|
36 |
|
#elif tm.lights[self.lightIndex] == 1: # ORANJE |
|
37 |
|
self.lamp_tint.fill((190, 60, 60), None, pygame.BLEND_RGB_MULT) |
|
38 |
|
#elif tm.lights[self.lightIndex] == 2: # GROEN |
|
39 |
|
|
|
40 |
|
screen.blit(self.lamp_tint, self.rect) |
|
41 |
|
|
|
42 |
|
|
19 |
43 |
class Vehicle(object): |
class Vehicle(object): |
20 |
44 |
def __init__(self, x, y, trafficId): |
def __init__(self, x, y, trafficId): |
21 |
45 |
trafficType = tm.lights[trafficId]["Id"] |
trafficType = tm.lights[trafficId]["Id"] |
22 |
46 |
print("Traffic type: " + str(trafficType)) |
print("Traffic type: " + str(trafficType)) |
23 |
|
print(trafficType) |
|
|
47 |
|
|
24 |
48 |
if trafficType >= 100 and trafficType < 200: # Is een auto |
if trafficType >= 100 and trafficType < 200: # Is een auto |
25 |
49 |
self.auto = pygame.transform.scale(pygame.image.load("Auto.png"), (64,64)) |
self.auto = pygame.transform.scale(pygame.image.load("Auto.png"), (64,64)) |
26 |
50 |
elif trafficType < 300: # Is een bus |
elif trafficType < 300: # Is een bus |
|
... |
... |
class Vehicle(object): |
33 |
57 |
self.auto = pygame.transform.scale(pygame.image.load("Trein.png"), (64,64)) |
self.auto = pygame.transform.scale(pygame.image.load("Trein.png"), (64,64)) |
34 |
58 |
else: # zijn slagbomen |
else: # zijn slagbomen |
35 |
59 |
pass |
pass |
|
60 |
|
|
36 |
61 |
self.rect = self.auto.get_rect() |
self.rect = self.auto.get_rect() |
37 |
62 |
self.dest = [x,y] |
self.dest = [x,y] |
38 |
63 |
self.speed = [0,0] |
self.speed = [0,0] |
|
... |
... |
class Vehicle(object): |
40 |
65 |
|
|
41 |
66 |
self.wp = 1 |
self.wp = 1 |
42 |
67 |
self.waiting = True |
self.waiting = True |
|
68 |
|
self.transform = 0 |
43 |
69 |
|
|
44 |
70 |
self.rect.x = tm.queues[trafficId][0][0] |
self.rect.x = tm.queues[trafficId][0][0] |
45 |
71 |
self.rect.y = tm.queues[trafficId][0][1] |
self.rect.y = tm.queues[trafficId][0][1] |
46 |
72 |
|
|
|
73 |
|
# Bij sommige ID's moet de texture gespiegeld worden. |
47 |
74 |
if trafficType == 103 or trafficType == 106 or trafficType == 108 or trafficType == 109 or trafficType == 110 or trafficType == 303 or trafficType == 402: |
if trafficType == 103 or trafficType == 106 or trafficType == 108 or trafficType == 109 or trafficType == 110 or trafficType == 303 or trafficType == 402: |
48 |
75 |
self.auto = pygame.transform.flip(self.auto, True, False) |
self.auto = pygame.transform.flip(self.auto, True, False) |
49 |
76 |
|
|
|
77 |
|
self.transform = self.checkForTransform() |
|
78 |
|
print(self.transform) |
|
79 |
|
|
|
80 |
|
def checkForTransform(self): |
|
81 |
|
trafficType = tm.lights[self.trafficId]["Id"] |
|
82 |
|
for i in range(0, len(tm.transforms)): |
|
83 |
|
if tm.transforms[i]["from"] == trafficType: |
|
84 |
|
return tm.transforms[i]["to"] |
|
85 |
|
print("transform found!") |
|
86 |
|
return 0 |
|
87 |
|
|
50 |
88 |
def update(self): |
def update(self): |
51 |
89 |
screen.blit(self.auto, self.rect) |
screen.blit(self.auto, self.rect) |
52 |
90 |
self.rect = self.rect.move(self.speed) |
self.rect = self.rect.move(self.speed) |
|
... |
... |
class Vehicle(object): |
75 |
113 |
self.wp += 1 |
self.wp += 1 |
76 |
114 |
print("W: " + str(self.wp) + " / " + str(len(tm.queues[self.trafficId]))) |
print("W: " + str(self.wp) + " / " + str(len(tm.queues[self.trafficId]))) |
77 |
115 |
|
|
78 |
|
# elif self.waiting: # Debug |
|
79 |
|
# time.sleep(1) |
|
80 |
|
# tm.lights[self.trafficId]["Status"] = 1 # Debug |
|
|
116 |
|
#elif self.waiting: # Debug |
|
117 |
|
# time.sleep(1) |
|
118 |
|
# tm.lights[self.trafficId]["Status"] = 1 # Debug |
81 |
119 |
|
|
82 |
120 |
elif not self.waiting and self.wp < len(tm.waypoints[self.trafficId]): |
elif not self.waiting and self.wp < len(tm.waypoints[self.trafficId]): |
83 |
121 |
self.goto(tm.waypoints[self.trafficId][self.wp][0], tm.waypoints[self.trafficId][self.wp][1]) |
self.goto(tm.waypoints[self.trafficId][self.wp][0], tm.waypoints[self.trafficId][self.wp][1]) |
84 |
122 |
self.wp += 1 |
self.wp += 1 |
85 |
123 |
print("NW: " + str(self.wp) + " / " + str(len(tm.waypoints[self.trafficId]))) |
print("NW: " + str(self.wp) + " / " + str(len(tm.waypoints[self.trafficId]))) |
86 |
124 |
|
|
|
125 |
|
elif not self.waiting: # Check for transform |
|
126 |
|
if self.transform != 0: |
|
127 |
|
self.trafficId = self.transform |
|
128 |
|
self.wp = 1 |
|
129 |
|
self.waiting = True |
|
130 |
|
|
|
131 |
|
self.transform = self.checkForTransform() |
|
132 |
|
connection.update(self.trafficId, 1, None) |
|
133 |
|
else: |
|
134 |
|
pass # Desctructor here? |
|
135 |
|
|
87 |
136 |
|
|
88 |
137 |
|
|
89 |
138 |
def trafficCheck(self): |
def trafficCheck(self): |
|
... |
... |
def update(jsonObj): |
102 |
151 |
|
|
103 |
152 |
def spawnTrein(naarGroningen): |
def spawnTrein(naarGroningen): |
104 |
153 |
if naarGroningen: |
if naarGroningen: |
105 |
|
train = Vehicle(0,0,28) |
|
|
154 |
|
train = Vehicle(tm.queues[28][0][0], tm.queues[28][0][1], 28) |
106 |
155 |
|
|
107 |
156 |
else: |
else: |
108 |
|
train = Vehicle(0,0,29) |
|
|
157 |
|
train = Vehicle(tm.queues[28][0][0], tm.queues[28][0][1],28)#9) |
109 |
158 |
|
|
110 |
159 |
def slagbomenDalen(): |
def slagbomenDalen(): |
111 |
160 |
print("Lichten knipperen & bellen luiden") |
print("Lichten knipperen & bellen luiden") |
|
... |
... |
def spawnVehicle(index): |
124 |
173 |
|
|
125 |
174 |
# Mainloop |
# Mainloop |
126 |
175 |
def gameloop(): |
def gameloop(): |
127 |
|
#id = 16 |
|
|
176 |
|
#id = 28 |
128 |
177 |
#car = Vehicle(tm.queues[id][0][0],tm.queues[id][0][1],id) |
#car = Vehicle(tm.queues[id][0][0],tm.queues[id][0][1],id) |
|
178 |
|
|
|
179 |
|
for i in range(0, len(tm.lights)): |
|
180 |
|
lights.append(Light(i)) |
|
181 |
|
|
129 |
182 |
while True: |
while True: |
130 |
183 |
clock.tick(60) |
clock.tick(60) |
131 |
184 |
for event in pygame.event.get(): |
for event in pygame.event.get(): |
|
... |
... |
def gameloop(): |
135 |
188 |
screen.blit(bg, bg.get_rect()) |
screen.blit(bg, bg.get_rect()) |
136 |
189 |
for vehicle in vehicles: |
for vehicle in vehicles: |
137 |
190 |
vehicle.trafficCheck() |
vehicle.trafficCheck() |
|
191 |
|
#for light in lights: |
|
192 |
|
# light.lightCheck() |
138 |
193 |
#car.trafficCheck() |
#car.trafficCheck() |
139 |
194 |
pygame.display.flip() |
pygame.display.flip() |
140 |
195 |
|
|