Posts: 15
Threads: 7
Joined: Jan 2020
Apr-17-2020, 01:58 AM
(This post was last modified: Apr-17-2020, 02:11 AM by CrazyMakes.)
# 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
Posts: 18
Threads: 6
Joined: Feb 2020
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.
Posts: 544
Threads: 15
Joined: Oct 2016
Apr-19-2020, 03:04 AM
(This post was last modified: Apr-19-2020, 03:05 AM by Windspar.)
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.
|