Skip to content

Commit

Permalink
#16 Implementacija klase labela u klasu lajsna
Browse files Browse the repository at this point in the history
  • Loading branch information
Pancake2608 committed Mar 29, 2024
2 parents 56b6b69 + 8936734 commit 21afbf3
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 86 deletions.
33 changes: 33 additions & 0 deletions ClassLajsna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pygame
from Labela import *

class Lajsna:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.press = 2
self.prevpress = [-1, -1]
self.labela = Label(x, y)

def draw(self, screen):
pygame.draw.rect(screen, (0, 102, 255), pygame.Rect(self.x, self.y, self.width, self.height))
pygame.draw.line(screen, (200, 20, 0), (self.x + self.width*14/15 - self.width/30, self.y + self.height/10),
(self.x + self.width*14/15 - self.width/30 + self.width/15, self.y + self.height*8/10), 3)
pygame.draw.line(screen, (200, 20, 0), (self.x + self.width*14/15 - self.width/30, self.y + self.height/10 + self.height*8/10),
(self.x + self.width*14/15 - self.width/30 + self.width/15, self.y + self.height/10), 3)
self.labela.draw(screen)

def click(self, x, y):
if x > self.x + self.width*14/15 - self.width/30 and x < self.x + self.width*14/15 - self.width/30 + self.width/15:
self.press = 0
else:
self.press = 1
self.prevpress = [x, y]

def drag(self, x, y, prevx, prevy):
self.x += x
self.y += y
self.prevpress = [prevx, prevy]
self.labela.change_pos(self.x, self.y)
21 changes: 21 additions & 0 deletions ClassProzor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ClassLajsna import *
import pygame
class Prozor:
def __init__(self,x,y,width,height,labeltext):
self.x=x
self.y=y
self.width=width
self.height=height
self.label=labeltext
self.lajsna=Lajsna(self.x,self.y,self.width,30)
def draw(self,screen):
pygame.draw.rect(screen,(153, 153, 102),pygame.Rect(self.x,self.y,self.width,self.height))
self.lajsna.draw(screen)
def click(self,x,y):
if(x>=self.lajsna.x and x<=self.lajsna.x+self.lajsna.width) and (y>self.lajsna.y and y<self.lajsna.y+self.lajsna.height):
self.lajsna.click(x,y)
def drag(self,x,y,prevx,prevy):
self.x+=x
self.y+=y
self.lajsna.drag(x,y,prevx,prevy)

28 changes: 28 additions & 0 deletions ClassWindowMenager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from ClassProzor import *
from ClassLajsna import *
import pygame
class WindowMenager:
def __init__(self):
self.prozori=[]
def add_prozor(self):
self.prozori.append(Prozor(100,50,1500,750,'a'))
def remove_prozor(self,prozor):
self.prozori.remove(prozor)
def draw(self,screen):
for prozor in self.prozori:
prozor.draw(screen)
def check_click(self,x,y):
for i in range(len(self.prozori)-1,-1,-1):
print(i)
if(x>=self.prozori[i].x and x<=self.prozori[i].x+self.prozori[i].width) and (y>self.prozori[i].y and y<self.prozori[i].y+self.prozori[i].height):
self.prozori[i].click(x,y)
if self.prozori[i].lajsna.press==0:
self.remove_prozor(self.prozori[i])
break
def mouseup(self):
for prozor in self.prozori:
prozor.lajsna.press=2
def drag(self,x,y):
for prozor in self.prozori:
if prozor.lajsna.press==1:
prozor.drag(x-prozor.lajsna.prevpress[0],y-prozor.lajsna.prevpress[1],x,y)
33 changes: 0 additions & 33 deletions Klase.py

This file was deleted.

21 changes: 14 additions & 7 deletions Labela.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@
import string

class Label(pygame.sprite.Sprite):
def __init__(self, font, color=(0, 0, 0)):
def __init__(self, x, y):
super().__init__()
self.font = font
self.color = color
pygame.font.init()
self.font = pygame.font.SysFont('Arial',15)
self.x = x
self.y = y
self.color = (0,0,0)
self.generate_random_text()
self.image = self.font.render(self.text, True, self.color)
self.rect = self.image.get_rect()
self.rect = self.image.get_rect(topleft=(self.x, self.y))

def generate_random_text(self):
self.text = ''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(1, 10)))

def set_text(self, text):
self.text = text
self.image = self.font.render(self.text, True, self.color)
self.rect = self.image.get_rect()
self.rect = self.image.get_rect(topleft=(self.x, self.y))

def change_pos(self, pos):
self.rect.topleft = pos
def change_pos(self, x, y):
print(self.x, self.y)
self.x = x
self.y = y
self.rect.topleft = (self.x, self.y)

def draw(self, surface):
# print(self.x, self.y)
surface.blit(self.image, self.rect)
81 changes: 35 additions & 46 deletions Projekat.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
import pygame
from Klase import *
from Labela import Label
import random
import string
from ClassWindowMenager import *
from ClassProzor import *
from ClassLajsna import *
from Labela import *

background_colour = (0, 0, 0)
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('Pygame GUI')
screen.fill(background_colour)
pygame.display.flip()
import pygame
background_colour = (0, 0, 0)
screen = pygame.display.set_mode((1750, 850))
pygame.display.set_caption('GUI')
screen.fill(background_colour)
pygame.display.flip()
running = True
laj = []
laj.append(Lajsna(450, 200, 600, 30))

# Initialize font
pygame.font.init()
font = pygame.font.SysFont('Arial', 36) # Using Arial font, change if needed

# Create label with random text
label = Label(font)
label.change_pos((50, 50)) # Set position of the label

while running:
for event in pygame.event.get():
if (event.type == pygame.KEYDOWN and event.key == pygame.K_u) or event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
for lajsna in laj:
if (x >= lajsna.x and x <= lajsna.x + lajsna.width) and (y > lajsna.y and y < lajsna.y + lajsna.height):
lajsna.click(x, y)
if lajsna.press == 0:
laj.remove(lajsna)
if event.type == pygame.MOUSEBUTTONUP:
for lajsna in laj:
lajsna.press = 2
if event.type == pygame.MOUSEMOTION:
for lajsna in laj:
if lajsna.press == 1:
lajsna.drag(x - lajsna.prevpress[0], y - lajsna.prevpress[1], x, y)
prozori=[]
window_menager=WindowMenager()
while running:
x, y = pygame.mouse.get_pos()
screen.fill(background_colour)
for lajsna in laj:
lajsna.draw(screen)
pygame.draw.circle(screen, (254, 254, 254), [x, y], 7)

# Draw the label
label.draw(screen)

for event in pygame.event.get():
if (event.type == pygame.KEYDOWN and event.key == pygame.K_u) or event.type==pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
window_menager.add_prozor()
if event.type==pygame.MOUSEBUTTONDOWN:
window_menager.check_click(x,y)
# for prozor in prozori:
# if(x>=prozor.x and x<=prozor.x+prozor.width) and (y>prozor.y and y<prozor.y+prozor.height):
# prozor.click(x,y)
# if prozor.lajsna.press==0:
# prozori.remove(prozor)
#print(lajsna.press)
if event.type==pygame.MOUSEBUTTONUP:
window_menager.mouseup()
if event.type==pygame.MOUSEMOTION:
window_menager.drag(x,y)

#print(x,y)
screen.fill(background_colour)
window_menager.draw(screen)
pygame.draw.circle(screen,(254,254,254), [x,y], 7)
pygame.display.update()

# Update the display
Expand Down
Binary file added __pycache__/Klase.cpython-310.pyc
Binary file not shown.

0 comments on commit 21afbf3

Please sign in to comment.