pygame, making buttons, and drawing to screen

Ok, so first step to making the skull out of circles like I wanted to, was hell, I can make this a drawing program to just add the circles to screen i want with mouse clicks,  from there alter, manipulate it.

The progress so far:









Make it take button clicks... done...
Make it draw to screen.... done....
alter the choices with more then one button .... done
Much more to do, but it's a start, and hey maybe someone's looking up how to do this, so here's the code:
Question I'm asking myself is... Shouldn't all these global variables I'm making be in a container?  A class maybe, so they aren't just laying all over all... ugly, alterable, maybe getting clashed with another variable.  EH, I'll see what I can find, but it's easy to see this way for now.

Update: Put as much as I could into those handy python containers.
Runs the same, seems to be good.   Re-pasting code to update.
I need to make sure everything scales to screen size.  Will make a new post when I get this all doing what I want.
Found a bad error, fixing it... Then really, not updating until I get good working model.


#!usr/bin/python3
# -*- coding: utf-8 -*-
import pygame as pg

pg.init()

class SkullGlobals(object):
"""Class container for globally used variables """
    def __init__(self):
        self.WIDTH = 800
        self.HEIGHT = 800
        self.background_color = (110, 100, 110)
        self.BLUE = (40, 60, 200)
        self.LBLUE = (100, 100, 255)
        self.left_y = 0 + self.HEIGHT//100
        self.left_x = 0 + self.WIDTH//100
        self.right_y = self.HEIGHT - (self.left_y * 5)
        self.right_x = self.WIDTH - (self.left_x * 10)
        self.small_height = self.left_y * 5
        self.small_width = self.left_x * 10
        self.CENTER = (self.WIDTH//2, self.HEIGHT//2)
        self.MEDIUM_SIZE = (self.WIDTH//4 + self.HEIGHT//4)//2
        self.RED = (230, 50, 50)
        self.GREEN = (50, 230, 50)
        self.base_color = 150
## can not do this --> self.screen = pg.display.set_mode((globals.WIDTH, globals.HEIGHT))

globals = SkullGlobals()
screen = pg.display.set_mode((globals.WIDTH, globals.HEIGHT))
pg.display.set_caption(('Skull'))
screen.fill(globals.background_color)
animation_timer = pg.time.Clock()
pg.display.flip()


class ButtonXY(object):
""" class for a button object to draw to screen, and get x y coordinates of button """
    def __init__(self, my_screen, pos_x, pos_y, rect_width, rect_height, R, G, B, name):
        self.my_screen = my_screen
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.width = rect_width
        self.height = rect_height
        self.R = R
        self.G = G
        self.B = B
        self.name = name

    def place_rect(self):
        """ pygame.draw.rect(screen, color, (x, y, width, height), width of outline) """
        color = (self.R, self.G, self.B)
        size = (self.pos_x, self.pos_y, self.width, self.height)
        pg.draw.rect(self.my_screen, color, size, 5)

    def get_rect_place(self):
        """ return the needed dimensions for button click check """
        end_x_pos = self.pos_x + self.width
        end_y_pos = self.pos_y + self.height
        rect_list_dims = [self.pos_x, self.pos_y, end_x_pos, end_y_pos]
        return rect_list_dims



class CircleXY(object):
""" Class for drawing circles onto screen """
    def __init__(self,my_screen, pos_x, pos_y, radius, R, G, B, speed, thickness):
        self.my_screen = my_screen
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.radius = radius
        self.R = R
        self.G = G
        self.B = B
        self.x_change = speed
        self.y_change = speed
        self.thickness = thickness

    def draw_circle(self):
        """ place a circle at mouse position """
        color = (self.R, self.G, self.B)
        position = (self.pos_x, self.pos_y)
        pg.draw.circle(self.my_screen, color, position, self.radius, self.thickness)


class DrawIt(object):
""" class for defining and running the pygame screen creation/interaction"""
    def __init__(self):

        self.first_button = ButtonXY(screen, globals.left_x, globals.left_y, globals.small_width, globals.small_height, globals.base_color - 100, globals.base_color, globals.base_color - 100, "button1")

        self.second_button = ButtonXY(screen, globals.right_x, globals.right_y, globals.small_width, globals.small_height, globals.base_color, globals.base_color - 100, globals.base_color -100, "button2")

        self.rect_list = [self.first_button, self.second_button]

        self.ON = CircleXY(screen, globals.WIDTH//2, globals.HEIGHT//2, globals.MEDIUM_SIZE,  50, 230, 50, 1, 0)

        self.OFF = CircleXY(screen, globals.WIDTH//2, globals.HEIGHT//2, globals.MEDIUM_SIZE, 200, 50, 50, 1, 0)

        self.object_list = [self.ON, self.OFF]

        self.running = True

    def run_draw(self):
""" Running the pygame screen """
        while self.running:
            animation_timer.tick(45)
            screen.fill(globals.background_color)
            mousedown = False
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    self.running = False
                if event.type == pg.MOUSEBUTTONDOWN:
                    mousedown = True
          
           for rects in self.rect_list:
                rects.place_rect()
                #print("placing rect =", rects.name) loops prints, delete when done
                if mousedown == True:
                    position = pg.mouse.get_pos()
                    # cbd = Current Button Dimensions
                    # list returned = [x position, y postion, x postion + width, y position + height]
                    cbd = rects.get_rect_place()
                    if cbd[0] < position[0] < cbd[2]:
                        if cbd[1] < position[1] < cbd[3]:
                            #print("IN BUTTON", rects.name)
                            if rects.name == "ON":
                                #self,my_screen, pos_x, pos_y, radius, R, G, B, speed, thickness
                                on = True
                                off = False
                            if rects.name == "OFF":
                                #print(" IN BUTTON", rects.name)
                                off = True
                                on = False

                        else:
                            pass

                if on:
                    item = self.object_list[0]
                    item.draw_circle()

                if off:
                    item = self.object_list[1]
                    item.draw_circle()
                #print(" object_list length ==========", len(self.object_list))
                pg.display.update()
drawing = DrawIt()
drawing.run_draw()




Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame