arctic-masher/arctic-masher.py

147 lines
3.7 KiB
Python
Raw Normal View History

2023-05-17 06:29:36 +00:00
import pygame
import sys
import random
from constants import *
2023-05-17 11:04:10 +00:00
from player import *
from blocks import *
2023-05-17 11:52:05 +00:00
from enemies import *
2023-05-17 11:04:10 +00:00
2023-05-17 06:29:36 +00:00
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((screen_width,screen_height))
2023-05-17 07:58:04 +00:00
pygame.display.set_caption('Arctic Masher')
2023-05-17 06:29:36 +00:00
2023-05-17 11:04:10 +00:00
gameMap = []
for i in range(tileCountx):
column = []
for j in range(tileCounty):
column.append(False)
gameMap.append(column)
2023-05-17 07:05:33 +00:00
2023-05-17 06:29:36 +00:00
players = []
2023-05-17 11:04:10 +00:00
players.append(Player(
5,
5,
red,
[
(pygame.K_w, "n"),
(pygame.K_e, "ne"),
(pygame.K_d, "e"),
(pygame.K_c, "se"),
(pygame.K_x, "s"),
(pygame.K_z, "sw"),
(pygame.K_a, "w"),
(pygame.K_q, "nw"),
],
gameMap,
))
players.append(Player(
3,
3,
green,
[
(pygame.K_KP_8, "n"),
(pygame.K_KP_9, "ne"),
(pygame.K_KP_6, "e"),
(pygame.K_KP_3, "se"),
(pygame.K_KP_2, "s"),
(pygame.K_KP_1, "sw"),
(pygame.K_KP_4, "w"),
(pygame.K_KP_7, "nw"),
],
gameMap,
))
players.append(Player(
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"),
],
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)
2023-05-17 11:52:05 +00:00
for i in range(15):
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]] = Enemy(spot[0], spot[1], gameMap)
except Exception as error:
print(error)
2023-05-17 11:04:10 +00:00
for player in players:
for i in range(50):
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]] = player
player.x = spot[0]
player.y = spot[1]
break
except Exception as error:
print(error)
if i > 45:
pygame.quit()
sys.exit()
2023-05-17 06:29:36 +00:00
2023-05-17 09:01:42 +00:00
sprite_sheet_image = pygame.image.load('assets/penguin.png').convert_alpha()
2023-05-17 11:52:05 +00:00
def get_image(sheet, x, y, width, height, scale, colour):
2023-05-17 09:01:42 +00:00
image = pygame.Surface((width, height)).convert_alpha()
2023-05-17 11:52:05 +00:00
image.blit(sheet, (0, 0), (x, y, width, height))
2023-05-17 09:01:42 +00:00
image = pygame.transform.scale(image, (width * scale, height * scale))
image.set_colorkey(colour)
return image
2023-05-17 11:52:05 +00:00
frame_0 = get_image(sprite_sheet_image, 0, 0, 32, 32, (tile-2)/32, black)
frame_4 = get_image(sprite_sheet_image, 32, 0, 32, 32, (tile-2)/32, black)
2023-05-17 09:01:42 +00:00
2023-05-17 06:29:36 +00:00
game_font = pygame.font.Font("freesansbold.ttf",32)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
2023-05-17 07:05:33 +00:00
keys = pygame.key.get_pressed()
2023-05-17 09:06:36 +00:00
for player in players:
player.useKeys(keys)
2023-05-17 06:29:36 +00:00
# Rendering
screen.fill(bg_color)
for column in gameMap:
for spot in column:
if not spot:
continue
elif isinstance(spot, Block):
pygame.draw.rect(screen, blue, spot.sprite)
2023-05-17 11:52:05 +00:00
elif isinstance(spot, Enemy):
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))
2023-05-17 06:29:36 +00:00
pygame.display.flip()
clock.tick(60)