Simple Strategy Game

 avatar
unknown
python
a year ago
7.1 kB
4
Indexable
#libarries and subroutines are imported
import pygame
import sys
import random
#colours and class constants are declared below
width = 1920
height = 1080
rows, columns = 8, 8
board_square_size = width // columns
c_black = (0, 0, 0)
c_white = (255, 255, 255)
c_grey = (200, 200, 200)
c_red = (255, 40, 0)
c_blue = (0, 50, 245)

#pygame is initialised
pygame.init
#name of window/application is set
pygame.display.set_caption('mental maze')
#the music mixer is initalised
pygame.mixer.init()

#game window
window = pygame.display.set_mode((width, height))
#music file is imported
music = pygame.mixer.sound('Across The Sea.wav')
#player and enemy character images are imported and their size set
hans_img = pygame.image.load('hans.png')
hans_img = pygame.transform.scale(player_img, (tile_size, tile_size))
mark_img = pygame.image.load('mark.png')
mark_img = pygame.transform.scale(enemy_img, (tile_size, tile_size))

#button class is created and attributes declared/set
class button:
    def __init__(self, x, y, width, height, text, colour, hover_colour, activated=None):
        self.rect = pygame.rect(x, y, width, height)
        self.text = text
        self.colour = colour
        self.hover_colour
        self.activated = activated
        self.hover = False
#button class methods are created below
    def draw(self):
        pygame.draw.rect(window, self.hover_colour if self.hover else self.colour, self.rect)
        text_surf = font.render(self.text, true, c_red)
        window.blit(text_surf, text_rect)

    def hovered(self, position):
        self.hover = self.rect.collidepoint(position)

    def when_clicked(self):
        if self.activated:
            self.activated()

#gameboard creation function is made to draw the gameboard when required it includes a feature that use the valid moves the player can make and highlights those specific tiles
def draw_board():
    valid_moves = get_valid_moves(player_pos)
    for row in range(rows):
        for column in range(columns):
            colour = c_white if (row + column) % 2 == 0 else c_grey
            rect = pygame.rect(column * tile_size, row * tile_size, tile_size, tile_size)
            pygame.draw.rect(window, colour, rect)
            if rect.collidepoint(pygame.mouse.get_position()):
                if (row, column) in valid_moves:
                    pygame.draw.rect(window, c_blue, rect, 3)
#the function to create the player character is defined
def draw_hans(position):
    window.blit(hans_img, (position[1] * tile_size, position[0] * tile_size))
#the function to create the enemy character is defined
def draw_mark(position):
    window.blit(mark_img, (position[1] * tile_size, position[0] * tile_size))
#the function that starts up the gameloop is defined
def start():
    global started, hans_position, mark_position, hans_moves, game_end
    started = True
    music.play(-1)
    hans_position = (0, 0)
    mark_position = (rows -1, columns - 1)
    hans_moves = 0
    game_end = False
    print("started")
#the function to quit the application is created
def exit():
    print("Exited")
    pygame.quit()
    sys.exit
#this is the function that navigates the enemy character towards the player no matter where they are
def move_enemy(mark_position, hans_position):
    x = hans_position[1] - mark_position[1]
    y = hans_position[0] - mark_position[0]

    if abs(x) > abs(y):
        new_position = (mark_position[0], mark_position[1] + 1 if x > 0 else mark_position[1] - 1)
    else:
        new_position = (mark_position[0] + 1 if y > 0 else mark_position[0] - 1, mark_position[1])

    new_position = (max(0, min(rows - 1, new_position[0])), max(0, min(columns - 1, new_position[1])))

    return new_position
#this function is created to detect if the player and enemy have landed on the same tile therefore ending the game
def collision():
    global hans_position, mark_position, game_end
    if hans_position == mark_position:
        game_end = True
#this function is created to calculate evry move that the player can make from the position they are currently in
def get_valid_moves(mark_position):
    valid_moves = []
    for move in [(0, -1), (-1, 0), (0,1), (1, 0)]:
        new_position = (mark_position[0] + move[0], mark_position[1] + move[1])
        if 0 <= new_position[0] < rows and 0 <= new_position[1] < columns:
            valid_moves.append(new_position)
    return valid_moves
#fonts are set and the buttons for the menu are created here
font = pygame.font.Font(None, 34)
start_button = Button(150, 100, 250, 75, "Start", c_grey, c_red, starts_game)
exit_button = Button(150, 100, 250, 75, "Quit", c_grey, c_red, exit_game)

#Main Execution Loop
#constants are set for the game to start running
started =  False
running = True
clock = pygame.time.Clock()
hans_position = (0, 0)
mark_position = (rows - 1, columns - 1)
player_moves = 0
game_over = False

while running:
    window.fill(c_white)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            print("Quit event")
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if not game_started:
                if start_button.rect.collidepoint(event.position):
                    start_button.on_click()
                elif exit_button.rect.collidepoint(event.position):
                    exit_button.on_click()
                elif game_started:
                    mouse_position = pygame.mouse.get_position()
                    if 0 <= mouse_position[0] < WIDTH and 0 <= mouse_position[1] < HEIGHT:
                        clicked_row, clicked_column = mouse_position[1] // tile_size, mouse_position[0] // tile_size

                        valid_moves = get_valid_moves(hans_position)
                        for move in valid_moves:
                            if (clicked_row, clicked_col) == move:
                                hans_position = move
                                hans_moves += 1
                                if player_moves % 2 == 0:
                                    mark_position = move_enemy(mark_position, hans_position)
                                    collision()
                                break

        if game_started:
            draw_board()
            draw_mark(mark_position)
            draw_hans(hans_position)

            if game_over:
	    # Display game over text or screen
                game_over_text = font.render("You've Been Sectioned!!!", True, c_red)
                screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - game_over_text.get_height() // 2))
            if not game_started:
                for button in [start_button, exit_button]:
                    button.check_hover(pygame.mouse.get_position())
                    button.draw()

    pygame.display.flip()
    clock.tick(60)

    
        

    
    
    
    

Editor is loading...
Leave a Comment