arctic-masher/arctic-masher.py

123 lines
3.2 KiB
Python
Raw Normal View History

2023-05-17 06:29:36 +00:00
import pygame
import sys
import random
pygame.init()
clock = pygame.time.Clock()
bg_color = pygame.Color('cornsilk4')
red = pygame.Color('brown4')
blue = pygame.Color('cadetblue3')
green = pygame.Color('green3')
2023-05-17 09:01:42 +00:00
black = pygame.Color("black")
2023-05-17 06:29:36 +00:00
tile = 34
screen_width = tile * 50
screen_height = tile * 30
2023-05-17 06:29:36 +00:00
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
class Player:
2023-05-17 07:05:33 +00:00
def __init__(self, x, y, color, keys):
2023-05-17 06:29:36 +00:00
self.x = x
self.y = y
self.color = color
2023-05-17 09:06:36 +00:00
commands = {
2023-05-17 07:58:04 +00:00
"n": lambda: self.move(0,-1),
"ne": lambda: self.move(1,-1),
"e": lambda: self.move(1,0),
"se": lambda: self.move(1,1),
"s": lambda: self.move(0,1),
"sw": lambda: self.move(-1,1),
"w": lambda: self.move(-1,0),
"nw": lambda: self.move(-1,-1),
}
2023-05-17 09:06:36 +00:00
self.commands = []
for key in keys:
self.commands.append([key[0], commands[key[1]], 0, 0])
2023-05-17 06:29:36 +00:00
self.sprite = pygame.Rect((tile*x)+2,(tile*y)+2,tile-2,tile-2)
2023-05-17 09:06:36 +00:00
def useKeys(self, keys):
try:
for command in self.commands:
if keys[command[0]]:
if command[2] == 0 or (command[2] > 10 and command[3] > 2):
command[1]()
command[3] = 0
command[2] += 1
command[3] += 1
else:
command[2] = 0
command[3] = 0
2023-05-17 09:06:36 +00:00
except:
pass
2023-05-17 06:29:36 +00:00
2023-05-17 07:05:33 +00:00
def move(self, x, y):
self.x += x
self.y += y
2023-05-17 06:29:36 +00:00
players = []
players.append(Player(5, 5, red, [
2023-05-17 09:06:36 +00:00
(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"),
]))
players.append(Player(3, 3, green, [
2023-05-17 09:06:36 +00:00
(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"),
]))
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"),
]))
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()
def get_image(sheet, width, height, scale, colour):
image = pygame.Surface((width, height)).convert_alpha()
image.blit(sheet, (0, 0), (0, 0, width, height))
image = pygame.transform.scale(image, (width * scale, height * scale))
image.set_colorkey(colour)
return image
frame_0 = get_image(sprite_sheet_image, 32, 32, (tile-2)/32, black)
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 player in players:
2023-05-17 09:01:42 +00:00
screen.blit(frame_0, ((tile*player.x)+2, (tile*player.y)+2))
2023-05-17 06:29:36 +00:00
pygame.display.flip()
clock.tick(60)