def test_centery( self ): """Changing the centerx attribute moves the rect and does not change the rect's width """ r = Rect( 1, 2, 3, 4 ) new_centery = r.centery + 20 expected_top = r.top + 20 old_height = r.height r.centery = new_centery self.assertEqual( new_centery, r.centery ) self.assertEqual( expected_top, r.top ) self.assertEqual( old_height, r.height )
def test_centery(self): """Changing the centerx attribute moves the rect and does not change the rect's width """ r = Rect(1, 2, 3, 4) new_centery = r.centery + 20 expected_top = r.top + 20 old_height = r.height r.centery = new_centery self.assertEqual(new_centery, r.centery) self.assertEqual(expected_top, r.top) self.assertEqual(old_height, r.height)
def draw_text(text='sample text', color=(255, 255, 255), pos=(0, 0), surface=None, align_x='left', align_y='top'): if (surface == None): surface = api.screen text_rect = Rect(0, 0, *font.size(text)) if (align_x == 'left'): text_rect.left = pos[0] elif (align_x == 'center'): text_rect.centerx = pos[0] elif (align_x == 'right'): text_rect.right = pos[0] if (align_y == 'top'): text_rect.top = pos[1] elif (align_y == 'center'): text_rect.centery = pos[1] elif (align_y == 'bottom'): text_rect.bottom = pos[1] surface.blit(font.render(text, 1, color), text_rect)
def align_rect(outer: pygame.Rect, inner: pygame.Rect, coords, *, halign: Literal["left", "center", "right"] = "left", valign: Literal["top", "center", "bottom"] = "top", outer_halign: Literal["left", "center", "right"] = "left", outer_valign: Literal["top", "center", "bottom"] = "top"): """Calculate coordinates for an inner rectangle aligned to an outer rectangle""" x, y = coords if outer_halign == "left": pass elif outer_halign == "center": x = outer.centerx + x elif outer_halign == "right": x = outer.right + x if outer_valign == "top": pass elif outer_valign == "center": y = outer.centery + y elif outer_valign == "bottom": y = outer.bottom + y if halign == "left": inner.left = x elif halign == "center": inner.centerx = x elif halign == "right": inner.right = x if valign == "top": inner.top = y elif valign == "center": inner.centery = y elif valign == "bottom": inner.bottom = y return inner.topleft
def set_screensize(screensize): """ Set the physical screen size in pixels. Calculates the transformations to and from game space, using an aspect-preserving scale. """ global screenarea, screenplayarea global s_f_g_w, s_f_g_h, s_f_g_x, s_f_g_y global g_f_s_w, g_f_s_h, g_f_s_x, g_f_s_y # find screenarea rect # the rect in screen space that the game will take up # by doing an aspect preserve scale, and centering in the middle of the screen if screensize[0] / screensize[1] > GAME_AREA.width / GAME_AREA.height: # game uses full height of screen screenarea = Rect(0, 0, screensize[1] * GAME_AREA.width / GAME_AREA.height, screensize[1]) screenarea.centerx = screensize[0] / 2 else: # game uses full width of screen screenarea = Rect(0, 0, screensize[0], screensize[0] * GAME_AREA.height / GAME_AREA.width) screenarea.centery = screensize[1] / 2 # screen_from_game: s_f_g_w = screenarea.width / GAME_AREA.width s_f_g_h = screenarea.height / GAME_AREA.height s_f_g_x = screenarea.x - GAME_AREA.x * s_f_g_w s_f_g_y = screenarea.y - GAME_AREA.y * s_f_g_h # game_from_screen g_f_s_w = GAME_AREA.width / screenarea.width g_f_s_h = GAME_AREA.height / screenarea.height g_f_s_x = GAME_AREA.x - screenarea.x * g_f_s_w g_f_s_y = GAME_AREA.y - screenarea.y * g_f_s_h screenplayarea = screenrect_from_gamerect(GAME_PLAY_AREA)
def __init__(self, gui) -> None: """ Creates a button with attributes Note: If colour is none then the button is invisible on_click is a function that will exectute when the button has been pressed """ self.gui = gui # Title self.title_rect = Rect(120, self.gui.height // 8, self.gui.width // 2, self.gui.width // 2 * 244 / 925) self.title_rect.centerx = self.gui.width // 2 self.game_title = pygame.image.load('assets/A45.png').convert_alpha() self.game_title = pygame.transform.smoothscale( self.game_title, self.title_rect.size) # Start 2 Player Button two_player_rect = Rect(0, 0, 200, 50) two_player_rect.centerx = self.gui.width // 2 two_player_rect.centery = self.gui.height // 2 - 50 two_player_button = Button(two_player_rect, self.gui.start_game_two_player, (255, 255, 255), '2 Player') # Start AI Easy Button easy_rect = Rect(0, 0, 200, 50) easy_rect.centerx = self.gui.width // 2 easy_rect.centery = self.gui.height // 2 + 50 easy_button = Button(easy_rect, self.gui.start_game_easy, (255, 255, 255), 'Easy AI') # Start AI Hard Button hard_rect = Rect(0, 0, 200, 50) hard_rect.centerx = self.gui.width // 2 hard_rect.centery = self.gui.height // 2 + 150 hard_button = Button(hard_rect, self.gui.start_game_hard, (255, 255, 255), 'Hard AI') # Quit Button end_rect = Rect(0, 0, 200, 50) end_rect.centerx = self.gui.width // 2 end_rect.centery = self.gui.height // 2 + 250 end_game_button = Button(end_rect, self.gui.end_game, (255, 255, 255), 'Quit Game') self.buttons = [] self.buttons.append(two_player_button) self.buttons.append(easy_button) self.buttons.append(hard_button) self.buttons.append(end_game_button)