changeset 6:eaeb703bc91e

boulders and gems
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sat, 26 Apr 2014 15:21:52 +0200
parents 022ecd420594
children 996d2a83a7ee
files undermine/game.py
diffstat 1 files changed, 30 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/undermine/game.py	Sat Apr 26 14:58:07 2014 +0200
+++ b/undermine/game.py	Sat Apr 26 15:21:52 2014 +0200
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+import random
+
 import pygame
 
 import undermine.display
@@ -9,26 +11,22 @@
 
 
 class Game(object):
-    def __init__(self, fps=24):
-        map_size = (12, 12)
-        self.map = [[0] * map_size[0] for y in range(map_size[1])]
+    def __init__(self, fps=24, map_size=(12, 12)):
         self.fps = fps
         self.display = undermine.display.Display(
-            (len(self.map[0]) * 64, len(self.map) * 64))
+            (map_size[0] * 64, map_size[1] * 64))
         self.clock = pygame.time.Clock()
         self.images = undermine.resources.load_atlas('undermine.png', 64)
-        self.hero = undermine.sprites.Hero(self.images)
-        self.display.sprites.add(self.hero)
-        self.hero_pos = 0, 0
+        self.make_map(map_size)
 
     def move(self, dx, dy):
-        x, y = self.hero_pos
+        x, y = self.hero.pos
         if not (0 <= x + dx <= len(self.map[0]) and
                 0 <= y +dy < len(self.map)):
             return
         self.hero.animations.append(self.hero.move_anim(dx, dy))
-        self.hero_pos = x + dx, y + dy
-        self.dig(self.hero_pos)
+        self.hero.place((x + dx, y + dy), False)
+        self.dig((x + dx, y + dy))
 
     def dig(self, (x, y), tile=1):
         self.map[y][x] = tile
@@ -36,6 +34,28 @@
         self.display.screen.blit(self.images[tile + 8], (x * 64, y * 64))
         self.display.background.blit(self.images[tile + 8], (x * 64, y * 64))
 
+    def make_map(self, map_size):
+        squares = []
+        self.map = []
+        for y in range(map_size[1]):
+            self.map.append([])
+            for x in range(map_size[0]):
+                self.map[-1].append(0)
+                squares.append((x, y))
+        random.shuffle(squares)
+        self.hero = undermine.sprites.Hero(self.images)
+        self.hero.place(squares.pop())
+        self.display.sprites.add(self.hero)
+        self.map[self.hero.pos[1]][self.hero.pos[0]] = 1
+        for i in range(8):
+            boulder = undermine.sprites.Boulder(self.images[4])
+            boulder.place(squares.pop())
+            self.display.sprites.add(boulder)
+        for i in range(4):
+            gem = undermine.sprites.Gem(self.images[5])
+            gem.place(squares.pop())
+            self.display.sprites.add(gem)
+
     def draw_map(self):
         for y, row in enumerate(self.map):
             for x, tile in enumerate(row):