GenericAI_Sweeper/sprites.py
2021-04-10 13:48:55 +02:00

146 lines
5.2 KiB
Python

import pygame as pg
import enum
import ctypes
from settings import *
class Player(pg.sprite.Sprite):
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, direction = ''):
if direction == self.direction:
if self.direction == Direction.Right.name:
if dx > 0:
if self.check_border(dx):
if self.collide_with_mines(dx):
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
else:
self.x += dx
if self.direction == Direction.Up.name:
if dy < 0:
if self.check_border(0, dy):
if self.collide_with_mines(0, dy):
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
else:
self.y += dy
if self.direction == Direction.Down.name:
if dy > 0:
if self.check_border(0, dy):
if self.collide_with_mines(0, dy):
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
else:
self.y += dy
if self.direction == Direction.Left.name:
if dx < 0:
if self.check_border(dx):
if self.collide_with_mines(dx):
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
else:
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 check_border(self, dx=0, dy=0):
if (self.x + dx) < 0 or (self.y + dy) < 0 or (self.x + dx) >= MAP_SIZE or (self.y + dy) >= MAP_SIZE :
return False
else:
return True
def collide_with_mines(self, dx=0, dy=0):
for mine in self.game.mines:
if mine.x == self.x + dx and mine.y == self.y + dy:
return True
return False
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):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.mines
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
#self.image = pg.Surface((TILESIZE, TILESIZE))
self.image = pg.image.load('images/mine.bmp')
#self.image.fill(YELLOW)
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.x = x
self.y = y
def update(self):
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
class Bomb(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.mines
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
#self.image = pg.Surface((TILESIZE, TILESIZE))
self.image = pg.image.load('images/bomb.bmp')
#self.image.fill(YELLOW)
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.x = x
self.y = y
def update(self):
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
class Grenade(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.mines
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
#self.image = pg.Surface((TILESIZE, TILESIZE))
self.image = pg.image.load('images/grenade.bmp')
#self.image.fill(YELLOW)
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.x = x
self.y = y
def update(self):
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE