forked from sd16spring/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTexasMap.py
More file actions
97 lines (84 loc) · 3.19 KB
/
TexasMap.py
File metadata and controls
97 lines (84 loc) · 3.19 KB
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
import os
import pygame, sys
from pygame.locals import *
import us_map
import matplotlib.path
BLACK = (0, 0, 0)
BLUE = (0, 245, 255)
STATES = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'VI', 'WA', 'WV', 'WI', 'WY']
STATE = 'TX'
# set window size
width = 950
height = 650
# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.update(pygame.Rect(0,0,width,height))
class Slider(object):
"""Makes slider"""
def __init__(self):
self.sWidth = 8
self.sHeight = 40
self.x = width / 2
self.a = self.x
pygame.draw.rect(windowSurfaceObj,BLUE,Rect(self.x,600,self.sWidth,self.sHeight))
def slider_button(self):
"""move slider"""
button = pygame.mouse.get_pressed()
if button[0] != 0:
pos = pygame.mouse.get_pos()
x = pos[0]
y = pos[1]
a = x - self.sWidth
if a < 0:
a = 0
elif a > width - self.sWidth:
a = width - self.sWidth
pygame.draw.rect(windowSurfaceObj,BLACK,Rect(0,650,width,-60))
pygame.draw.rect(windowSurfaceObj,BLUE,Rect(a,600,self.sWidth,self.sHeight))
for i in StateList:
i.ColorGradient(a)
i.Draw_Map()
pygame.display.update(pygame.Rect(0,0,width,height))
self.a = a
slider = Slider()
a = slider.a
class State(object):
"""Makes and draws a state"""
def __init__(self, state, color):
self.state = state
self.color = color
def point_in_polygon(self, pt, polygon):
"""Returns True iff `pt` is inside `polygon`.
polygon` is a list of tuples `(x, y)`."""
return matplotlib.path.Path(polygon).contains_point(pt)
def ColorGradient(self, a):
"""Determines shade of blue/black"""
percentBlue = ((float(a)-width)/(-width))
self.color = (0, 245*percentBlue, 255*percentBlue)
def Draw_Map(self):
"""draws state on screen, color determined by ColorGradient()"""
for polygon in us_map.states[self.state]: #originaly [STATE]
# `polygon` points are tuples `(float, float)`. PyGame requires `(int, int)`.
points = [(int(x), int(y)) for x, y in polygon]
# Draw the interior
pygame.draw.polygon(windowSurfaceObj, self.color, points)
# Draw the boundary
pygame.draw.polygon(windowSurfaceObj, BLACK, points, 1)
pygame.display.flip()
StateList = []
for i in STATES:
StateList.append(State(i, (0, 245/2., 255/2.)))
StateList[-1].Draw_Map()
s = 0
while s == 0:
slider.slider_button()
# check for ESC key pressed, or pygame window closed, to quit
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()