First commit, create tile and player
This commit is contained in:
parent
12788a8373
commit
ced24eec33
BIN
__pycache__/settings.cpython-38.pyc
Normal file
BIN
__pycache__/settings.cpython-38.pyc
Normal file
Binary file not shown.
BIN
__pycache__/sprites.cpython-38.pyc
Normal file
BIN
__pycache__/sprites.cpython-38.pyc
Normal file
Binary file not shown.
92
main.py
Normal file
92
main.py
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import pygame as pg
|
||||||
|
import sys
|
||||||
|
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):
|
||||||
|
pass
|
||||||
|
|
||||||
|
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()
|
||||||
|
self.player = Player(self, 1, 1)
|
||||||
|
|
||||||
|
|
||||||
|
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_grid(self):
|
||||||
|
for x in range(0, WIDTH, TILESIZE):
|
||||||
|
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
|
||||||
|
for y in range(0, WIDTH, TILESIZE):
|
||||||
|
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
|
||||||
|
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
self.screen.fill(BGCOLOR)
|
||||||
|
self.draw_grid()
|
||||||
|
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:
|
||||||
|
self.player.move(dx=-1)
|
||||||
|
if event.key == pg.K_RIGHT:
|
||||||
|
self.player.move(dx=1)
|
||||||
|
if event.key == pg.K_UP:
|
||||||
|
self.player.move(dy=-1)
|
||||||
|
if event.key == pg.K_DOWN:
|
||||||
|
self.player.move(dy=1)
|
||||||
|
|
||||||
|
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()
|
19
settings.py
Normal file
19
settings.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# define some colors (R, G, B)
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
DARKGREY = (40, 40, 40)
|
||||||
|
LIGHTGREY = (100, 100, 100)
|
||||||
|
GREEN = (0, 255, 0)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
YELLOW = (255, 255, 0)
|
||||||
|
|
||||||
|
# game settings
|
||||||
|
WIDTH = 800 # 16 * 64 or 32 * 32 or 64 * 16
|
||||||
|
HEIGHT = 600 # 16 * 48 or 32 * 24 or 64 * 12
|
||||||
|
FPS = 60
|
||||||
|
TITLE = "Tilemap Demo"
|
||||||
|
BGCOLOR = DARKGREY
|
||||||
|
|
||||||
|
TILESIZE = 50
|
||||||
|
GRIDWIDTH = WIDTH / TILESIZE
|
||||||
|
GRIDHEIGHT = HEIGHT / TILESIZE
|
22
sprites.py
Normal file
22
sprites.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import pygame as pg
|
||||||
|
from settings import *
|
||||||
|
|
||||||
|
class Player(pg.sprite.Sprite):
|
||||||
|
def __init__(self, game, x, y):
|
||||||
|
self.groups = game.all_sprites
|
||||||
|
pg.sprite.Sprite.__init__(self, self.groups)
|
||||||
|
self.game = game
|
||||||
|
self.image = pg.Surface((TILESIZE, TILESIZE))
|
||||||
|
self.image.fill(YELLOW)
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
def move(self, dx=0, dy=0):
|
||||||
|
self.x += dx
|
||||||
|
self.y += dy
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.rect.x = self.x * TILESIZE
|
||||||
|
self.rect.y = self.y * TILESIZE
|
||||||
|
|
Loading…
Reference in New Issue
Block a user