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
#1
# i am trying to follow pep 8 here

# importing stuff

import pygame
import time

# initializing pygame

pygame.init()

# defining variables

autog = 0
coins = 0
display_width = 800
display_height = 600
white = (255, 255, 255)
black = (0, 0, 0)
grey = (128, 128, 128)
light_grey = (224, 224, 224)

# creating display and caption

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("clicky clicks")

# defining functions


def DrawText(text, Textcolor, Rectcolor, x, y, fsize):
    font = pygame.font.Font('freesansbold.ttf', fsize)
    text = font.render(text, True, Textcolor, Rectcolor)
    textRect = text.get_rect()
    textRect.center = (x, y)
    gameDisplay.blit(text, textRect)


def rectangle(display, color, x, y, w, h):
    pygame.draw.rect(display, color, (x, y, w, h))


def main_loop():
    global autog
    tc = 0
    mong = 1
    cost = 50
    cost2 = 50
    global coins
    game_running = True
    while game_running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_running = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                tc += 1
                print(tc)
                mopos = pygame.mouse.get_pos()
                if mopos >= (350, 0):
                    if mopos <= (450, 0):
                        print("button clicked 1")
                        coins += mong

                if mopos <= (800, 0):
                    if mopos >= (600, 0):
                        print("button clicked 2")
                        if coins >= cost:
                            coins = coins - cost
                            cost = cost * 2
                            mong += 3

                if mopos >= (50, 0):
                    if mopos <= (245, 0):
                        print("button clicked 3")
                        if coins >= cost2:
                            coins = coins - cost2
                            cost2 = cost2 * 2
                            autog += 1
                            while True:
                                time.sleep(0.5)
                                coins = coins + autog

        # drawing stuff

        gameDisplay.fill(white)
        DrawText("Clicky Clicks", black, white, 400, 100, 50)
        DrawText("you have " + str(coins) + " coins", black, white, 100, 50, 20)
        DrawText("upgrade 50*2", black, light_grey, 700, 300, 20)
        DrawText("buy auto miner 50*2", black, light_grey, 150, 370, 20)
        rectangle(gameDisplay, light_grey, 50, 400, 200, 300)
        rectangle(gameDisplay, black, 350, 250, 100, 100)
        rectangle(gameDisplay, light_grey, 600, 317, 200, 300)
        pygame.display.update()

# ending the program


main_loop()
pygame.quit()
quit()
So I have 2 problems 1 is that when I made my buttons they had a problem that when I clicked above or below the buttons they still activated. my second problem was with this code while True:
time.sleep(0.5)
coins = coins + autog this code is in button 3 and it crashes the program when I click the button

I just realized i posted this in the wrong thread is there a way to change which thread this is on
~~ UwU
Reply
#2
I don't know about the 2nd issue you had,
but the following might help you w/ the 1st issue:

button1("GO!", intro_button_width1, intro_button_height, 100, 50, green, bright_green, game_loop)
button1("Quit", intro_button_width2, intro_button_height, 100, 50, red, bright_red, quit)
mouse = pygame.mouse.get_pos()

if (intro_button_width1 + 100 > mouse[0] > intro_button_width1
and intro_button_height + 50 > mouse[1] > intro_button_height):
pygame.draw.rect(gameDisplay, bright_green, (intro_button_width1, intro_button_height, 100, 50))
else:
pygame.draw.rect(gameDisplay, green, (intro_button_width1, intro_button_height, 100, 50))

smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects("GO!", smallText)
textRect.center = ((intro_button_width1 + (100 / 2)), (intro_button_height + (50 / 2)))
gameDisplay.blit(textSurf, textRect)

if (intro_button_width2 + 100 > mouse[0] > intro_button_width2
and intro_button_height + 50 > mouse[1] > intro_button_height):
pygame.draw.rect(gameDisplay, bright_red, (intro_button_width2, intro_button_height, 100, 50))
else:
pygame.draw.rect(gameDisplay, red, (intro_button_width2, intro_button_height, 100, 50))

**** button1 is defined as:
def button1(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))


Hopefully, this helps.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Isometric game pygame Tiled howardberger 1 601 Jan-31-2024, 10:01 PM
Last Post: deanhystad
  [PyGame] Pygame attribute error djwilson0495 3 3,940 Feb-18-2021, 03:34 PM
Last Post: michael1789
Big Grin Error installing Pygame-Zero dpa2007 1 3,175 Dec-26-2020, 03:50 PM
Last Post: MK_CodingSpace
  Error to install pygame skp 1 3,486 Apr-14-2020, 05:17 PM
Last Post: joe_momma
  installing pygame error pylab 1 4,264 Dec-31-2019, 05:44 PM
Last Post: pylab
  [PyGame] pygame image loading error BlueClaw 6 6,400 Dec-10-2019, 08:50 PM
Last Post: BlueClaw
  Problem with music - Pygame.error GaseBall 1 3,197 Nov-28-2019, 07:46 PM
Last Post: SheeppOSU
  Pygame to exe error Clunk_Head 0 2,971 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,106 Sep-10-2019, 08:12 PM
Last Post: metulburr
  Pygame 2d game maximk301 1 2,866 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