#!/usr/bin/python # -*- coding: iso-8859-1 -*- import os, random from math import sin, cos try: import wx except ImportError: raise ImportError, 'The wxPython module is required to run this program' map=[] a=77 grass=0 sea=1 desert=2 red_color = (250, 0, 0) yellow_color = (255, 255, 0) green_color = (0, 255, 0) blue_color = (0, 0, 255) white_color = (255, 255, 255) class Hexagon: def __init__(self, coord): self.coord=coord self.terrain=random.randint(0,2) def create_map(height, width): for i in range(width): map.append([]) for j in range(height): map[i].append(Hexagon((i,j))) class MapDisplay(wx.Panel): def __init__(self, parent): self.parent=parent wx.Panel.__init__(self, parent, size=(800, 600)) # Couleur de fond self.SetBackgroundColour('black') self.Bind(wx.EVT_PAINT, self.OnPaint) # Clics de souris self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) def OnPaint(self, event=None): dc = wx.PaintDC(self) dc.Clear() # Conversion de l'image en bitmap grassfile = os.path.join('data', 'grass.png') self.grassbmp=wx.Image(grassfile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() seafile = os.path.join('data', 'sea.png') self.seabmp=wx.Image(seafile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() desertfile = os.path.join('data', 'desert.png') self.desertbmp=wx.Image(desertfile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() for i in range(len(map)): for j in range(len(map[i])): self.draw_hexagon(dc, (i, j)) def OnLeftDown(self, event): mouse_x, mouse_y=event.GetPosition() #mouse_map_height,mouse_map_width=self.GetSizeTuple() mouse_map_height,mouse_map_width=(77, 77) region_x=int(mouse_x/mouse_map_width) region_y=int(mouse_y/mouse_map_height*2) # Find out where in the mousemap the mouse is mouse_map_x=mouse_x % mouse_map_width mouse_map_y= mouse_y % mouse_map_height hexagonfile = os.path.join('data', 'hexagon.png') hexagonimg=wx.Image(hexagonfile, wx.BITMAP_TYPE_ANY) red=hexagonimg.GetRed(mouse_map_x, mouse_map_y) green= hexagonimg.GetGreen(mouse_map_x, mouse_map_y) blue=hexagonimg.GetBlue(mouse_map_x, mouse_map_y) pixel_color = (red, green, blue) region_dx = 0 region_dy = 0 if pixel_color == red_color: region_dx = -1 region_dy = -1 elif pixel_color == yellow_color: region_dx = 1 region_dy = -1 elif pixel_color == white_color: region_dx = 0 region_dy = 0 elif pixel_color == green_color: region_dx = -1 region_dy = 0 elif pixel_color == blue_color: region_dx = 1 region_dy = 0 tile_x = region_x + region_dx tile_y = region_y + region_dy print (tile_x,tile_y) def draw_hexagon(self, dc, coord): terrain=map[coord[0]][coord[1]].terrain if coord[1]%2==0: x_pixel=coord[0]*a y_pixel=coord[1]*0.75*a else: x_pixel=coord[0]*a+(77/2.0) y_pixel=coord[1]*0.75*a if terrain==0: dc.DrawBitmap(self.grassbmp, x_pixel, y_pixel) elif terrain==1: dc.DrawBitmap(self.seabmp, x_pixel, y_pixel) elif terrain ==2: dc.DrawBitmap(self.desertbmp, x_pixel, y_pixel) dc.DrawText('[%d, %d]'%(coord[0],coord[1]), x_pixel+30,y_pixel+30) class MainFrame(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent, title=title, size=(800,600)) self.parent=parent self.initialize() def initialize(self): sizer1=wx.BoxSizer(wx.VERTICAL) sizer2=wx.BoxSizer(wx.HORIZONTAL) self.label=wx.StaticText(self, label='Test') # Couleur self.label.SetBackgroundColour(wx.BLUE) self.label.SetForegroundColour(wx.WHITE) sizer2.Add(self.label) self.entry=wx.TextCtrl(self, value='1233') self.entry.SetFocus() sizer2.Add(self.entry) sizer1.Add(sizer2) button = wx.Button(self, label='+') self.Bind(wx.EVT_BUTTON, self.OnButtonClick, button) sizer1.Add(button) map=MapDisplay(self) sizer1.Add(map) self.SetSizerAndFit(sizer1) self.Show(True) self.playsound() def OnButtonClick(self, event): print self.entry.GetValue() def playsound(self): fullname=os.path.join('data', 'BLOOZ01.WAV') sound=wx.Sound(fullname) sound.Play(wx.SOUND_ASYNC) if __name__ == '__main__': create_map(10, 10) app=wx.App(False) frame=MainFrame(None, 'Hexaengine') app.MainLoop()