add mine collision

This commit is contained in:
eugenep 2021-04-10 13:48:55 +02:00
parent 63cc9c0ab2
commit dafa68f3fd
2 changed files with 28 additions and 11 deletions

View File

@ -1,5 +1,6 @@
import pygame as pg
import sys
import ctypes
from os import path
from grid import *
from settings import *
@ -25,6 +26,7 @@ class Game:
# initialize all variables and do all the setup for a new game
self.all_sprites = pg.sprite.Group()
self.walls = pg.sprite.Group()
self.mines = pg.sprite.Group()
for row, tiles in enumerate(self.map_data):
for col, tile in enumerate(tiles):
if tile == '2':
@ -63,7 +65,6 @@ class Game:
def draw(self):
self.screen.fill(BGCOLOR)
self.draw_grid()
self.draw_grid()
self.all_sprites.draw(self.screen)
pg.display.flip()

View File

@ -1,5 +1,6 @@
import pygame as pg
import enum
import ctypes
from settings import *
class Player(pg.sprite.Sprite):
@ -23,22 +24,34 @@ class Player(pg.sprite.Sprite):
if self.direction == Direction.Right.name:
if dx > 0:
if self.check_border(dx):
self.x += 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):
self.y += 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):
self.y += 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):
self.x += 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
@ -59,8 +72,11 @@ class Player(pg.sprite.Sprite):
return True
def collide_with_walls(self, dx=0, dy=0):
pass
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
@ -76,7 +92,7 @@ class Direction(enum.Enum):
class Mine(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
self.groups = game.all_sprites, game.mines
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
#self.image = pg.Surface((TILESIZE, TILESIZE))
@ -94,7 +110,7 @@ class Mine(pg.sprite.Sprite):
class Bomb(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
self.groups = game.all_sprites, game.mines
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
#self.image = pg.Surface((TILESIZE, TILESIZE))
@ -112,7 +128,7 @@ class Bomb(pg.sprite.Sprite):
class Grenade(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
self.groups = game.all_sprites, game.mines
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
#self.image = pg.Surface((TILESIZE, TILESIZE))