Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from avinashkranjan:master #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 1 commit into Uncodedtech:master from avinashkranjan:master
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions Pygame-Buttons/ExampleButtons.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-

import pygame
import os,sys


class Button(object):

def __init__(self, position, size):

# create 3 images
self._images = [
pygame.Surface(size),
pygame.Surface(size),
pygame.Surface(size),
]

# fill images with color - red, gree, blue
self._images[0].fill((255, 226, 39))
self._images[1].fill((235, 89, 110))
self._images[2].fill((77, 55, 93))

# get image size and position
self._rect = pygame.Rect(position, size)

# select first image
self._index = 0

def draw(self, screen):

# draw selected image
screen.blit(self._images[self._index], self._rect)

def event_handler(self, event):

# change selected color if rectange clicked
if event.type == pygame.MOUSEBUTTONDOWN: # is some button clicked
if event.button == 1: # is left button clicked
if self._rect.collidepoint(event.pos): # is mouse over button
self._index = (self._index+1) % 3 # change image

# --- main ---

pygame.init()
w, h = 800, 800
screen = pygame.display.set_mode((w, h))
GREEN = (0, 255, 0)
GRAY= (174, 150, 255)
font = pygame.font.Font('freesansbold.ttf', 22)

# =============================================================================
# Rendring texts23564
# =============================================================================

p1 = font.render("A PNG button , Click it ", True,(233, 248, 103))
p2= font.render("A color img buttons", True,(254, 32, 107))
p0 = font.render('Go Back', True,(15, 28, 2))
textRectp0 = p0.get_rect()
textRectp1 = p1.get_rect()
textRectp2 = p2.get_rect()
textRectp0.center = (405, 225)
textRectp1.center = (200, 225)
textRectp2.center=(350,420)

#backButton=main.backButton

module = sys.modules['__main__']
path, name = os.path.split(module.__file__)
path = os.path.join(path, 'retry_button.png')

img0 = pygame.image.load(path)
img0.convert()
rect0 = img0.get_rect()
rect0.x=350
rect0.y=200
pygame.draw.rect(img0, GREEN, rect0, 1)
act=False


# create buttons

button1 = Button((205, 435), (100, 100))
button2 = Button((310, 435), (100, 100))
button3 = Button((420, 435), (100, 100))

# mainloop


running=True
while running:
for event in pygame.event.get():
pos=pygame.mouse.get_pos()
if event.type == pygame.QUIT:
running = False
print("Job Done!!")
#pygame.quit()
pygame.quit()
sys.exit()

# --- buttons events ---

button1.event_handler(event)
button2.event_handler(event)
button3.event_handler(event)


if event.type==pygame.MOUSEBUTTONDOWN:
if rect0.collidepoint(event.pos):
# Toggle the active variable.
act = not act
else:
act = False
if act:
print("You Clicked PNG button")
print("Gone back")
running=False
import main


screen.fill(GRAY)
screen.blit(img0, rect0)
screen.blit(p0,textRectp0)
screen.blit(p1,textRectp1)
screen.blit(p2,textRectp2)


#---Buttons
button1.draw(screen)
button2.draw(screen)
button3.draw(screen)
pygame.display.update()




40 changes: 40 additions & 0 deletions Pygame-Buttons/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Buttons in Python <img width="35" src="https://img.icons8.com/fluent/48/000000/play-button-circled.png"/>
## Aim 🎯
- How to make buttons in python
- Why to use them
- To be able to design and animate buttons in python .
- Make buttons use in any GUI based applications . ( It will be preety handy )
- To Redirect user to any other module using buttons .

## Details 🛈
| Library | README | Install |
| ------ | ------ | ------ |
| Pygame | [Pygame/PyPi](https://pypi.org/project/pygame/) | `pip install pygame` |

## What is Included Inside ✨

1. Animating Button Using MOUSEOVER Porperty of pygame .
2. Adding Image Buttons.
> i. PNG images as buttons .
> ii.Simple Color images as buttons .
3. Assigning Loop Switching and Module Switching to Buttons .
4. Taking Input from USER and rendring it on a button .
5. Applying logic to leave/quit buttons .

## How to get Started 🤔
- clone the repo or [copy the source code](https://github.com/Lakhankumawat/Amazing-Python-Scripts/edit/Buttons-Pygame/Pygame-Buttons/) ( be sure you maintain files in same directory ) .
- Run main.py .
- Enter your name -> click play -> welcome screen -> come back -> click close .
- You will be headed to next module -> check out all buttons .
- Looks cool !! .
- Check out line of code and then start making changes , happy coding ! .

## How it may Look 👀


| <img width="200" src="https://user-images.githubusercontent.com/55774240/115188301-5c91d600-a102-11eb-8a14-9e60e414d6d6.png"/> | <img align="center" width="200" src="https://user-images.githubusercontent.com/55774240/115188308-5ef43000-a102-11eb-9eac-b0b96210ddaf.png"/> | <img align="right" width="200" src="https://user-images.githubusercontent.com/55774240/115188314-60bdf380-a102-11eb-8ded-866464fa4c26.png"/><hr> |
| ------ | ------ | ------ |



## Author : [LakhanKumawat](https://github.com/Lakhankumawat)
176 changes: 176 additions & 0 deletions Pygame-Buttons/main.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# -*- coding: utf-8 -*-


import pygame as pg

pg.init()

screen=pg.display.set_mode((500,500))
pg.display.set_caption('Buttons Pygame')


class button():
def __init__(self, color, x,y,width,height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text

def draw(self,screen,outline=None):
#Call this method to draw the button on the screen
if outline:
pg.draw.rect(screen, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)

pg.draw.rect(screen, self.color, (self.x,self.y,self.width,self.height),0)

if self.text != '':
font = pg.font.SysFont('comicsans', 60)
text = font.render(self.text, 1, (0,0,0))
screen.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))

def isOver(self, pos):
#Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True

return False


def redraw():
screen.fill((164,235,243))
greenButton.draw(screen,(0,0,0))
redButton.draw(screen,(0,0,0))
txt_surface1 = font.render(text1, True, color1)

# Resize the box if the text is too long.
width = max(200, txt_surface1.get_width()+10)
input_box1.w = width

# Blit the text.
screen.blit(txt_surface1, (input_box1.x+5, input_box1.y+10))

screen.blit(p1,textRectp1)

# Blit the input_box rect.
pg.draw.rect(screen,color1, input_box1,2)

pg.display.flip()

def draw_enter():
screen.fill((164,235,243))
welcomeButton.draw(screen,(0,0,0))
backButton.draw(screen,(0,0,0))

run=True
redButton=button((164,235,243),250,335,150,100,'Close')
greenButton=button((164,235,243),50,335,150,100,'Play')
welcomeButton=button((164,235,243),50,105,400,300,'Hi')

backButton=button((164,235,243),150,435,170,50,'Go Back')
game_is_going=True
enter_screen=False

input_box1 = pg.Rect(140, 170, 170, 50)
color_inactive = pg.Color((2, 0, 93))
color_active = pg.Color((33, 224, 239))
color1 = color_inactive

active1 = False

text1 = ''
font = pg.font.SysFont('comicsans', 60)
p1 = font.render('Enter Your Name ', True,(14, 43, 103))

textRectp1 = p1.get_rect()

textRectp1.center = (245, 115)


while(game_is_going):
while(run):
redraw()
pg.display.update()

for event in pg.event.get():
pos=pg.mouse.get_pos()

if event.type==pg.QUIT:
run=False
game_is_going=False
pg.quit()


if event.type == pg.KEYDOWN:
if not active1:
if event.key == pg.K_RETURN:
print(text1)

elif event.key == pg.K_BACKSPACE:
text1 = text1[:-1]
else:
text1 += event.unicode


if event.type==pg.MOUSEBUTTONDOWN:

if input_box1.collidepoint(event.pos):
# Toggle the active variable.
active1 = not active1
else:
active1 = False
color1 = color_active if active1 else color_inactive
if greenButton.isOver(pos):
print("Clicked Enter")
run=False
enter_screen=True
elif redButton.isOver(pos):
print("Clicked Close")
run=False
game_is_going=False
pg.quit()

if event.type ==pg.MOUSEMOTION:
if greenButton.isOver(pos):
greenButton.color=(158,222,11)
elif redButton.isOver(pos):
redButton.color=(232, 69, 69)
else:
greenButton.color=(164,235,243)
redButton.color=(164,235,243)


while(enter_screen):
draw_enter()
pg.display.update()

for event in pg.event.get():
pos=pg.mouse.get_pos()
welcomeButton.text="Hi "+str(text1)
if event.type==pg.QUIT:
enter_screen=False
game_is_going=False
pg.quit()


if event.type==pg.MOUSEBUTTONDOWN:
if backButton.isOver(pos):
print("Gone back")
run=True
enter_screen=False
if event.type ==pg.MOUSEMOTION:
if welcomeButton.isOver(pos):
welcomeButton.color=(158,222,11)
elif backButton.isOver(pos):
backButton.color=(232, 69, 69)
else:
welcomeButton.color=(164,235,243)
backButton.color=(164,235,243)


if __name__=="__main__":
import ExampleButtons


1 change: 1 addition & 0 deletions Pygame-Buttons/requirements.txt
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pygame==2.0.1
Binary file added Pygame-Buttons/retry_button.png
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]

AltStyle によって変換されたページ (->オリジナル) /