Ik zit met het volgende probleem, ik ben in python een kleine game aan het schrijven dat gebruik maakt van polygonen. De polygonen die ik gebruik zitten in een array.
Hele Code
Python:
De bovenstaande code werkt perfect, hoe zet alle polygonen op het scherm. Maar nu wou ik laatst inbouwen dat als je op een polygoon klikt die van kleur veranderd, daarvoor heb ik een handige functie op internet gevonden. 1
2
3
4
5
6
7
8
9
10
| # Array hexes = [ ((0,20), (10,0), (35,0), (45,20), (35,40), (10, 40)), ((35,40), (45,20), (70,20), (80,40), (70,60), (45, 60)), ((70,60), (80,40), (105,40), (115,60), (105,80), (80, 80)), ((70,20), (80,0), (105,0), (115,20), (105,40), (80, 40)) ] colors = [ 255000000,000255000,000000255,255255255 ] # Draw map for v,k in zip(hexes, colors): pygame.draw.polygon(screen, k, v) |
Python:
Als ik deze functie een van de waarde geef dat in de array zitten loopt het fout. Ik denk zelf dat het komt doordat er een [ voor en ] achter staat. Nu vroeg ik me af is het mogelijk om die op een manier weg te krijgen zonder ze te moeten strippen.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
| def pointInPoly(point, pointsList): "Return True if point is contained in polygon (defined by given list of points.)" assert len(pointsList) >= 3, 'Not enough points to define a polygon (I require 3 or more.)' assert len(point) >= 2, 'Not enough dimensions to define a point(I require 2 or more.)' # If given values are ints, code will fail subtly. Force them to floats. x,y = float(point[0]), float(point[1]) xp = [float(p[0]) for p in pointsList] yp = [float(p[1]) for p in pointsList] # Initialize loop c=False i=0 npol = len(pointsList) j=npol-1 while i < npol: if ((((yp[i]<=y) and (y<yp[j])) or ((yp[j]<=y) and(y<yp[i]))) and (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])): c = not c j = i i += 1 return c |
Hele Code
Python:
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
| import pygame, sys, os, string from string import * from pygame.locals import * pygame.init() window = pygame.display.set_mode((400, 400)) pygame.display.set_caption('HexWar') screen = pygame.display.get_surface() # Array hexes = [ ((0,20), (10,0), (35,0), (45,20), (35,40), (10, 40)), ((35,40), (45,20), (70,20), (80,40), (70,60), (45, 60)), ((70,60), (80,40), (105,40), (115,60), (105,80), (80, 80)), ((70,20), (80,0), (105,0), (115,20), (105,40), (80, 40)) ] colors = [ 255000000,000255000,000000255,255255255 ] # Draw map for v,k in zip(hexes, colors): pygame.draw.polygon(screen, k, v) # Refresh Screen pygame.display.flip() # Point in hex def pointInPoly(point, pointsList): "Return True if point is contained in polygon (defined by given list of points.)" assert len(pointsList) >= 3, 'Not enough points to define a polygon (I require 3 or more.)' assert len(point) >= 2, 'Not enough dimensions to define a point(I require 2 or more.)' # If given values are ints, code will fail subtly. Force them to floats. x,y = float(point[0]), float(point[1]) xp = [float(p[0]) for p in pointsList] yp = [float(p[1]) for p in pointsList] # Initialize loop c=False i=0 npol = len(pointsList) j=npol-1 while i < npol: if ((((yp[i]<=y) and (y<yp[j])) or ((yp[j]<=y) and(y<yp[i]))) and (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])): c = not c j = i i += 1 return c # Main Loop def input(events): for event in events: if event.type == QUIT: sys.exit(0) elif event.type == 5: for k in hexes: if pointInPoly(event.pos ,k) == true: pygame.draw.polygon(screen, 000000000, k) else: print event pygame.display.flip() while True: input(pygame.event.get()) |
[ Voor 3% gewijzigd door Verwijderd op 25-02-2005 20:36 ]