import pygame
from random import randint
pygame.init()
# Define base resolution
BASE_RESOLUTION = (1920, 1080)
resolution_for_testing = ((800, 600), (1024, 720))
current_resolution = resolution_for_testing[0]
#current_resolution = resolution_for_testing[1]
screen = pygame.display.set_mode(current_resolution)
screen_color = '#222222'
# Setup framerate
clock = pygame.time.Clock()
fps = 60
# Set the window title with the current resolution
pygame.display.set_caption(f'Current resolution: {current_resolution[0]} x {current_resolution[1]}')
scale_x = current_resolution[0] / BASE_RESOLUTION[0]
scale_y = current_resolution[1] / BASE_RESOLUTION[1]
# Define a Position class to store x and y coordinates
class Position:
def __init__(self, x=0, y=0, named=''):
self._x = x
self._y = y
self.named = named
class Player(pygame.sprite.Sprite):
def __init__(self, position, color, z_index=0):
pygame.sprite.Sprite.__init__(self)
base_size = 50
self.z_index = z_index
self.image = pygame.Surface((base_size * scale_x, base_size * scale_y))
self.image.fill(color)
self.rect = self.image.get_rect()
match position.named:
case 'R1C1-Left-Top':
self.rect.x = 0
self.rect.y = 0
case 'R1C1-Center-Center':
self.rect.x = (current_resolution[0] / 4) - (self.rect.width / 2)
self.rect.y = (current_resolution[1] / 4) - (self.rect.height / 2)
case 'R1C2-Right-Top':
self.rect.x = current_resolution[0] - self.rect.width
self.rect.y = 0
case 'R1C2-Right-Center':
self.rect.x = current_resolution[0] - self.rect.width
self.rect.y = (current_resolution[1] / 4) - (self.rect.height / 2)
case 'R1C2-Right-Bottom':
self.rect.x = current_resolution[0] - self.rect.width
self.rect.y = (current_resolution[1] / 2) - self.rect.height
case 'R1C2-Center-Bottom':
self.rect.x = current_resolution[0] - (current_resolution[0] / 4) - (self.rect.width /2)
self.rect.y = (current_resolution[1] / 2) - self.rect.height
case 'R1C2-Center-Center':
self.rect.x = current_resolution[0] - (current_resolution[0] / 4) - (self.rect.width / 2)
self.rect.y = (current_resolution[1] / 4) - (self.rect.height / 2)
case 'R2C1-R2C2-Center-Center':
self.rect.x = (current_resolution[0] / 2) - ((current_resolution[0] / 4) / 2) - (self.rect.width /2)
self.rect.y = current_resolution[1] - ((current_resolution[1] / 4) * 1.5) - (self.rect.height /2)
case 'Center-Center':
self.rect.x = (current_resolution[0] / 2) - (self.rect.width / 2)
self.rect.y = (current_resolution[1] / 2) - (self.rect.height / 2)
case 'Center-Center-Left':
self.rect.x = (current_resolution[0] / 2) - self.rect.width
self.rect.y = (current_resolution[1] / 2) - (self.rect.height / 2)
case 'Center-Center-Right':
self.rect.x = (current_resolution[0] / 2)
self.rect.y = (current_resolution[1] / 2) - (self.rect.height / 2)
case _:
# Set position proportionally to the screen resolution
self.rect.x = int(position._x * scale_x)
self.rect.y = int(position._y * scale_y)
self.title = f'Square: {color} {self.rect.x} x {self.rect.y}'
def check_hover(self, mouse_pos):
return self.rect.collidepoint(mouse_pos)
def draw_tooltip(self, screen, mouse_pos):
font = pygame.font.SysFont(None, 24)
text_surface = font.render(self.title, True, pygame.Color('white'))
text_rect = text_surface.get_rect()
if mouse_pos[0] > 3 * current_resolution[0] / 4:
text_rect.x = mouse_pos[0] - text_rect.width - 10
else:
text_rect.x = mouse_pos[0] + 10
if mouse_pos[1] > current_resolution[1] - (text_rect.height * 2):
text_rect.y = mouse_pos[1] - text_rect.height - 10
else:
text_rect.y = mouse_pos[1] + 10
screen.blit(text_surface, text_rect)
def draw_grid_lines():
# Draw the red, vertical line in the center of the screen (top to bottom)
center_x = current_resolution[0] / 2
pygame.draw.line(screen, 'red', (center_x, 0), (center_x, current_resolution[1]), 1)
# Draw the red, horizontal line in the center of the screen (left to right)
center_y = current_resolution[1] / 2
pygame.draw.line(screen, 'red', (0, center_y), (current_resolution[0], center_y), 1)
# Draw the red, vertical line in the third quarter of the screen (top to bottom)
center_x = current_resolution[0] / 4
center_y = current_resolution[1] / 2
pygame.draw.line(screen, 'red', (center_x, center_y), (center_x, current_resolution[1]), 1)
# Draw the red, horizontal line in the third quarter of the screen (left to right)
center_x = current_resolution[0] / 2
center_y = current_resolution[1] - (current_resolution[1] / 4)
pygame.draw.line(screen, 'red', (0, center_y), (center_x, center_y), 1)
# Create player objects with the given position
players = []
players.append(Player(Position(x=1800, y=800), 'Cyan'))
players.append(Player(Position(x=randint(0, 1920), y=randint(0, 1080)), 'Blue'))
players.append(Player(Position(named='R1C1-Left-Top'), 'Yellow'))
players.append(Player(Position(named='R1C1-Center-Center'), 'Yellow'))
players.append(Player(Position(named='R1C2-Right-Top'), 'Green'))
players.append(Player(Position(named='R1C2-Right-Center'), 'Green'))
players.append(Player(Position(named='R1C2-Right-Bottom'), 'Green'))
players.append(Player(Position(named='R1C2-Center-Bottom'), 'Green'))
players.append(Player(Position(named='R1C2-Center-Center'), 'Green'))
players.append(Player(Position(named='R2C1-R2C2-Center-Center'), 'Red'))
players.append(Player(Position(named='Center-Center'), 'Orange', 1))
players.append(Player(Position(named='Center-Center-Right'), 'Purple'))
players.append(Player(Position(named='Center-Center-Left'), 'Brown'))
# Sorting player by z-index value
players = sorted(players, key=lambda p: p.z_index)
# Main loop
running = True
while running:
screen.fill(screen_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
draw_grid_lines()
mouse_pos = pygame.mouse.get_pos()
tooltip_drawn = False
for player in players:
screen.blit(player.image, player.rect)
if player.check_hover(mouse_pos) and not tooltip_drawn:
player.draw_tooltip(screen, mouse_pos)
tooltip_drawn = True
pygame.display.update()
clock.tick(fps)
pygame.quit()