0

I am trying to make my first game in PyGame. The objective is to click on a jar (Titled "jar.png") to gain more sand. There is also text to constantly tell you how much sand you have.

I have a problem though, I need a script that makes sure when you click on "jar.png" it increases the variable sand by 1. Also, if I do find a solution to this problem, I'm not sure if the text will display the variable correctly.

My current code looks like this:

import sys, pygame
from pygame.locals import *
pygame.init()
size = width, height = 800,500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Sand Maker (Created by - Not Really Working Lamp Productions:)")
CORNBLUE = (100,149,237)
WHITE = (255,255,255)
sand = 0
liams = 0
screen.fill (CORNBLUE)
jar = pygame.image.load("jar.png")
screen.blit(jar,(90,50))
liambutton = pygame.image.load("liam button.png")
screen.blit(liambutton,(450,50))
liamdesc = pygame.image.load("liam desc.png")
screen.blit(liamdesc,(350,50))
myfont = pygame.font.SysFont("monospace", 16)
scoretext = myfont.render("Number of sand = "+str(sand), 1, (255,255,255))
screen.blit(scoretext, (5, 10))
liamscoretext = myfont.render("Number of Liams = "+str(liams), 1, (255,255,255))
screen.blit(liamscoretext, (350, 10))
import time
while 1:
    for event in pygame.event.get():
        pygame.display.flip()
        if event.type == pygame.QUIT:sys.exit()
while 1:
    time.sleep(5)
    sand = sand + 1 * liams

So, could you please provide me with an edited piece of this code that contains the function I desire. Thank you.

  • 1
    "could you please provide me with an edited piece of this code that contains the function I desire" Nope! But try creating that function yourself and come back with any specific questions/issues you run into. – admdrew Oct 29 '13 at 19:07
  • I don't see any [pygame.Sprite](http://pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite) in code so what do you want to click ? – furas Oct 29 '13 at 19:15
  • 1
    By the way: You need to know `class` and `object programing` - list of [top 10 pygame tutorials](http://inventwithpython.com/blog/2010/09/01/the-top-10-pygame-tutorials/) – furas Oct 29 '13 at 19:26

2 Answers2

0

Your code is not a well structured pygame application. How a small pygame app code look like:

Structure

Application initialization

Here we put the pygame.init(), get our screen variable, load all the necessary images, and files, create any other variables that we may need in our program.

While loop

Here we put all the stuff that needs to be updated. We can divide code here into 3 parts:

  • Drawing - a background, and every other sprite that needs to be drawn
  • Updating - updates the clock, moves the sprites, checks for collision etc.
  • Handling input - sets variables that change the behavior of sprites in the update method.

These 3 parts have to be clearly separated!

Few other things : never call sleep(), since this essentially freezes up your game. Also, your second while loop is never executed.

Now back to the question: In the event loop, check for collision with the jar rect. The method to use is: Rect.collidepoint which returns True when the point is in the rectangle.

Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
  • So, where in my loop do I add "Rect.collidepoint" and what EXACTLY do I put? (EG: Is there brackets ivolved afterwards? Where do I insert the information to make the program recognize it as jar?) Finally, what do I replace "sleep()" with if I want the program to wait on something? – Not Really Working Lamp Produc Oct 30 '13 at 16:23
  • Ok, so it will not be Rect.collidepoint, but jar.rect.collidepoint(), You should put it in your event loop, after a MOUSE DOWN EVENT. What do you mean to make the program recognize if as jar? And lastly, it depends on what is happening on the screen while you wait. If you do not need to show anything, a while loop with event loop would suffice. If you want to show an animation, you would also need to include that. – Bartlomiej Lewandowski Oct 30 '13 at 19:10
0

I have two things two say for this:

First of all, as Bartlomiej Lewandowski stated, this is some unorganized pygame code. I am recommending this free e-book about pygame and general gameLoop organization from inventwithpython.

Second, I have also worked with this kind of problem with tkinter. First of all have a function that would increase a global variable that counts clicks:

clickCount = 0
multiplier = 1

def incrementClickCount():
    global clickCount
    global multiplier
    click += 1 * multiplier  

And then what you do, is test for the mouse cursors position when it is pressed, if it is on the coordinates you want. (You could add button animations for this):

def checkForCursorPressed(x,y):
    if pygame.mouse.get_pressed() == (1, 0, 0) and pygame.mouse.get_pos() == (x,y):
        incrementClickCount()

You can change the value of the multipler through different user actions and/or using simple checkpoints! I do not suggest using the multiplier as a parameter, since it decreases felxibilty within your app.

Do not forget to accept as answer if you found it useful! Cheers :)

By the way, this example tests for pixel-by-pixel accuracy on the mouse cursor click test. To check for areal clicks, check if the (x,y) lay inside your button/click area.

Community
  • 1
  • 1
Umut Berk Bilgic
  • 177
  • 1
  • 1
  • 8
  • So... I understand what you're saying, however since we are increasing the variable "sand", where in this code do we put it so that every click increases the variable "sand". Do I just swap "clickCount" with "sand"? – Not Really Working Lamp Produc Nov 01 '13 at 18:18
  • @NotReallyWorkingLampProduc Yeah, exactly! The clickCount is just an abstraction for ease of understanding for other further readers. Please check out the edit I made tht also adds the multiplier. I also have code for a click counter game in python: http://pastebin.com/u/GameNationRDF, just look at the ones that says "cookie clicker" those will help you understand the concept a little bit better :) – Umut Berk Bilgic Nov 01 '13 at 21:08
  • global multiplier is unnecessary, since you are not changing it in your method. There is no definition of the variable in your method, so python knows that you are referencing the global one. recommend reading this http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – Bartlomiej Lewandowski Nov 12 '13 at 16:52