Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/game1/tracktmx.py
blob: 886794205278570a97014849c848676d3e835333 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""

There are 4 types of tiles:

1) Navigable materials (lowercase letter) like grass, dirt
2) Non-navigable materials (uppercase letter) like Water, Plant, Rock, Tree, trunK
3) Dangerous materials (numbers) like black cuy (2) and owl (1)
4) Goals (symbols) like apples (#), oranges ($)

"""

import sys
import random
import pygame
import glob
import os.path
from pygame import *
from pygame.locals import *
from constants import *
from resources import *
from tiledtmxloader import *

class Track():
    
    def __init__(self):
        self.numbers_image = {}
        self.numbers_number = {}
        self.touched = {}
        self.load()

    def load(self, filename = "./tracks/track1.tmx"):
        self.world_map = TileMapParser().parse_decode(filename)
        self.world_map.load(ImageLoaderPygame())
        assert self.world_map.orientation == "orthogonal"

    def endOfTrack(self, offset = 0):
        return False

    def draw(self,  screen,  offset = 0): 
        # cam_offset is for scrolling
        cam_offset_x = 0
        cam_offset_y = -offset
        screen_width = screen_size[0]
        screen_height = screen_size[1]
        # draw the map
        for layer in self.world_map.layers[:]:
            if layer.visible:
                idx = 0
                # loop over all tiles
                for ypos in xrange(0, layer.height):
                    for xpos in xrange(0, layer.width):
                        # add offset in number of tiles
                        x = (xpos + layer.x) * self.world_map.tilewidth
                        y = (ypos + layer.y) * self.world_map.tileheight
                        # get the gid at this position
                        img_idx = layer.content2D[xpos][ypos]
                        material = self.world_map.indexed_tiles_tileset[img_idx]
                        idx += 1
                        if img_idx:
                            # get the actual image and its offset
                            offx, offy, screen_img = self.world_map.indexed_tiles[img_idx]
                            # only draw the tiles that are relly visible (speed up)
                            if x >= cam_offset_x - 3 * self.world_map.tilewidth and x + cam_offset_x <= screen_width + self.world_map.tilewidth\
                               and y >= cam_offset_y - 3 * self.world_map.tileheight and y + cam_offset_y <= screen_height + 3 * self.world_map.tileheight:
                                """
                                if screen_img.get_alpha():
                                    screen_img = screen_img.convert_alpha()
                                else:
                                    screen_img = screen_img.convert()
                                    if layer.opacity > -1:
                                        #print 'per surf alpha', layer.opacity
                                        screen_img.set_alpha(None)
                                        alpha_value = int(255. * float(layer.opacity))
                                        screen_img.set_alpha(alpha_value)
                                screen_img = screen_img.convert_alpha()
                                """
                                # draw image at right position using its offset
                                screen.blit(screen_img, (x + cam_offset_x + offx, y + cam_offset_y + offy + horizon_y))
                                if material == "goals":
                                    surface = self.getTextSurface(xpos, ypos, img_idx)
                                    screen.blit(surface, (x + cam_offset_x + offx + 10, y + cam_offset_y + offy + horizon_y + 10))
                                    
        """
        # map objects
        for obj_group in world_map.object_groups:
            goffx = obj_group.x
            goffy = obj_group.y
            if goffx >= cam_offset_x - 3 * world_map.tilewidth and goffx + cam_offset_x <= screen_width + world_map.tilewidth \
               and goffy >= cam_offset_y - 3 * world_map.tileheight and goffy + cam_offset_y <= screen_height + 3 * world_map.tileheight:
                for map_obj in obj_group.objects:
                    size = (map_obj.width, map_obj.height)
                    if map_obj.image_source:
                        surf = pygame.image.load(map_obj.image_source)
                        surf = pygame.transform.scale(surf, size)
                        screen.blit(surf, (goffx + map_obj.x + cam_offset_x, goffy + map_obj.y + cam_offset_y))
                    else:
                        r = pygame.Rect((goffx + map_obj.x + cam_offset_x, goffy + map_obj.y + cam_offset_y), size)
                        pygame.draw.rect(screen, (255, 255, 0), r, 1)
        """

    def getTextSurface(self, xtile, ytile, object):
        if self.numbers_image.has_key( (xtile, ytile) ):
            return self.numbers_image[(xtile, ytile)]
        if (object % 2) == 1:        
            number = random.randint(0,49) * 2 + 1
        elif  (object % 2) == 0:
            number = random.randint(0,49) * 2
        afont = pygame.font.SysFont("droidsans", 44)
        text = afont.render(str(number), 1, (255,255,255))
	self.numbers_number[(xtile, ytile)] = number
	self.numbers_image[(xtile, ytile)] = text
        return text
   
    def getTileNumberWC(self, x_wc, y_wc):
        return [x_wc / tile_size[0], y_wc / tile_size[1]]
   
    def getTileRectWC(self, x_wc, y_wc):
        tile_rect = Rect()
        tile_rect.top = y_wc / tile_size[1]
        tile_rect.left = x_wc / tile_size[0]
        tile_rect.width = tile_size[0]
        tile_rect.height = tile_size[1]
   
    def getTileType(self, tilex, tiley):
        return self.lines[tiley][tilex]
   
    def collidesWith(self, rect, justCheck = False):
        """ check collision """
        xtilemin, ytilemin = self.getTileNumberWC(rect.left, rect.top)
        xtilemax, ytilemax = self.getTileNumberWC(rect.right, rect.bottom)
        retmaterial = "navegable"
        for layer in self.world_map.layers[:]:
            for tiley in range(ytilemin, ytilemax + 1):
                for tilex in range(xtilemin, xtilemax + 1):
                    img_idx = layer.content2D[tilex][tiley]
                    material = self.world_map.indexed_tiles_tileset[img_idx]
                    if self.isGoal(material):
                        if not self.touched.has_key ( (tilex, tiley) ):
                            if not justCheck:
                                self.touched[ (tilex,tiley) ] = True
                            return self.numbers_number[(tilex, tiley)]
                    if self.isDanger(material):
                        if not self.touched.has_key ( (tilex, tiley) ):
                            if not justCheck:
                                self.touched[ (tilex,tiley) ] = True
                            return material                      
                    if self.isNotNavigable(material):
                        retmaterial = material
        return retmaterial 

    def isNavigable(self, tile):
        return tile == "navegable"

    def isNotNavigable(self, tile):
        return tile == "nonavegable"

    def isDanger(self, tile):
        return tile == "enemies"

    def isGoal(self, tile):
        return tile == "goals"