cant get button location right

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1172 Views - Last Post: 04 August 2015 - 06:59 AM Rate Topic: -----

#1 sigint-ninja   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

cant get button location right

Posted 02 August 2015 - 01:24 PM

hi guys

everything is working the way it is supposed to...but i just cant get the mouse click working
in the location of the new game jpg...it works in the top left corner,but not where it says new game.
i have tried everything i can think of...i must be missing something completely.
im not expecting a fix...just more a...what you would do..as i feel its the best way to learn
thanks for reading...my code is below

time.sleep(3)
Button = pygame.image.load('startgame.png')
screen.blit(Button,(320,210))
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
           if Button.get_rect().collidepoint(pygame.mouse.get_pos()):
              button_pressed = pygame.image.load('button_pressed.png')
              screen.blit(button_pressed,(320,210))
              pygame.display.update()
              pygame.mixer.music.stop()
              file = 'ngsound.mp3'
              pygame.mixer.init()
              pygame.mixer.music.load(file)
              pygame.mixer.music.play()


Is This A Good Question/Topic? 0
  • +

Replies To: cant get button location right

#2 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,550
  • Joined: 12-December 12

Re: cant get button location right

Posted 02 August 2015 - 01:32 PM

There is no new game jpg mentioned in your code, now any text saying "new game". You should post more code and preferably a screenshot.
Was This Post Helpful? 0
  • +
  • -

#3 sigint-ninja   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: cant get button location right

Posted 02 August 2015 - 02:23 PM

the png files are in the same directory as the source file
i attached a screen shot...the new game button doesnt change red if i click on it...
only if i click in the top left corner of the screen
thanks for the reply

sorry my mistake...attached now

look heres the entire code:

###invader 5-7-2015
###main file
import pygame
import sys
import time
pygame.init()
#draws the title screen background
screen = pygame.display.set_mode((800, 600))
#changes caption on main window
pygame.display.set_caption('Invader')
#loads the title screen image
title_screen = pygame.image.load("titlescreen.jpg")
#draws the jpg to the surface
screen.blit (title_screen,(0,0))
#updates the surface
pygame.display.update()
#adds music to the title screen
file = 'titletheme.mp3'
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
#adds start game to title screen
time.sleep(3)
Button = pygame.image.load('startgame.png')
screen.blit(Button,(320,210))
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
           if Button.get_rect().collidepoint(pygame.mouse.get_pos()):
              button_pressed = pygame.image.load('button_pressed.png')
              screen.blit(button_pressed,(320,210))
              pygame.display.update()
              pygame.mixer.music.stop()
              file = 'ngsound.mp3'
              pygame.mixer.init()
              pygame.mixer.music.load(file)
              pygame.mixer.music.play()

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,550
  • Joined: 12-December 12

Re: cant get button location right

Posted 02 August 2015 - 02:59 PM

I probably can't help, although it seems to me that the mouse coordinates are being read relative to the button. This SO topic may shed some light:

Python surface real position coordinates of pygame.mouse.get_pos and Rect.collidepoint

This post has been edited by andrewsw: 02 August 2015 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 sigint-ninja   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: cant get button location right

Posted 02 August 2015 - 03:15 PM

thanks andrew will check it out
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,550
  • Joined: 12-December 12

Re: cant get button location right

Posted 02 August 2015 - 03:23 PM

To pursue the problem I would print() some of the coordinate values, which the accepted answer at that link demonstrates.
Was This Post Helpful? 0
  • +
  • -

#7 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 405
  • View blog
  • Posts: 1,312
  • Joined: 27-December 13

Re: cant get button location right

Posted 03 August 2015 - 03:08 AM

In line 25 you blit the Button to the screen, - but you never assign a rect to the Button. This you do in line 33, but you forget to state the position. By default it is (0, 0).

You can change line 33 to
if Button.get_rect(topleft=(320, 210)).collidepoint(pygame.mouse.get_pos()):



Better still is to assign the rect right after the definition of the surface (after the existing line 24, insert):

Button_rect = Button.get_rect(topleft=(320, 210))


Then your blit in existing line 25 becomes
screen.blit(Button, Button_rect)


and you line 33 changes to

if Button_rect.collidepoint(...)

This post has been edited by DK3250: 03 August 2015 - 03:23 AM

Was This Post Helpful? 2
  • +
  • -

#8 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 405
  • View blog
  • Posts: 1,312
  • Joined: 27-December 13

Re: cant get button location right

Posted 03 August 2015 - 03:22 AM

More generel info on surfaces and rects:
In pygame you have to distinguish between a surface and the associated rect.
The surface is 'responsible' for size and graphics. A surface is created by <surface> = pygame.Surface(..) or <surface> = pygame.image.load(..) or similar.
The rect is 'responsible' for position, collision etc.
A rect is created by the <surface>.get_rect() command.
later you can assign the position with <rect>.topleft(pos)
You can use other than topleft; center, midbottom, ...
When making the rect you can combine the position in the command: rect = <surface>.get_rect(topleft=(pos))

This post has been edited by DK3250: 03 August 2015 - 03:25 AM

Was This Post Helpful? 1
  • +
  • -

#9 sigint-ninja   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: cant get button location right

Posted 03 August 2015 - 04:42 AM

Thanks a million DK3250 for your help!!!

i changed 33 as you said to:

if Button.get_rect(topleft=(320, 210)).collidepoint(pygame.mouse.get_pos()):

i had tried

if Button.get_rect(320, 210).collidepoint(pygame.mouse.get_pos()):

but it didnt work...didnt know about top left...

again,thanks...time to make the UFO move around...should be fun!!!

This post has been edited by sigint-ninja: 03 August 2015 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

#10 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 405
  • View blog
  • Posts: 1,312
  • Joined: 27-December 13

Re: cant get button location right

Posted 03 August 2015 - 05:05 AM

I'm glad to help - I clearly remember when I started with Python/Pygame two years ago, many frustrations and failures until I figured it out (still, some challenges persists).

A small advice for your UFO game: You need an UFO_surface and an UFO_rect. When moved around, the position should be updated on the UFO_rect like: UFO_rect.topleft = (new_x, new_y).

As mentioned earlier you can use other rect attributes than topleft. This are the possibilities:
topleft, bottomleft, topright, bottomright, midtop, midleft, midbottom, midright, center.

For other purposes than (full) position you can also use:
x,y, top, left, bottom, right, centerx, centery, size, width, height, w,h

Happy coding.
Was This Post Helpful? 0
  • +
  • -

#11 sigint-ninja   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: cant get button location right

Posted 03 August 2015 - 05:51 AM

thanks DK,

last question...

is it better to have a few files for a game...or multiple...does it matter?
i have a title_screen.py and a main_game.py should i just keep them in one file?
i tried importing main_game.py using import but it doesnt work.
tried a couple of options in google that just produced errors.
any suggestions.

and...how difficult will it be to have the UFO tilt slightly with direction from left to right...so it doesnt just
look like a static cardboard cut out moving around the screen.

i know im jumping the gun...as my python skills are in their infancy...but its just so much fun!!!

thanks again...

ps"have you made any full games? do you work in the industry? or are you just a hobbyist?"

This post has been edited by sigint-ninja: 03 August 2015 - 05:54 AM

Was This Post Helpful? 0
  • +
  • -

#12 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 405
  • View blog
  • Posts: 1,312
  • Joined: 27-December 13

Re: cant get button location right

Posted 03 August 2015 - 07:43 AM

The reason for splitting a program into separate files is normally either reuse (I have a fixed end-scene in by games and use a separeate file for this) or overview (say one file for gameplay, one for music, one for menus, etc.)

As a beginner, I recommend to keep things together - If you file grow too big you can always separate it.

The import function, however, should work. Please show the code you have tried and the failure report; I'm sure we can find the malfunction.

To tilt the UFO you can have two (or more) pictures that you switch upon depending of the game situation. This is simple, flexible and straightforward.

You can also rotate the sprite by software - it is included in pygame, but rather difficult to use. Lookup pygame.transform.rotate() and also lookup 'pygame rotatecenter'; here you will find a very important small function for rotations (very difficult to understand, but it works).

I have made many games, but I don't invent new games, I only make my own versions of games I have seen on the net. Then I migth add a variation or two, but basically I copy games that I like.
I also do a bit of 'AI' - like I have build versions of Kalaha and 4-in-a-row (man against computer) that are very difficult to beat. My 'AI' for Kalaha has never been beaten.
Finally I solve problems on Euler and Rosalind and similar.

I am totally autodidact, started programming only 2 years ago as a hobby. Professionally, I'm a chemical engineer and don't do any programming in my job. In my free time I work as volunteer in educating/training children (age 12-17) in Python (and HTML, Javascript, Raspberry Pi, ..).
Was This Post Helpful? 1
  • +
  • -

#13 sigint-ninja   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: cant get button location right

Posted 03 August 2015 - 08:04 AM

wow!!! you definitely are a genius!!!
i work in IT (tech level) so have very little time to contribute to programming but i spend about 3 hours a night.

sure...i will keep it all under one file for now...also will check out what you recommended!!!

thanks again...good luck!!!
Was This Post Helpful? 0
  • +
  • -

#14 sigint-ninja   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: cant get button location right

Posted 03 August 2015 - 08:47 AM

Hey DK did you find any particular resources most useful or did you just read pygame tutorials on the pygame site?
Was This Post Helpful? 0
  • +
  • -

#15 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 405
  • View blog
  • Posts: 1,312
  • Joined: 27-December 13

Re: cant get button location right

Posted 03 August 2015 - 09:00 AM

I found a little introduction in 'Hello World, Computer Programming for Kids and other Beginners' by Warren and Carter Sander. From there, I've used the pygame documentation and asked here on DIC !!!

I have one more book, 'Beginning Game Development with Python and Pygame', it is not a bad book, but I don't find that it adds much.

What it really takes is: Programming, programming, programming. Try, get frustrated, try again, get frustrated again - But don't give up !!

Your friends here on DIC are glad to help - specifically if the code so far is provided.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2