GenericAI_Sweeper/sprites.py
2021-04-11 12:02:52 +02:00

261 lines
9.7 KiB
Python

import pygame as pg
import enum
import ctypes
import ast
from settings import *
from maze 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
self.maze = Maze()
self.moves = ''
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 not self.collide_with_walls(dx):
if self.collide_with_mines(dx):
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
print("Mine Ahead!")
self.x += dx
else:
self.x += dx
if self.direction == Direction.Up.name:
if dy < 0:
if self.check_border(0, dy):
if not self.collide_with_walls(0, dy):
if self.collide_with_mines(0, dy):
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
print("Mine Ahead!")
self.y += dy
else:
self.y += dy
if self.direction == Direction.Down.name:
if dy > 0:
if self.check_border(0, dy):
if not self.collide_with_walls(0, dy):
if self.collide_with_mines(0, dy):
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
print("Mine Ahead!")
self.y += dy
else:
self.y += dy
if self.direction == Direction.Left.name:
if dx < 0:
if self.check_border(dx):
if not self.collide_with_walls(dx):
if self.collide_with_mines(dx):
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
print("Mine Ahead!")
self.x += dx
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 collide_with_walls(self, dx=0, dy=0):
for wall in self.game.walls:
if wall.x == self.x + dx and wall.y == self.y + dy:
return True
return False
"""
right = 1
left = -1
if self.self.moves[n+1] != 'R':
if self.moves[n+1] == 'U':
module.insert(n, 'Turn Up')
if self.moves[n+1] == 'L':
module.insert(n, 'Turn Left')
if self.moves[n+1] == 'D':
module.insert(n, 'Turn Down')
"""
def parse_maze_moves(self):
self.moves = [char for char in self.maze.moves]
if self.moves[0] != 'R':
if self.moves[0] == 'D':
self.moves.insert(0, 'Turn Down')
if self.moves[0] == 'U':
self.moves.insert(0, 'Turn Up')
if self.moves[0] == 'L':
self.moves.insert(0, 'Turn Left')
for n, i in enumerate(self.moves):
if i == 'R':
self.moves[n] = 'Right'
if n != len(self.moves)-1:
if i != self.moves[n+1]:
if self.moves[n+1] == 'D':
self.moves.insert(n+1, 'Turn Down')
if self.moves[n+1] == 'U':
self.moves.insert(n+1, 'Turn Up')
if self.moves[n+1] == 'L':
self.moves.insert(n+1, 'Turn Left')
else:
self.move(dx=-1, direction='Right')
if i == 'L':
self.moves[n] = 'Left'
if n != len(self.moves)-1:
if i != self.moves[n+1]:
if self.moves[n+1] == 'D':
self.moves.insert(n+1, 'Turn Down')
if self.moves[n+1] == 'U':
self.moves.insert(n+1, 'Turn Up')
if self.moves[n+1] == 'R':
self.moves.insert(n+1, 'Turn Right')
if i == 'D':
self.moves[n] = 'Down'
if n != len(self.moves)-1:
if i != self.moves[n+1]:
if self.moves[n+1] == 'R':
self.moves.insert(n+1, 'Turn Right')
if self.moves[n+1] == 'U':
self.moves.insert(n+1, 'Turn Up')
if self.moves[n+1] == 'L':
self.moves.insert(n+1, 'Turn Left')
if i == 'U':
self.moves[n] = 'Up'
if n != len(self.moves)-1:
if i != self.moves[n+1]:
if self.moves[n+1] == 'D':
self.moves.insert(n+1, 'Turn Down')
if self.moves[n+1] == 'R':
self.moves.insert(n+1, 'Turn Right')
if self.moves[n+1] == 'L':
self.moves.insert(n+1, 'Turn Left')
print(self.moves)
def update(self):
#sleep(0.25)
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
class Wall(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.walls
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
#self.image = pg.Surface((TILESIZE, TILESIZE))
self.image = pg.image.load('images/wall.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