Refactor constants, draw blocks, fix push

This commit is contained in:
PAlexanderFranklin 2023-05-17 04:25:07 -07:00
parent b1d51f3b2c
commit cf2bf0ae51
4 changed files with 43 additions and 18 deletions

View File

@ -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)

View File

@ -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
except Exception as error:
raise error

14
constants.py Normal file
View File

@ -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

View File

@ -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)