Instantly share code, notes, and snippets.
Created
October 15, 2024 14:46
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save horstjens/ac6020565aa0b1cf2aa9b1530f977d7f to your computer and use it in GitHub Desktop.
cookie_clicker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
W,H = 1280, 720 | |
FPS = 60 | |
price = {"cursor": 10, | |
"cursor_upgrade": 20, | |
} | |
price_factor = {"cursor": 1.4, | |
"cursor_upgrade": 1.4, | |
} | |
quantity = {"cursor": 0, | |
} | |
cursor_click_interval = 5.0 | |
cursor_how_many_clicks = 1 | |
# pygame setup | |
pygame.init() | |
screen = pygame.display.set_mode((W, H)) | |
background = pygame.surface.Surface((W,H)) | |
background.fill("#0000DD") | |
pygame.draw.line(background, "#FFFFFF", (W*0.5, 0), (W*0.5, H), 10) | |
clock = pygame.time.Clock() | |
running = True | |
#dt = 0 | |
big_image = pygame.image.load("cookie.png") | |
small_image = pygame.transform.scale_by(big_image, 0.5) | |
cursor_image = pygame.image.load("cursor.png") | |
cursor_image_small = pygame.transform.scale_by(cursor_image, 0.08) | |
gold_image = pygame.image.load("cookie_gold.png") | |
gold_image = pygame.transform.scale_by(gold_image, 0.05) | |
#background.blit(gold_image, (50,50)) | |
background.blit(small_image, (200, 230)) # cookie | |
background.blit(cursor_image_small, (W*0.52, 70)) | |
keksmittex, keksmittey = 360, 390 | |
keksradius = 152 | |
pygame.draw.circle(background, "#FF0000", (keksmittex, keksmittey), keksradius, 1) | |
font1 = pygame.font.SysFont("System", 40) | |
font2 = pygame.font.SysFont("System", 20) | |
cookies = 0 | |
cookies_per_second = 0 | |
playtime = 0 | |
text = font1.render("Shop", True, "#FFFFFF") | |
background.blit(text, (W*.70, 30)) | |
time_since_last_cursor_click = 0 | |
show_gold = False | |
gold_appear = playtime + random.uniform(5,10) | |
while running: | |
dt = clock.tick(FPS) / 1000 | |
playtime += dt | |
if playtime > gold_appear and not show_gold: | |
show_gold = True | |
gold_location = (200 + random.randint(0,200), | |
230 + random.randint(0,200)) | |
if show_gold and gold_appear > playtime: | |
show_gold = False | |
time_since_last_cursor_click += dt | |
if time_since_last_cursor_click > cursor_click_interval: | |
time_since_last_cursor_click = 0 | |
cookies += quantity["cursor"] * cursor_how_many_clicks | |
screen.blit(background, (0, 0)) | |
text = font1.render(f"cookies", True, "#FFFFFF") | |
screen.blit(text, (W*0.2, 30)) | |
text = font1.render(f"{cookies}", True, "#FFFFFF") | |
r = text.get_rect() | |
screen.blit(text, (W*0.2 - r.width/2, 60)) | |
text2 = font2.render(f"cookies per second: {cookies/playtime:.2f}", True, "#DDDDDD") | |
screen.blit(text2, (W*0.2, 80)) | |
# gold blitten | |
if show_gold: | |
screen.blit(gold_image, gold_location) | |
if random.random() < 0.001: | |
gold_appear = playtime + random.uniform(5, 10) | |
# ---------- shop ------- | |
# ---- cursor buy button ------ | |
if cookies < price["cursor"]: | |
color = "#FF0000" # red | |
else: | |
color = "#00FF00" # green | |
text = font1.render(f" {quantity['cursor']:>2} cursor: {price['cursor']}", True, "#FFFFFF") | |
screen.blit(text, (W*0.55, 80)) | |
buttontext = font1.render(" buy ", True, color) | |
r = buttontext.get_rect() | |
r.topleft = (W*0.8, 80) | |
pygame.draw.rect(screen, "#001066", r) | |
screen.blit(buttontext, r.topleft) | |
# ----- upgrade button ----- | |
if cookies < price["cursor_upgrade"]: | |
color = "#FF0000" # red | |
else: | |
color = "#00FF00" # green | |
buttontext = font1.render(" upgrade ", True, color) | |
r2 = buttontext.get_rect() | |
r2.topleft = (W * 0.88, 80) | |
pygame.draw.rect(screen, "#001066", r2) | |
screen.blit(buttontext, r2.topleft) | |
# help text | |
text = font2.render(f"each cursor clicks {cursor_how_many_clicks} time(s) every {cursor_click_interval} seconds on the cookie.", True, "#FFFF00") | |
screen.blit(text, (W*0.61, 120)) | |
pygame.display.flip() # once! | |
pygame.display.set_caption(f"fps: {clock.get_fps():.2f}") | |
# poll for events | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
if event.type == pygame.KEYDOWN: | |
#print(event.type) | |
if event.key == pygame.K_SPACE: | |
running = False | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
#print(event.type) | |
x,y = pygame.mouse.get_pos() | |
# button "cursor" clicked? | |
if r.collidepoint(x,y): | |
# cursor buyed ? | |
if cookies >= price["cursor"]: | |
cookies -= price["cursor"] | |
quantity["cursor"] += 1 | |
price["cursor"] = int(price["cursor"] * price_factor["cursor"]) | |
#Cursor() | |
# button "cursor upgrade" clicked? | |
elif r2.collidepoint(x,y): | |
if cookies >= price["cursor_upgrade"]: | |
cookies -= price["cursor_upgrade"] | |
cursor_how_many_clicks += 1 | |
price["cursor_upgrade"] = int(price["cursor_upgrade"] * price_factor["cursor_upgrade"]) | |
# cookie clicked ? | |
distanz = ((x-keksmittex)**2 + (y-keksmittey)**2)**0.5 | |
if distanz < keksradius: | |
print("+1") | |
cookies += 1 | |
else: | |
print("nix") | |
keys = pygame.key.get_pressed() | |
#if keys[pygame.K_b]: # duck | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment