Untitled

mail@pastecode.io avatar
unknown
python
6 months ago
7.9 kB
10
Indexable
Never
#pygame map maker NEA
import pygame
import sys

pygame.init()

#game variables
button_count = 0
rows = 12
collums = 12
tile_size = 65
screen_height = 720
screen_width = 1280
lower_margin = tile_size * rows
last_collum_x = tile_size * collums
side_margin = screen_width - last_collum_x
SCREEN = pygame.display.set_mode((screen_width + side_margin, screen_height + lower_margin))
pygame.display.set_caption("Map Maker NEA")
tile_types = 13
current_tile = 0
eraser = pygame.image.load("eraser.png")
#backgrounds

try:
  BG = pygame.image.load("backgrounds/Background.png")
  grass_background = pygame.image.load("backgrounds/grass_background2.png")
except Exception as er:
  print("Couldn't load assets. /nPlease Relaod program.")
  print(er)
  pygame.quit()
  sys.exit()

#tile sets
img_list = []
for x in range(-1,tile_types):
  img = pygame.image.load(f"tiles/tile{x}.png")
  img = pygame.transform.scale(img, (tile_size, tile_size))
  img_list.append(img)


#game data
world_data = []
for row in range(rows):
    r = [-1] * collums
    world_data.append(r)

#draw grids
def draw_grid():
  #vertical lines
  for c in range(collums + 1):
    pygame.draw.line(SCREEN, "WHITE", (c * tile_size, 0),
                     (c * tile_size, screen_height))
  for c in range(rows + 1):
    pygame.draw.line(SCREEN, "WHITE", (0, c * tile_size),
                     (screen_width, c * tile_size))


#borders
def borders():
    pygame.draw.rect(SCREEN, "Grey", (last_collum_x, 0, side_margin, screen_height))
    pygame.draw.rect(SCREEN, "Grey", (0, 715, screen_width, lower_margin))



#function for drawing the world tiles
def draw_world():
    for y, row in enumerate(world_data):
            for x, tile in enumerate(row):
                if tile >= 0:
                    SCREEN.blit(img_list[tile], (x * tile_size, y * tile_size))

def get_font(size):
  return pygame.font.Font("font.ttf",
                          size)  # Returns Press-Start-2P in the desired size


class Button():
  def __init__(self, image, pos, text_input, font, base_color, hovering_color):
    self.clicked = False
    self.image = image
    self.x_pos = pos[0]
    self.y_pos = pos[1]
    self.font = font
    self.base_color, self.hovering_color = base_color, hovering_color
    self.text_input = text_input
    self.text = self.font.render(self.text_input, True, self.base_color)
    if self.image is None:
      self.image = self.text
    self.rect = self.image.get_rect(center=(self.x_pos, self.y_pos))
    self.text_rect = self.text.get_rect(center=(self.x_pos, self.y_pos))
    

    
  def update(self, screen):
    if self.image is not None:
      screen.blit(self.image, self.rect)
    screen.blit(self.text, self.text_rect)

  def checkForInput(self, position):
    if position[0] in range(self.rect.left,
                            self.rect.right) and position[1] in range(
                                self.rect.top, self.rect.bottom):
      return True
    return False
    

  def changeColor(self, position):
    if position[0] in range(self.rect.left,
                            self.rect.right) and position[1] in range(
                                self.rect.top, self.rect.bottom):
      self.text = self.font.render(self.text_input, True, self.hovering_color)
    else:
      self.text = self.font.render(self.text_input, True, self.base_color)


  def draw_tool(self, surface):
        action = False

        #get mouse position
        mouse_pos = pygame.mouse.get_pos()

        #check mouseover and clicked conditions
        if self.rect.collidepoint(mouse_pos):
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                action = True
                self.clicked = True

        if pygame.mouse.get_pressed()[0] == 0:
            self.clicked = False

        #draw button
        surface.blit(self.image, (self.rect.x, self.rect.y))

        return action



#create buttons
button_list = []
button_col = 0
button_row = 0

for i in range(len(img_list)):
  tile_button = Button(image=img_list[i],
                       pos=((screen_width + 50) - side_margin + (80 * button_col),
                            100 * button_row + 50),
                       text_input=str(i),
                       font=get_font(30),
                       base_color="white",
                       hovering_color="green")
  button_list.append(tile_button)
  button_col += 1
  if button_col == 3:
    button_row += 1
    button_col = 0




def play():  # Play screen
  global current_tile
  while True:
    PLAY_MOUSE_POS = pygame.mouse.get_pos()
    SCREEN.fill("black")
    SCREEN.blit(grass_background, (0, 0))
    draw_grid()
    borders()
    draw_world()

      
    pygame.draw.rect(SCREEN, "Grey", (last_collum_x, 0, side_margin, screen_height))
    pygame.draw.rect(SCREEN, "Grey", (0, lower_margin, screen_width, lower_margin))

    PLAY_BACK = Button(image=None,
                       pos=(1150, 80),
                       text_input="BACK",
                       font=get_font(45),
                       base_color="Black",
                       hovering_color="Green")

    PLAY_BACK.changeColor(PLAY_MOUSE_POS)
    PLAY_BACK.update(SCREEN)

    
      #add new tiles to screen
    pos = pygame.mouse.get_pos()
    x = (pos[0] // tile_size)
    y = (pos[1] // tile_size)
    
        #check coods are within play area
    if pos[0] < screen_width - side_margin:
            if pygame.mouse.get_pressed()[0] == 1:
                print(world_data)
                print(x, y)
                if world_data[y][x] != current_tile:
                    world_data[y][x] = current_tile
            if pygame.mouse.get_pressed()[2] == 1:
                world_data[y][x] = -1


    
    for button_count, i in enumerate(button_list):    
        if i.draw_tool(SCREEN):
            current_tile = button_count

    SCREEN.blit(eraser, (806, 27))

      #highlight current tile
    pygame.draw.rect(SCREEN, "Green", button_list[current_tile].rect, 3)

   
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
      if event.type == pygame.MOUSEBUTTONDOWN:
        if PLAY_BACK.checkForInput(PLAY_MOUSE_POS):
          main_menu()

    pygame.display.update()


def main_menu():
  while True:
    SCREEN.blit(BG, (0, 0))

    MENU_MOUSE_POS = pygame.mouse.get_pos()

    MENU_TEXT = get_font(100).render("MAIN MENU", True, "#b68f40")
    MENU_RECT = MENU_TEXT.get_rect(center=(640, 100))

    PLAY_BUTTON = Button(image=pygame.image.load("buttons/button2.png"),
                         pos=(640, 250),
                         text_input="PLAY",
                         font=get_font(75),
                         base_color="#d7fcd4",
                         hovering_color="Green")

    QUIT_BUTTON = Button(image=pygame.image.load("buttons/button2.png"),
                         pos=(640, 550),
                         text_input="QUIT",
                         font=get_font(75),
                         base_color="#d7fcd4",
                         hovering_color="Green")

    SCREEN.blit(MENU_TEXT, MENU_RECT)

    for button in [PLAY_BUTTON, QUIT_BUTTON]:
      button.changeColor(MENU_MOUSE_POS)
      button.update(SCREEN)

         
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
      if event.type == pygame.MOUSEBUTTONDOWN:
        if PLAY_BUTTON.checkForInput(MENU_MOUSE_POS):
          play()
        if QUIT_BUTTON.checkForInput(MENU_MOUSE_POS):
          pygame.quit()
          sys.exit()

    pygame.display.update()


main_menu()
Leave a Comment