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


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 774 Jan-31-2024, 10:01 PM
Last Post: deanhystad
  [PyGame] Pygame attribute error djwilson0495 3 4,052 Feb-18-2021, 03:34 PM
Last Post: michael1789
Big Grin Error installing Pygame-Zero dpa2007 1 3,244 Dec-26-2020, 03:50 PM
Last Post: MK_CodingSpace
  Error to install pygame skp 1 3,590 Apr-14-2020, 05:17 PM
Last Post: joe_momma
  installing pygame error pylab 1 4,322 Dec-31-2019, 05:44 PM
Last Post: pylab
  [PyGame] pygame image loading error BlueClaw 6 6,509 Dec-10-2019, 08:50 PM
Last Post: BlueClaw
  Problem with music - Pygame.error GaseBall 1 3,264 Nov-28-2019, 07:46 PM
Last Post: SheeppOSU
  Pygame to exe error Clunk_Head 0 3,034 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,283 Sep-10-2019, 08:12 PM
Last Post: metulburr
  Pygame 2d game maximk301 1 2,921 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