The "code" you found on the Internet is not so good. All you need to do is a button. Put this next to the beginning of your code:
def Buttonify(Picture, coords, surface): image = pygame.image.load(Picture) imagerect = image.get_rect() imagerect.topright = coords surface.blit(image,imagerect) return (image,imagerect)
Put the following into your game loop. Also somewhere in your game loop:
Image = Buttonify('YOUR_PICTURE.png',THE_COORDS_OF_THE_BUTTON'S_TOP_RIGHT_CORNER, THE_NAME_OF_THE_SURFACE)
Also put this in your game loop, wherever you do for event in pygame.event.get
if event.type == MOUSEBUTTONDOWN and event.button == 1: mouse = pygame.mouse.getpos if Image[1].collidrect(mouse): #code if button is pressed goes here
So buttonify loads the image that will be on the button. This image must be a .jpg file or any other PICTURE file in the same directory as the code. A picture is his name. The name must have .jpg or something else after it, and the name must be in quotation marks. The coords parameter in Buttonify is the top right coordinate on your screen or window that opens from pygame. A surface is a thing:
blahblahblah = pygame.surface.set_mode((WindowSize)) /|\ | Surface Name
So the function does something called an “image”, which is the surface of the piggy, it puts a rectangle around it called “imagerect” (to set it in place for the second parameter when blitting), and then it sets its location and brings him on the second and last last line.
The next bit of code makes the “image” a tuple of both the “image” and the “imagerect”.
The last code has if event.type == MOUSEBUTTONDOWN and event.button == 1:
which basically means that the left mouse button is pressed. This code MUST be in for event in pygame.event.get
. The next line makes the mouse a tuple of mouse position. The last line checks if the mouse has collided with Image [1], which, as we know, is "imagerect". The code follows this.
Tell me if I need to explain further.