resizing a pygame screen

It's a theory I had, so far looking good.
I worked on finding a way to resize the pygame screen, and how the objects inside can scale with it.

Once again, there is probably some package shortcut that does this for you, but this is how I learn best.

Demo Video:







Code:

My theory was if I made every image creation, draw, whathave you, scale by the screen's size,  and then change that size, the objects drawn on would size properly with it.  Still working on it, so If I get it all proper, I'll drop it in an update

**NOTE** if you place too many run_screen where they don't belong <like outside an event handler in the while loop> You can stall out the computer.

I did fix it, so it's a freindly warning if you like to experiment like I do.
 
I didn't have to hard reset, but had to hold the CTRL^C and push CTRL ALT DEL a bit to get it to stop.

It's in progress, but if you're looking for a start like I was:

#!usr/bin/python3
# -*- coding: utf-8 -*-

import pygame as pg
from sys import exit
from time import sleep



pg.init()

class SkullGlobals(object):
    """ class container for globally used variables """
    def __init__(self):
        self.WIDTH = 800
        self.HEIGHT = 800
        self.CENTER_SCREEN_X = 0 + self.WIDTH//12
        self.CENTER_SCREEN_Y = 0 + self.HEIGHT//12
        self.CENTER_SCREEN_H = self.HEIGHT - self.HEIGHT//6
        self.CENTER_SCREEN_W = self.WIDTH - self.WIDTH//6
        self.background_color = (110, 100, 110)
        self.BLUE = (40, 60, 200)
        self.LBLUE = (100, 100, 255)
        self.BORDER = (self.WIDTH + self.HEIGHT)//200
        self.left_y = 0 + self.HEIGHT//100
        self.left_x = 0 + self.WIDTH//100
        self.right_y = self.HEIGHT - self.left_y
        self.right_x = self.WIDTH - self.left_x
        self.small_height = self.left_y * 5
        self.small_width = self.left_x * 10
        self.left_upper_y = 0 + (self.HEIGHT//10)
        self.left_upper_x = 0 + (self.WIDTH//10)
        self.left_upper_right_y = self.HEIGHT - self.BORDER
        self.right_lower_x = self.WIDTH - (self.WIDTH//10)
        self.right_lower_y = self.HEIGHT - (self.HEIGHT//10)
        self.CENTER = (self.WIDTH//2, self.HEIGHT//2)
        self.FULL_SIZE = (self.WIDTH//4 + self.HEIGHT//4)//2
        self.SMALL_SIZE = (self.WIDTH//50 + self.HEIGHT//50)//2
        self.RED = (230, 50, 50)
        self.GREEN = (50, 230, 50)
        self.D_WHITE = (200,200,200)
        self.base_color = 150
        self.menu_bar_color = (90, 100, 80)

    def change_scale(self, height, width):
        self.WIDTH = width
        self.HEIGHT = height



def draw_circle(globals, screen, on=False):
    """place a circle on screen """
    if not on:
        color = globals.BLUE
        position = (globals.WIDTH//2, globals.HEIGHT//2)
        size = globals.WIDTH//4
        pg.draw.circle(screen, color, position, size, 0)
    else:
        color = globals.GREEN
        position = (globals.WIDTH//2, globals.HEIGHT//2)
        size = globals.WIDTH//4
        pg.draw.circle(screen, color, position, size, 0)

def run_screen(globals):
    globals = globals
    screen = pg.display.set_mode((globals.WIDTH, globals.HEIGHT))
    pg.display.set_caption(('My Painter'))
    animation_timer = pg.time.Clock()
    pg.display.flip()
    events = pg.event.get()
    for event in events:
        if event.type == pg.QUIT:
            pg.quit()
            exit(0)
    return screen, animation_timer


def run_paint():
    globals = SkullGlobals()
    running = True
    screen, animation_timer = run_screen(globals)
    animation_timer.tick(60)
    #screen.fill(globals.background_color)
    small = False
    large = False
    while running:
        events = pg.event.get()

        for event in events:
            if event.type == pg.QUIT:
                self.running = False
                pg.quit()
                exit(0)
            if event.type == pg.MOUSEBUTTONDOWN:
                position = pg.mouse.get_pos()
                if position[0] > 300:
                    globals.change_scale(400, 400)
                    run_screen(globals)
                    small = True
                    large = False

                elif position[0] < 300:
                    globals.change_scale(800, 800)
                    run_screen(globals)
                    large = True
                    small = False

            if small:
                draw_circle(globals, screen, on=True)
            if large:
                draw_circle(globals, screen)
        pg.display.flip()

run_paint()





Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

playing with color in powershell python