From cf2bf0ae517af350528d2b4f2b321262b2d2c294 Mon Sep 17 00:00:00 2001 From: PAlexanderFranklin Date: Wed, 17 May 2023 04:25:07 -0700 Subject: [PATCH] Refactor constants, draw blocks, fix push --- arctic-masher.py | 34 +++++++++++++++++++--------------- blocks.py | 11 +++++++++-- constants.py | 14 ++++++++++++++ player.py | 2 +- 4 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 constants.py diff --git a/arctic-masher.py b/arctic-masher.py index 71f7ec0..b82cb0f 100644 --- a/arctic-masher.py +++ b/arctic-masher.py @@ -2,24 +2,13 @@ import pygame import sys import random +from constants import * from player import * +from blocks import * pygame.init() clock = pygame.time.Clock() -bg_color = pygame.Color('cornsilk4') -red = pygame.Color('brown4') -blue = pygame.Color('cadetblue3') -green = pygame.Color('green3') -black = pygame.Color("black") - -tile = 34 -tileCountx = 50 -tileCounty = 30 - -screen_width = tile * tileCountx -screen_height = tile * tileCounty - screen = pygame.display.set_mode((screen_width,screen_height)) pygame.display.set_caption('Arctic Masher') @@ -81,6 +70,15 @@ players.append(Player( gameMap, )) +for i in range(300): + try: + spot = [random.randint(0, tileCountx - 1), random.randint(0, tileCounty - 1)] + if gameMap[spot[0]][spot[1]]: + raise Exception("spot taken!") + gameMap[spot[0]][spot[1]] = Block(spot[0], spot[1], gameMap) + except Exception as error: + print(error) + for player in players: for i in range(50): try: @@ -123,8 +121,14 @@ while True: # Rendering screen.fill(bg_color) - for player in players: - screen.blit(frame_0, ((tile*player.x)+2, (tile*player.y)+2)) + for column in gameMap: + for spot in column: + if not spot: + continue + elif isinstance(spot, Player): + screen.blit(frame_0, ((tile*spot.x)+2, (tile*spot.y)+2)) + elif isinstance(spot, Block): + pygame.draw.rect(screen, blue, spot.sprite) pygame.display.flip() clock.tick(60) \ No newline at end of file diff --git a/blocks.py b/blocks.py index 380841a..c1c2bbe 100644 --- a/blocks.py +++ b/blocks.py @@ -1,8 +1,13 @@ +import pygame + +from constants import * + class Block: def __init__(self, x, y, gameMap): self.x = x self.y = y self.gameMap = gameMap + self.sprite = pygame.Rect((x*tile) + 2, (y*tile) + 2, tile - 2, tile - 2) def move(self, x, y, caller=False): try: @@ -10,7 +15,9 @@ class Block: self.gameMap[self.x + x][self.y + y].move(x, y, caller=self) self.gameMap[self.x][self.y] = 0 self.x += x + self.sprite.x = (self.x*tile)+2 self.y += y + self.sprite.y = (self.y*tile)+2 self.gameMap[self.x][self.y] = self - except: - pass \ No newline at end of file + except Exception as error: + raise error \ No newline at end of file diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..655b8b7 --- /dev/null +++ b/constants.py @@ -0,0 +1,14 @@ +import pygame + +bg_color = pygame.Color('cornsilk4') +red = pygame.Color('brown4') +blue = pygame.Color('cadetblue3') +green = pygame.Color('green3') +black = pygame.Color("black") + +tile = 34 +tileCountx = 50 +tileCounty = 30 + +screen_width = tile * tileCountx +screen_height = tile * tileCounty \ No newline at end of file diff --git a/player.py b/player.py index 986c7e4..e1818ed 100644 --- a/player.py +++ b/player.py @@ -35,7 +35,7 @@ class Player: def move(self, x, y, caller=False): try: - if isinstance(caller, Player): + if caller: raise Exception("Cannot push other players.") if self.gameMap[self.x + x][self.y + y]: self.gameMap[self.x + x][self.y + y].move(x, y, caller=self)