Implement direction movement

This commit is contained in:
eugenep 2021-04-08 16:29:37 +02:00
parent e5269af7bb
commit d0d7376ad8
3 changed files with 44 additions and 8 deletions

BIN
images/robot1.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -79,13 +79,13 @@ class Game:
if event.key == pg.K_ESCAPE: if event.key == pg.K_ESCAPE:
self.quit() self.quit()
if event.key == pg.K_LEFT: if event.key == pg.K_LEFT:
self.player.move(dx=-1) self.player.move(dx=-1, direction='Left')
if event.key == pg.K_RIGHT: if event.key == pg.K_RIGHT:
self.player.move(dx=1) self.player.move(dx=1, direction='Right')
if event.key == pg.K_UP: if event.key == pg.K_UP:
self.player.move(dy=-1) self.player.move(dy=-1, direction='Up')
if event.key == pg.K_DOWN: if event.key == pg.K_DOWN:
self.player.move(dy=1) self.player.move(dy=1, direction='Down')
def show_start_screen(self): def show_start_screen(self):
pass pass

View File

@ -1,28 +1,64 @@
import pygame as pg import pygame as pg
import enum
from settings import * from settings import *
class Player(pg.sprite.Sprite): class Player(pg.sprite.Sprite):
def __init__(self, game, x, y): def __init__(self, game, x, y, direction = 'Right'):
self.groups = game.all_sprites self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups) pg.sprite.Sprite.__init__(self, self.groups)
self.game = game self.game = game
#self.image = pg.Surface((TILESIZE, TILESIZE)) #self.image = pg.Surface((TILESIZE, TILESIZE))
self.image = pg.image.load('images/robot.bmp') self.image = pg.image.load('images/robot.bmp')
self.baseImage = pg.image.load('images/robot.bmp')
#self.image.fill(YELLOW) #self.image.fill(YELLOW)
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE)) self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
self.baseImage = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.x = x self.x = x
self.y = y self.y = y
self.direction = direction
def move(self, dx=0, dy=0): def move(self, dx=0, dy=0, direction = ''):
self.x += dx if direction == self.direction:
self.y += dy if self.direction == Direction.Right.name:
if dx > 0:
self.x += dx
if self.direction == Direction.Up.name:
if dy < 0:
self.y += dy
if self.direction == Direction.Down.name:
if dy > 0:
self.y += dy
if self.direction == Direction.Left.name:
if dx < 0:
self.x += dx
elif direction != self.direction:
self.direction = direction
print(self.direction)
"""if self.direction == Direction.Up.name:
self.image = pg.transform.rotate(self.baseImage, 90)
if self.direction == Direction.Right.name:
self.image == pg.transform.rotate(self.baseImage, 360)
if self.direction == Direction.Down.name:
self.image == pg.transform.rotate(self.baseImage, -90)
if self.direction == Direction.Left.name:
self.image == pg.transform.rotate(self.baseImage, -180)"""
def update(self): def update(self):
self.rect.x = self.x * TILESIZE self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE self.rect.y = self.y * TILESIZE
class Direction(enum.Enum):
Up = 1;
Left = 2;
Down = 3;
Right = 4;
class Mine(pg.sprite.Sprite): class Mine(pg.sprite.Sprite):