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:
self.quit()
if event.key == pg.K_LEFT:
self.player.move(dx=-1)
self.player.move(dx=-1, direction='Left')
if event.key == pg.K_RIGHT:
self.player.move(dx=1)
self.player.move(dx=1, direction='Right')
if event.key == pg.K_UP:
self.player.move(dy=-1)
self.player.move(dy=-1, direction='Up')
if event.key == pg.K_DOWN:
self.player.move(dy=1)
self.player.move(dy=1, direction='Down')
def show_start_screen(self):
pass

View File

@ -1,28 +1,64 @@
import pygame as pg
import enum
from settings import *
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
def __init__(self, game, x, y, direction = 'Right'):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
#self.image = pg.Surface((TILESIZE, TILESIZE))
self.image = pg.image.load('images/robot.bmp')
self.baseImage = pg.image.load('images/robot.bmp')
#self.image.fill(YELLOW)
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.x = x
self.y = y
self.direction = direction
def move(self, dx=0, dy=0):
def move(self, dx=0, dy=0, direction = ''):
if direction == self.direction:
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):
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
class Direction(enum.Enum):
Up = 1;
Left = 2;
Down = 3;
Right = 4;
class Mine(pg.sprite.Sprite):