Fix circular class checking, add scorebar

This commit is contained in:
PAlexanderFranklin 2023-05-17 22:30:15 -07:00
parent c80f919382
commit 238a4a26eb
5 changed files with 42 additions and 28 deletions

View File

@ -20,6 +20,8 @@ def main():
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('Arctic Masher')
bottomBar = pygame.Rect(0, screen_height - bottomBarHeight, screen_width, bottomBarHeight)
players["1"] = (Player(
"1",
5,
@ -54,23 +56,23 @@ def main():
(pygame.K_KP_5, "p"),
],
))
players["3"] = (Player(
"3",
5,
5,
red,
[
(pygame.K_y, "n"),
(pygame.K_u, "ne"),
(pygame.K_j, "e"),
(pygame.K_m, "se"),
(pygame.K_n, "s"),
(pygame.K_b, "sw"),
(pygame.K_g, "w"),
(pygame.K_t, "nw"),
(pygame.K_h, "p"),
],
))
# players["3"] = (Player(
# "3",
# 5,
# 5,
# red,
# [
# (pygame.K_y, "n"),
# (pygame.K_u, "ne"),
# (pygame.K_j, "e"),
# (pygame.K_m, "se"),
# (pygame.K_n, "s"),
# (pygame.K_b, "sw"),
# (pygame.K_g, "w"),
# (pygame.K_t, "nw"),
# (pygame.K_h, "p"),
# ],
# ))
mapgen.generateMap()
@ -104,6 +106,7 @@ def main():
# Rendering
screen.fill(bg_color)
pygame.draw.rect(screen, bar_color, bottomBar)
for column in gameMap:
for spot in column:
if not spot:
@ -114,6 +117,8 @@ def main():
screen.blit(frame_4, ((tile*spot.x)+2, (tile*spot.y)+2))
elif isinstance(spot, Player):
screen.blit(frame_0, ((tile*spot.x)+2, (tile*spot.y)+2))
scoreSurface = game_font.render(f'{spot.id}: {spot.kills}', False, black)
screen.blit(scoreSurface, (300*int(spot.id), screen_height - bottomBarHeight + 20))
pygame.display.flip()
clock.tick(60)

View File

@ -2,13 +2,14 @@ import pygame
import uuid
from globals import *
from enemies import Enemy
from enemies import *
class Block:
def __init__(self, id, x, y):
self.id = id
self.x = x
self.y = y
self.BLOCK = True
self.sprite = pygame.Rect((x*tile) + 2, (y*tile) + 2, tile - 2, tile - 2)
@ -27,7 +28,7 @@ class Block:
def pulled(self, x, y):
for i in range(-1,2):
for j in range(-1,2):
if isinstance(gameMap[self.x + i][self.y + j], Enemy):
if hasattr(gameMap[self.x + i][self.y + j], "ENEMY"):
raise Exception("Cannot pull a block that is touching an enemy")
gameMap[self.x][self.y] = False
self.x += x

View File

@ -3,8 +3,8 @@ import uuid
import random
from globals import *
from player import Player
from blocks import Block
from player import *
from blocks import *
def findClosestPlayer(x, y):
closestPlayerPosition = {"difference": (1,1), "distance": 9999}
@ -22,15 +22,17 @@ class Enemy:
self.x = x
self.y = y
self.AITime = random.randint(150, 200)
self.ENEMY = True
def die(self):
del enemies[self.id]
def pushed(self, x, y, caller, pusher):
if isinstance(pusher, Player) and isinstance(caller, Block):
if hasattr(pusher, "PLAYER") and hasattr(caller, "BLOCK"):
wallCrush = self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0
if wallCrush or isinstance(gameMap[self.x + x][self.y + y], Block):
if wallCrush or hasattr(gameMap[self.x + x][self.y + y], "BLOCK"):
pusher.kills += 1
self.die()
return
raise Exception("Not crushing enemy!")
@ -40,7 +42,7 @@ class Enemy:
if self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0:
raise Exception("Cannot move off edge!")
if gameMap[self.x + x][self.y + y]:
if isinstance(gameMap[self.x + x][self.y + y], Player):
if hasattr(gameMap[self.x + x][self.y + y], "PLAYER"):
gameMap[self.x + x][self.y + y].die()
else:
raise Exception("Path is blocked.")

View File

@ -1,6 +1,7 @@
import pygame
bg_color = pygame.Color('cornsilk4')
bar_color = pygame.Color('white')
red = pygame.Color('brown4')
blue = pygame.Color('cadetblue3')
green = pygame.Color('green3')
@ -10,9 +11,12 @@ tile = 34
tileCountx = 50
tileCounty = 25
screen_width = tile * tileCountx
screen_height = tile * tileCounty
bottomBarHeight = 100
screen_width = tile * tileCountx
screen_height = tile * tileCounty + bottomBarHeight
# Game Entity Arrays
gameMap = []
players = {}
enemies = {}

View File

@ -1,7 +1,7 @@
import uuid
from globals import *
from blocks import Block
from blocks import *
class Player:
def __init__(self, id, x, y, sprite, keys):
@ -10,6 +10,8 @@ class Player:
self.y = y
self.sprite = sprite
self.pull = False
self.PLAYER = True
self.kills = 0
commands = {
"n": lambda: self.move(0,-1),
@ -51,7 +53,7 @@ class Player:
try:
if self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0:
raise Exception("Cannot move off edge!")
pulling = self.pull and isinstance(gameMap[self.x - x][self.y - y], Block)
pulling = self.pull and hasattr(gameMap[self.x - x][self.y - y], "BLOCK")
if gameMap[self.x + x][self.y + y]:
if pulling:
raise Exception("Cannot push and pull at the same time")