-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSorterRobot.py
More file actions
146 lines (116 loc) · 3.68 KB
/
SorterRobot.py
File metadata and controls
146 lines (116 loc) · 3.68 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
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
import pygame
import Entity
import Tile
import Keyboard
import random
import scoring
import Jobs
import BubbleSort
import HeapSort
import BogoSort
import GeneticSort
import CocktailSort
#import mergeSort
class SorterRobot(Entity.Entity):
def __init__(self,level, x, y):
super(SorterRobot,self).__init__(level,x,y)
self.isSwimming = False
self.isMoving = False
self.img = pygame.image.load("robots/robot.png")
self.dest = (x>>5,y>>5)
self.speed = 4
self.treasures = []
for i in range(10):
self.treasures.append(random.randint(1,100))
print self.treasures
self.inHand = self.treasures[0]
self.inContainer = self.treasures[1]
self.currentJob = None
#self.jobs = CocktailSort.createJobs(self.treasures,self)
self.jobs = BubbleSort.createJobs(self.treasures,len(self.treasures),True)
#self.jobs = GeneticSort.geneticSort(self.treasures,self)
#self.jobs = mergeSort.mergeSort(self.treasures)
self.jobs[0].doJob(self)
def goTo(self,index):
x = (index * 2 + 2)
if x>10:x=x-10
y = ((index / 5)+1) * 3
print x,y
self.dest = (x,y)
"""Determins if the entity has collided
@Params:
None
@Return:
hasCollided(boolean): if the entity has collided
"""
def hasCollided(self,xa, ya):
xMin = 31
xMax = 31
yMin = 59
yMax = 60
for x in range(xMin,xMax):
if self.isSolidTile(xa, ya, x, yMin):
return True
for x in range(xMin,xMax):
if self.isSolidTile(xa, ya, x, yMax):
return True
for y in range(yMin,yMax):
if self.isSolidTile(xa, ya, xMin, y):
return True
for y in range(yMin,yMax):
if self.isSolidTile(xa, ya, xMax, y):
return True
return False
"""Updates logic associated with entity
@Params:
None
@Retrun:
None
"""
def tick(self):
global x,y
super(SorterRobot,self).tick()
self.currentJob.checkHasDone(self)
if self.currentJob.jobDone==True:
self.jobs.remove(self.currentJob)
self.jobs[0].doJob(self)
xa = 0
ya = 0
self.centreX= self.x+31
self.centreY= self.y+59
xx = self.centreX >>5
yy = self.centreY >>5
if self.dest!=None:
if xx < self.dest[0]:
xa=1
if yy < self.dest[1]:
ya=1
if xx > self.dest[0]:
xa=-1
if yy > self.dest[1]:
ya=-1
if xx==self.dest[0] and yy==self.dest[1]:
self.currentJob.jobDone = True
self.dest=None
if xa != 0 or ya != 0:
self.isMoving = not self.move(xa, ya)
else:
self.isMoving = False
if self.getTileUnder().getId() == Tile.water.getId():
self.isSwimming = True
else:
self.isSwimming = False
"""Renders the entity to the screen
@Params:
screen(pygame.Surface): suface to draw to
xoff(int): x offset of screen
yoff(int): y offset of screen
@Return:
None
"""
def render(self,screen,xoff,yoff):
if self.isSwimming:
source_area = pygame.Rect((0,0), (self.img.get_width(), self.img.get_height()/2))
screen.blit(self.img,(self.x-xoff,self.y-yoff+32),source_area)
else:
screen.blit(self.img, (self.x-xoff,self.y-yoff))