Python Forum
pygame error in my clicker game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame error in my clicker game
#3
After you get use to python. Learn Classes. 99% of everything in python is an object.
Here an example.
import pygame
pygame.init()

def create_button(font, text, text_color, color, rect):
    # Use the pygame Rect.
    rect = pygame.Rect(rect)
    surface = pygame.Surface(rect.size)
    surface.fill(color)

    center = rect.centerx - rect.x, rect.centery - rect.y
    text_image = font.render(text, 1, text_color)
    text_rect = text_image.get_rect(center=center)
    surface.blit(text_image, text_rect)

    return surface, rect

def main():
    pygame.display.set_caption("Button Example")
    display = pygame.display.set_mode((800, 600))
    clock = pygame.time.Clock()
    fps = 60

    font20 = pygame.font.Font(None, 20)
    font50 = pygame.font.Font(None, 50)

    # Button will be a tuple. (image, rect)
    button = create_button(font20, "MyButton", pygame.Color("grey50"), pygame.Color("grey70"), (50, 50, 150, 30))

    # Create your text. Then render it. This is a tuple. (text_image, position)
    clicky_click = font50.render("Clicky Click", 1, pygame.Color("white")), (400, 100)

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if button[1].collidepoint(event.pos):
                        print("You push my buttons")
            elif event.type == pygame.QUIT:
                running = False

        display.fill(pygame.Color('black')
        display.blit(button[0], button[1])
        display.blit(clicky_click[0], clicky_click[1])
        pygame.display.update()
        # Idle/Sleep
        clock.tick(fps)

main()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
pygame error in my clicker game - by CrazyMakes - Apr-17-2020, 01:58 AM
RE: pygame error in my clicker game - by vman44 - Apr-19-2020, 12:38 AM
RE: pygame error in my clicker game - by Windspar - Apr-19-2020, 03:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Isometric game pygame Tiled howardberger 1 654 Jan-31-2024, 10:01 PM
Last Post: deanhystad
  [PyGame] Pygame attribute error djwilson0495 3 4,004 Feb-18-2021, 03:34 PM
Last Post: michael1789
Big Grin Error installing Pygame-Zero dpa2007 1 3,204 Dec-26-2020, 03:50 PM
Last Post: MK_CodingSpace
  Error to install pygame skp 1 3,525 Apr-14-2020, 05:17 PM
Last Post: joe_momma
  installing pygame error pylab 1 4,295 Dec-31-2019, 05:44 PM
Last Post: pylab
  [PyGame] pygame image loading error BlueClaw 6 6,443 Dec-10-2019, 08:50 PM
Last Post: BlueClaw
  Problem with music - Pygame.error GaseBall 1 3,227 Nov-28-2019, 07:46 PM
Last Post: SheeppOSU
  Pygame to exe error Clunk_Head 0 3,006 Oct-19-2019, 01:34 PM
Last Post: Clunk_Head
  Basically a Python Tetris game [pygame] rather keyboard arrows can be controlled with lsepolis123 9 5,201 Sep-10-2019, 08:12 PM
Last Post: metulburr
  Pygame 2d game maximk301 1 2,899 Apr-08-2019, 11:22 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020