Commits

Pierre Surply committed 1d7acad

Added animated movement

Comments (0)

Files changed (8)

res/img/robots.png

Old
Old image
New
New image
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>. 
 ## 
 ## Started on  Thu Jun 28 21:12:48 2012 Pierre Surply
-## Last update Sun Aug 26 19:29:33 2012 Pierre Surply
+## Last update Mon Sep  3 18:11:22 2012 Pierre Surply
 ##
 
 import pygame
     def update_term(self):
         self.terminal.clear()
         self.terminal.write("Please select a landing site for the mothership\n\n")
-        self.info_biomes(self.worldmap.tile[self.select_x][self.select_y])
+        self.info_biomes(self.worldmap.tile[(self.select_y*self.worldmap.size)+self.select_x])
         self.terminal.write("\n\n")
         self.info_commands()
 
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>. 
 ## 
 ## Started on  Wed May  9 17:19:46 2012 Pierre Surply
-## Last update Mon Aug 27 16:01:43 2012 Pierre Surply
+## Last update Mon Sep  3 17:25:44 2012 Pierre Surply
 ##
 
 import pygame
                                                                      pygame.Color(0, 100, 0, 255),\
                                                                      pygame.Color(255, 255, 255, 255)),\
                                                (8, 12))
+        self.last_ticks = pygame.time.get_ticks()
+        self.frame = 0
 
     def set_display_list(self):
         self.list_char = glGenLists(1)
                     glPopMatrix()
 
     def flip(self):
+        self.frame += 1
+        if self.frame >= 100:
+            self.frame = 0
+            current_ticks = pygame.time.get_ticks()
+            pygame.display.set_caption("BASIC-RoBots - " + \
+                                           str(100000 / (current_ticks - self.last_ticks)) + " fps")
+            self.last_ticks = pygame.time.get_ticks()
         pygame.display.flip()
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
 
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>. 
 ##
 ## Started on  Thu Jun 28 13:22:05 2012 Pierre Surply
-## Last update Mon Sep  3 15:30:29 2012 Pierre Surply
+## Last update Tue Sep  4 10:57:45 2012 Pierre Surply
 ##
 
 import os
 import random
 import math
+import array
 import pygame
 from pygame.locals import *
 from OpenGL.GL import *
             self.build(biome)
             self.save()
         self.sprite_floor = surface.cut_surface(pygame.image.load("res/img/floor.png").convert_alpha(), (16, 16))
-        self.sprite_floor_mini = surface.cut_surface(pygame.image.load("res/img/floor_mini.png").convert_alpha(), (2, 2))
         self.sprite_elts = surface.cut_surface(pygame.image.load("res/img/lvl_elts.png").convert_alpha(), (16, 16))
         self.sprite_robots = surface.cut_surface(pygame.image.load("res/img/robots.png").convert_alpha(), (64, 32))
         self.tps_anim_sprite = 0
         gluQuadricTexture(self.quad, GL_TRUE);
         self.list_robot = glGenLists(3)
         glNewList(self.list_robot,GL_COMPILE)
-        gluSphere(self.quad, 0.4, 10, 10)
+        gluSphere(self.quad, 0.3, 16, 16)
         glEndList()
         
     def init_tile(self):
-        self.tile = [[0] * self.size for i in range(self.size)]
-        self.tile_elts = [[None] * self.size for i in range(self.size)]
-        self.light = [[0] * self.size for i in range(self.size)]
+        self.tile = array.array('B', [0] * (self.size**2))
+        self.tile_elts = array.array('B', [0xFF] * (self.size**2))
+        self.light = array.array('B', [0] * (self.size**2))
 
     def load(self):
         self.load_img(self.path_img, self.size, self.tile, self.color2tile)
             i.save()
 
     def tile2color(self, (x, y)):
-        tile = self.tile[x][y]
+        tile = self.tile[(y*self.size)+x]
         for i in self.temp_tiles:
             if tile == i[1]:
                 return pygame.Color(i[0][0],\
         return 0
 
     def elt2color(self, (x, y)):
-        elt = self.tile_elts[x][y]
+        elt = self.tile_elts[(y*self.size)+x]
         for i in self.temp_elts:
             if elt == i[1]:
                 return pygame.Color(i[0][0],\
             for i in self.temp_elts:
                 if color == i[0]:
                     return i[1]
+            return 0xFF
         else:
-            return None
+            return 0xFF
 
     def get_daylight(self):
         t = pygame.time.get_ticks() % time_day
             return 0xFF - (0xFF * (4 * (t-time_day/2))/time_day)
 
     def calculate_light(self):
-        self.light = [[0] * self.size for i in range(self.size)]
+        self.light = array.array('B', [0] * (self.size**2))
         for i in self.robots:
             self.rec_calculate_light((i.pos_x, i.pos_y), 0, 0xB0)
         for y in range(self.size):
             for x in range(self.size):
-                if self.tile[x][y] == 6:
+                if self.tile[(y*self.size)+x] == 6:
                     self.rec_calculate_light((x, y), 0, 0x80)
 
     def rec_calculate_light(self, (org_x, org_y), dist, intens):
             self.rec_calculate_light((org_x-1, org_y-1), dist+2, intens-0x44)
 
     def set_light(self, (x, y), intens):
-        org = self.light[x][y]
+        org = self.light[(y*self.size)+x]
         light = org + intens
         if light > 0xFF:
             light = 0xFF
-        self.light[x][y] = light
+        self.light[(y*self.size)+x] = light
 
     def set_color(self, (x, y)):
         l = 0
         if x >= 0 and x < self.size and\
                 y >=0 and y < self.size:
-            l = self.light[x][y] + self.get_daylight()
+            l = self.light[(y*self.size)+x] + self.get_daylight()
             if l > 0xFF:
                 l = 0xFF
         glColor3ub(l, l, l)
         lbl = 0
         lbr = 0
         if x > 0 and y > 0:
-            ltl = self.light[x-1][y-1]
+            ltl = self.light[((y-1)*self.size)+x-1]
         if x < self.size and y > 0:
-            ltr = self.light[x][y-1]
+            ltr = self.light[((y-1)*self.size)+x]
         if x > 0 and y < self.size:
-            lbl = self.light[x-1][y]
+            lbl = self.light[(y*self.size)+x-1]
         if x < self.size and y < self.size:
-            lbl = self.light[x][y]
+            lbl = self.light[(y*self.size)+x]
         l = (ltl + ltr + lbl + ltr) / 4 + self.get_daylight()
         if l > 0xFF:
             glColor3ub(0xFF, 0xFF, 0xFF)
             glColor3ub(l, l, l)
             
     def render_case(self, x, y):
-        tile = self.tile[x][y]
+        tile = self.tile[(y*self.size)+x]
         if tile == 2:
             glBindTexture(GL_TEXTURE_2D, self.sprite_water[self.id_sprite_water])
         elif tile == 4:
             glBindTexture(GL_TEXTURE_2D, self.sprite_water[self.id_sprite_water+8])
         else:
             glBindTexture(GL_TEXTURE_2D, self.sprite_floor[tile])
-        z = self.temp_tiles[self.tile[x][y]][3]
+        z = self.temp_tiles[self.tile[(y*self.size)+x]][3]
         glPushMatrix()
         glTranslated(x, y, z)
         glBegin(GL_QUADS)
         glEnd()
         self.set_color((x, y))
         #glCallList(self.list_floor)
-        if (not self.tile[x][y] in [2, 4]):
-            if (y >= self.size-1 or self.temp_tiles[self.tile[x][y+1]][3] < z):
+        if (not self.tile[(y*self.size)+x] in [2, 4]):
+            if (y >= self.size-1 or self.temp_tiles[self.tile[((y+1)*self.size)+x]][3] < z):
                 glPushMatrix()
                 glTranslated(0, 1, -1)
                 glCallList(self.list_floor_side)
                 glPopMatrix()
-            if (y <= 0 or self.temp_tiles[self.tile[x][y-1]][3] < z):
+            if (y <= 0 or self.temp_tiles[self.tile[((y-1)*self.size)+x]][3] < z):
                 glPushMatrix()
                 glTranslated(0, 0, -1)
                 glCallList(self.list_floor_side)
                 glPopMatrix()
-            if (x <= 0 or self.temp_tiles[self.tile[x-1][y]][3] < z):
+            if (x <= 0 or self.temp_tiles[self.tile[(y*self.size)+x-1]][3] < z):
                 glPushMatrix()
                 glTranslated(0, 0, -1)
                 glRotated(90, 0, 0, 1)
                 glCallList(self.list_floor_side)
                 glPopMatrix()
-            if (x >= self.size-1 or self.temp_tiles[self.tile[x+1][y]][3] < z):
+            if (x >= self.size-1 or self.temp_tiles[self.tile[(y*self.size)+x+1]][3] < z):
                 glPushMatrix()
                 glTranslated(1, 0, -1)
                 glRotated(90, 0, 0, 1)
         glPopMatrix()
 
     def render_elt(self, x, y, p_x, p_y): 
-        id_elt = self.tile_elts[x][y]
+        id_elt = self.tile_elts[(y*self.size)+x]
         glBindTexture(GL_TEXTURE_2D, self.sprite_elts[id_elt])
-        z = self.temp_tiles[self.tile[x][y]][3]
-        model = self.temp_elts[self.tile_elts[x][y]][3]
+        z = self.temp_tiles[self.tile[(y*self.size)+x]][3]
+        model = self.temp_elts[self.tile_elts[(y*self.size)+x]][3]
         glPushMatrix()
         glTranslated(x, y, z)
         if model == 1:
 
     def render_robot(self, r):
         glBindTexture(GL_TEXTURE_2D, self.sprite_robots[r.id_sprite])
-        z = self.temp_tiles[self.tile[r.pos_x][r.pos_y]][3] + 0.5
+        z = self.temp_tiles[self.tile[(r.pos_y*self.size)+r.pos_x]][3] + 0.5 + r.pos_z
         glPushMatrix()
-        glTranslated(r.pos_x+0.5, r.pos_y+0.5, z)
-        glRotated(90*r.orient, 0, 0, 1)
+        glTranslated(r.real_pos_x+0.5, r.real_pos_y+0.5, z)
+        glRotated(r.angle, 0, 0, 1)
         glCallList(self.list_robot)
         glPopMatrix()
 
     def render(self, (select_x, select_y, cam_w, cam_h), (dx, dy)):
-        p_x = -2.5
-        p_y = 8
-        p_z = 12
+        p_x = -1.5
+        p_y = 5
+        p_z = 9
         glMatrixMode(GL_PROJECTION)
         glPushMatrix()
         glLoadIdentity()
                       0, 0, 1)
         glRotated(self.angle, 0, 0, 1)
 
-        z = self.temp_tiles[self.tile[select_x][select_y]][3] + 0.1
+        z = self.temp_tiles[self.tile[(select_y*self.size)+select_x]][3] + 0.1
         glBindTexture(GL_TEXTURE_2D, self.cursor)
         glPushMatrix()
         glTranslated(-dx, -dy, z)
         glPopMatrix()
         glTranslated(-select_x-0.5-dx, -select_y-0.5-dy, 0);
 
-        for y in range(max(select_y-12, 0), min(select_y+8, self.size)):
-            for x in range(max(select_x-7, 0), min(select_x+12, self.size)):
+        for y in range(max(select_y-6, 0), min(select_y+5, self.size)):
+            for x in range(max(select_x-4, 0), min(select_x+7, self.size)):
                 self.render_case(x, y)
-                if self.tile_elts[x][y] != None:
+                if self.tile_elts[(y*self.size)+x] != 0xFF:
                     self.render_elt(x, y, p_x+select_x, p_y+select_y)
         glColor3ub(0xFF, 0xFF, 0xFF)
         for i in self.robots:
     def render_mini(self, surface, (dx, dy)):
         for y in range(self.size):
             for x in range(self.size):
-                surface.blit(self.sprite_floor_mini[self.tile[x][y]], (x*2+dx, y*2+dy))
+                surface.blit(self.sprite_floor_mini[self.tile[(y*self.size)+x]], (x*2+dx, y*2+dy))
 
     def build(self, biome):
         n = noise.Noise(33, 33, 8, 8)
         for y in range(self.size):
             for x in range(self.size):
-                self.tile[x][y] = self.get_rndtile(x, y, n, biome)
+                self.tile[(y*self.size)+x] = self.get_rndtile(x, y, n, biome)
         self.build_elts()
         self.land_mothership()
                 
         for y in range(self.size):
             for x in range(self.size):
                 elt = self.get_rndelts(x, y, n, biome)
-                tile = self.tile[x][y]
-                self.tile_elts[x][y] = None
+                tile = self.tile[(y*self.size)+x]
+                self.tile_elts[(y*self.size)+x] = 0xFF
                 if elt in [0, 1]:
                     if tile in [0,1]:
-                        self.tile_elts[x][y] = elt
+                        self.tile_elts[(y*self.size)+x] = elt
                 elif elt in [2, 3, 4, 5]:
                     if tile in [0,1, 5, 7, 8, 9, 10, 11]:
-                        self.tile_elts[x][y] = elt
+                        self.tile_elts[(y*self.size)+x] = elt
                 else:
-                    self.tile_elts[x][y] = elt
+                    self.tile_elts[(y*self.size)+x] = elt
                 
     def get_rndelts(self, x, y, noise, biome):
         val = noise.smooth_noise(x, y, 0.7)
         for i in self.biomes[biome][1]:
             if val < i[0]:
                 return i[1]
-        return None
+        return 0xFF
 
     def land_mothership(self):
         r = mothership.Mothership(True,\
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ##  
 ## Started on  Thu Jun 28 15:31:47 2012 Pierre Surply
-## Last update Wed Aug 29 12:19:00 2012 Pierre Surply
+## Last update Tue Sep  4 11:00:38 2012 Pierre Surply
 ##
 
 import pygame
             self.env.render((self.select_x, self.select_y, \
                                      display.window.get_width(), \
                                      display.window.get_height()), \
-                                (2, 1))
+                                (2+self.env.robots[self.select_robot].real_pos_x-self.select_x, \
+                                     self.env.robots[self.select_robot].real_pos_y-self.select_y))
             display.render_terminal()
         else:
             self.handle_events(events)

src/robot/robot.py

 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ## 
 ## Started on  Sun Jul  1 15:34:10 2012 Pierre Surply
-## Last update Sun Sep  2 13:36:14 2012 Pierre Surply
+## Last update Tue Sep  4 11:04:18 2012 Pierre Surply
 ##
 
 import os
         self.env = env
         self.pos_x = x
         self.pos_y = y
+        self.pos_z = float(random.randint(0, 200)) / 1000
+        self.d_pos_z = 0.002
+        self.real_pos_x = 0.0
+        self.real_pos_y = 0.0
+        self.angle = 0
         self.orient = 0
         self.id_sprite = 0
         self.name = name
     def update(self, selected, display, events):
         if selected:
             self.handle_events(events)
+        self.pos_z += self.d_pos_z
+        if self.pos_z > 0.2:
+            self.d_pos_z = -0.002
+        elif self.pos_z < 0:
+            self.d_pos_z = 0.002
+        th_angle = 90 * self.orient
+        if (self.angle%360 != th_angle):
+            if (th_angle-self.angle)%360 < 180:
+                self.angle += 10
+            else:
+                self.angle -= 10
+        if self.pos_x - 0.1 > self.real_pos_x:
+            self.real_pos_x += 0.1
+        elif self.pos_x + 0.1 < self.real_pos_x:
+            self.real_pos_x -= 0.1
+        if self.pos_y - 0.1 > self.real_pos_y:
+            self.real_pos_y += 0.1
+        elif self.pos_y + 0.1 < self.real_pos_y:
+            self.real_pos_y -= 0.1
         self.os.update(selected, display, events)
 
     def handle_events(self, events):
             f.close()
             self.pos_x = int(buf[0])
             self.pos_y = int(buf[1])
+            self.real_pos_x = float(self.pos_x)
+            self.real_pos_y = float(self.pos_y)
             self.orient = int(buf[2])
+            self.angle = 90*self.orient
 
     def load_prop(self):
         if os.path.isfile("saves/"+self.world+"/robots/"+self.name+"/prop"):
                     y >= 0 and \
                     x < self.env.size and\
                     y < self.env.size and\
-                    self.env.tile_elts[x][y] == None and\
-                    not self.env.tile[x][y] in self.cannot_move and\
+                    self.env.tile_elts[(y*self.env.size)+x] == 0xFF and\
+                    not self.env.tile[(y*self.env.size)+x] in self.cannot_move and\
                     not (x, y) in self.env.get_pos_robots())
 
     def run_ext_cmd(self, f, price):
     def findpath(self):
         if 0 <= self.mem["A"] < self.env.size and \
                 0 <= self.mem["B"] < self.env.size:
-            path =  pathfinding.findpath(self.coll, \
-                                             (self.env.size, self.env.size), \
-                                             (self.pos_x, self.pos_y), \
-                                             (self.mem["A"], self.mem["B"]), \
-                                             1 << 8)
+            path = pathfinding.findpath(self.coll, \
+                                            (self.env.size, self.env.size), \
+                                            (self.pos_x, self.pos_y), \
+                                            (self.mem["A"], self.mem["B"]), \
+                                            1 << 8)
             if path == None:
                 return 0
             else:
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ##
 ## Started on  Sat Jun 30 15:34:37 2012 Pierre Surply
-## Last update Wed Aug  1 17:50:05 2012 Pierre Surply
+## Last update Mon Sep  3 18:04:50 2012 Pierre Surply
 ##
 
 import pygame
         surface = pygame.image.load(path)
         for y in range(min(size, surface.get_height())):
             for x in range(min(size, surface.get_width())):
-                tile[x][y] = function(tuple(surface.get_at((x, y))))
+                tile[(y*size)+x] = function(tuple(surface.get_at((x, y))))
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ## 
 ## Started on  Thu Jun 28 20:42:21 2012 Pierre Surply
-## Last update Wed Aug 29 15:15:47 2012 Pierre Surply
+## Last update Mon Sep  3 18:12:42 2012 Pierre Surply
 ##
 
 import os
 import pygame
+import array
 from pygame.locals import *
 from OpenGL.GL import *
 from OpenGL.GLU import *
         self.build()
 
     def init_tile(self):
-        self.tile = [[0] * self.size for i in range(self.size)]
+        self.tile = array.array('B', [0] * (self.size**2))
         self.env = [[None] * self.size for i in range(self.size)]
         
     def render(self, (w, h), (sx, sy)):
         dly = dy + (h/2) + (((h/2) - size_large_biome*9)/2)
         for y in range(self.size):
             for x in range(self.size):
-                glBindTexture(GL_TEXTURE_2D, self.sprite_biomes[self.tile[x][y]])
+                glBindTexture(GL_TEXTURE_2D, self.sprite_biomes[self.tile[(y*self.size)+x]])
                 glBegin(GL_QUADS)
                 
                 glTexCoord2f(0, 0); glVertex2f(x*size_biome+dx, (y+1)*size_biome+dy)
         #    self.env[sx][sy].render_mini(surface, (dx-1, (self.size*8) + dy+2))
         for y in range(max(0,sy - 4), min(sy + 4, self.size)):
             for x in range(max(0,sx - 4), min(sx + 4, self.size)):
-                glBindTexture(GL_TEXTURE_2D, self.sprite_biomes[self.tile[x][y]])
+                glBindTexture(GL_TEXTURE_2D, self.sprite_biomes[self.tile[(y*self.size)+x]])
                 glBegin(GL_QUADS)
                 
                 glTexCoord2f(0, 0); glVertex2f((x-(sx-4))*size_large_biome+dlx, \
         for y in range(self.size):
             for x in range(self.size):
                 biome = self.get_rndtile(x, y, n)
-                self.tile[x][y] = biome
+                self.tile[(y*self.size)+x] = biome
 
     def get_rndtile(self, x, y, noise):
         val = noise.smooth_noise(x, y, 0.4)
     def build_env(self, (x, y), events):
         if self.env[x][y] == None:
             self.env[x][y] = env.Env(self.path, \
-                                         self.tile[x][y],\
+                                         self.tile[(y*self.size)+x],\
                                          (x, y), events)
 
 
             for x in range(self.size):
                 if os.path.isfile("saves/" + self.path + "/env/" + str(x) + "_" + str(y) + ".bmp"):
                     self.env[x][y] = env.Env(self.path, \
-                                                 self.tile[x][y],\
+                                                 self.tile[(y*self.size)+x],\
                                                  (x, y), events)
 
         for i in os.listdir("saves/" + self.path + "/robots"):
                     self.env[x][y].save()
 
     def tile2color(self, (x, y)):
-        n = self.tile[x][y]
+        n = self.tile[(y*self.size)+x]
         for i in self.biomes:
             if n == i[1]:
                 return pygame.Color(i[0][0], \