Commits

Richard Jones committed f08c45d

add basic driving example (no collisions); use modenine in minesweeper

Comments (0)

Files changed (6)

Binary file added.
Added
New image
+<?xml version="1.0" encoding="UTF-8"?>
+<map version="1.0" orientation="orthogonal" width="20" height="20" tilewidth="64" tileheight="64">
+ <tileset firstgid="1" name="road-tiles" tilewidth="64" tileheight="64">
+  <image source="road-tiles.png" width="512" height="512"/>
+ </tileset>
+ <layer name="Tile Layer 1" width="20" height="20">
+  <data encoding="base64" compression="zlib">
+   eJxjZGBgYAJiKTw0jE0IMwMxJwP1ADcQK1LZPHT3sQCxLBCzItFsJJiH7j4VIOZFw2oUuA+beXwUuI+Hyu4TAWJdIBZFovUocB8lAJv7Rs0bNW/UPIR5gkAsRCUsDMQA1IwGLw==
+  </data>
+ </layer>
+</map>
+#!/usr/bin/python
+
+import math
+import tmx
+import pygame
+
+class CarSprite(pygame.sprite.Sprite):
+    TOP_SPEED = 10
+    REVERSE_TOP_SPEED = 10
+    ACCELERATION = 20
+    TURN_SPEED = 200
+
+    def __init__(self, *groups):
+        super(CarSprite, self).__init__(*groups)
+        self.src_image = pygame.image.load('car.png')
+        self.rect = self.src_image.get_rect()
+
+        self.position = (64, 64)
+        self.speed = 0
+        self.direction = -90
+        self.acc = 0
+        self.turn = 0
+
+    def update(self, dt):
+        pressed = pygame.key.get_pressed()
+        self.turn = 0
+        if pressed[pygame.K_RIGHT]:
+            self.turn -= self.TURN_SPEED
+        if pressed[pygame.K_LEFT]:
+            self.turn += self.TURN_SPEED
+        if pressed[pygame.K_UP]:
+            self.acc += self.ACCELERATION
+        if pressed[pygame.K_DOWN]:
+            self.acc -= self.ACCELERATION
+
+        self.speed += self.acc * dt
+        if self.speed > self.TOP_SPEED:
+            self.speed = self.TOP_SPEED
+        if self.speed < -self.REVERSE_TOP_SPEED:
+            self.speed = -self.REVERSE_TOP_SPEED
+        self.direction += self.turn * dt
+        x, y = self.position
+        rad = self.direction * math.pi / 180
+        x += -self.speed * math.sin(rad)# * dt
+        y += -self.speed * math.cos(rad)# * dt
+        self.position = (x, y)
+
+        self.image = pygame.transform.rotate(self.src_image, self.direction)
+        self.rect.size = self.image.get_size()
+        self.rect.center = map(int, self.position)
+
+def main():
+    screen = pygame.display.set_mode((800, 600))
+
+    map = tmx.load('driving-map.tmx', screen.get_size())
+    sprites = tmx.SpriteLayer()
+    map.layers.append(sprites)
+    car = CarSprite(sprites)
+
+    clock = pygame.time.Clock()
+    while True:
+        dt = clock.tick(30) / 1000.0
+
+        for event in pygame.event.get():
+            if event.type == pygame.QUIT:
+                return
+            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
+                return
+
+        screen.fill((50,50,50))
+        map.update(dt)
+        map.set_focus(*car.rect.center)
+        map.draw(screen)
+        pygame.display.flip()
+
+if __name__ == '__main__':
+    pygame.init()
+    main()
+
         if self.bomb is None:
             Cell.bomb = pygame.image.load('bomb.png')
             Cell.cover = pygame.image.load('cover.png')
-            Cell.font = pygame.font.SysFont('Helvetica', 16)
+            Cell.font = pygame.font.Font('MODENINE.TTF', 16)
         self.i, self.j = i, j
         self.has_bomb = has_bomb
         self.image = self.cover
             num = board.count(self)
             if num:
                 self.image = self.font.render(str(num), True, (0, 0, 0))
+                self.rect = self.image.get_rect()
+                self.rect.center = (self.i*16+8, self.j*16+8)
             else:
                 self.kill()
                 del board.cells[self.i, self.j]

road-tiles.png

Added
New image