2021-03-14 13:42:33 +01:00
|
|
|
import pygame as pg
|
|
|
|
import sys
|
2021-04-10 13:48:55 +02:00
|
|
|
import ctypes
|
2021-04-10 12:44:54 +02:00
|
|
|
from os import path
|
|
|
|
from grid import *
|
2021-03-14 13:42:33 +01:00
|
|
|
from settings import *
|
|
|
|
from sprites import *
|
|
|
|
|
|
|
|
class Game:
|
|
|
|
def __init__(self):
|
|
|
|
pg.init()
|
|
|
|
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
pg.display.set_caption(TITLE)
|
|
|
|
self.clock = pg.time.Clock()
|
|
|
|
pg.key.set_repeat(500, 100)
|
|
|
|
self.load_data()
|
|
|
|
|
|
|
|
def load_data(self):
|
2021-04-10 12:44:54 +02:00
|
|
|
game_folder = path.dirname(__file__)
|
|
|
|
self.map_data = []
|
|
|
|
with open(path.join(game_folder, 'map.txt'), 'rt') as f:
|
|
|
|
for line in f:
|
|
|
|
self.map_data.append(line)
|
2021-03-14 13:42:33 +01:00
|
|
|
|
|
|
|
def new(self):
|
|
|
|
# initialize all variables and do all the setup for a new game
|
|
|
|
self.all_sprites = pg.sprite.Group()
|
|
|
|
self.walls = pg.sprite.Group()
|
2021-04-10 13:48:55 +02:00
|
|
|
self.mines = pg.sprite.Group()
|
2021-04-10 12:44:54 +02:00
|
|
|
for row, tiles in enumerate(self.map_data):
|
|
|
|
for col, tile in enumerate(tiles):
|
|
|
|
if tile == '2':
|
|
|
|
Mine(self, col, row)
|
|
|
|
if tile == '3':
|
|
|
|
Bomb(self, col, row)
|
|
|
|
if tile == '4':
|
|
|
|
Grenade(self, col, row)
|
|
|
|
if tile == 'A':
|
|
|
|
self.player = Player(self, col, row)
|
2021-03-14 13:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
# game loop - set self.playing = False to end the game
|
|
|
|
self.playing = True
|
|
|
|
while self.playing:
|
|
|
|
self.dt = self.clock.tick(FPS) / 1000
|
|
|
|
self.events()
|
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
|
|
|
|
def quit(self):
|
|
|
|
pg.quit()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
# update portion of the game loop
|
|
|
|
self.all_sprites.update()
|
|
|
|
|
|
|
|
def draw_grid(self):
|
|
|
|
for x in range(0, WIDTH, TILESIZE):
|
|
|
|
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
|
|
|
|
for y in range(0, HEIGHT, TILESIZE):
|
|
|
|
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
|
|
|
|
|
|
|
|
def draw(self):
|
|
|
|
self.screen.fill(BGCOLOR)
|
|
|
|
self.draw_grid()
|
|
|
|
self.all_sprites.draw(self.screen)
|
|
|
|
pg.display.flip()
|
|
|
|
|
|
|
|
def events(self):
|
|
|
|
# catch all events here
|
|
|
|
for event in pg.event.get():
|
|
|
|
if event.type == pg.QUIT:
|
|
|
|
self.quit()
|
|
|
|
if event.type == pg.KEYDOWN:
|
|
|
|
if event.key == pg.K_ESCAPE:
|
|
|
|
self.quit()
|
|
|
|
if event.key == pg.K_LEFT:
|
2021-04-08 16:29:37 +02:00
|
|
|
self.player.move(dx=-1, direction='Left')
|
2021-03-14 13:42:33 +01:00
|
|
|
if event.key == pg.K_RIGHT:
|
2021-04-08 16:29:37 +02:00
|
|
|
self.player.move(dx=1, direction='Right')
|
2021-03-14 13:42:33 +01:00
|
|
|
if event.key == pg.K_UP:
|
2021-04-08 16:29:37 +02:00
|
|
|
self.player.move(dy=-1, direction='Up')
|
2021-03-14 13:42:33 +01:00
|
|
|
if event.key == pg.K_DOWN:
|
2021-04-08 16:29:37 +02:00
|
|
|
self.player.move(dy=1, direction='Down')
|
2021-03-14 13:42:33 +01:00
|
|
|
|
|
|
|
def show_start_screen(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def show_go_screen(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# create the game object
|
|
|
|
g = Game()
|
|
|
|
g.show_start_screen()
|
|
|
|
while True:
|
|
|
|
g.new()
|
|
|
|
g.run()
|
|
|
|
g.show_go_screen()
|