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

View File

@ -1,5 +1,6 @@
import pygame as pg import pygame as pg
import enum import enum
import ctypes
from settings import * from settings import *
class Player(pg.sprite.Sprite): class Player(pg.sprite.Sprite):
@ -23,21 +24,33 @@ class Player(pg.sprite.Sprite):
if self.direction == Direction.Right.name: if self.direction == Direction.Right.name:
if dx > 0: if dx > 0:
if self.check_border(dx): if self.check_border(dx):
if self.collide_with_mines(dx):
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
else:
self.x += dx self.x += dx
if self.direction == Direction.Up.name: if self.direction == Direction.Up.name:
if dy < 0: if dy < 0:
if self.check_border(0, dy): 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 self.y += dy
if self.direction == Direction.Down.name: if self.direction == Direction.Down.name:
if dy > 0: if dy > 0:
if self.check_border(0, dy): 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 self.y += dy
if self.direction == Direction.Left.name: if self.direction == Direction.Left.name:
if dx < 0: if dx < 0:
if self.check_border(dx): if self.check_border(dx):
if self.collide_with_mines(dx):
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
else:
self.x += dx self.x += dx
elif direction != self.direction: elif direction != self.direction:
@ -59,8 +72,11 @@ class Player(pg.sprite.Sprite):
return True return True
def collide_with_walls(self, dx=0, dy=0): def collide_with_mines(self, dx=0, dy=0):
pass 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): def update(self):
self.rect.x = self.x * TILESIZE self.rect.x = self.x * TILESIZE
@ -76,7 +92,7 @@ class Direction(enum.Enum):
class Mine(pg.sprite.Sprite): class Mine(pg.sprite.Sprite):
def __init__(self, game, x, y): 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) 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))
@ -94,7 +110,7 @@ class Mine(pg.sprite.Sprite):
class Bomb(pg.sprite.Sprite): class Bomb(pg.sprite.Sprite):
def __init__(self, game, x, y): 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) 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))
@ -112,7 +128,7 @@ class Bomb(pg.sprite.Sprite):
class Grenade(pg.sprite.Sprite): class Grenade(pg.sprite.Sprite):
def __init__(self, game, x, y): 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) 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))