Posts: 59
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

cant get mouse co-ordinates within function loop

Fri Dec 07, 2018 5:05 pm

Hey guys, pretty basic issue here I'm sure you can help with.

I'm writing a bit of python code that will display one of several graphical pages on pygame. I've done this a few times using a single page and it works really well. I now want to expand so I have several pages that can display different sensor values, graphs and so forth. If I use 'C' I would use a function loop for each of the pages and then jump between them for different pages using the touch screen mouse to press abutton on the screen and use the main body of the code as a menu page. 'C 'seems to be soooo much easier in this respect (not wanting to cause offence to you python boys)

I am having real issues trying to read mouse coordinates within a python function loop, in fact I've wasted a days holiday trying so is getting a bit frustrating, I can print text in alternating colours or print an incrementing counter on the screen so I know the loop is working, I can even exit the loop back to the main body of code using the counter but in no way can I read and display / act of the mouse values. Is there something I'm missing?

The mouse can be read, displayed and press buttons within the main loop.

My dubious bit of test code follows.

Code: Select all

import pygame
import time

from pygame.locals import*

pygame.init()       #init pygame
pygame.font.init()  #init pygame fonts

mouse = pygame.mouse.get_pos()      #define mouse posision
click = pygame.mouse.get_pressed()  #define mouse button

gameDisplay=pygame.display.set_mode ((795,445)) #set screen size
#gameDisplay=pygame.display.set_mode ((795,445),pygame.FULLSCREEN)
clock = pygame.time.Clock() #set screen update rate (can't make work)

blue = (0,0,255)
red = (255,0,0)
black = (0,0,0)
yellow = (255,255,0)

gameExit = False


def screen1():

    exit_page = False

    pygame.draw.rect (gameDisplay, black, [0,0,795,445])    #clear screen

    pygame.draw.rect (gameDisplay, yellow, [540,20,200,200],1)  #draw yellow rectangle

    while not exit_page:    #screen1 loop

        mouse = pygame.mouse.get_pos()  #difine local mouse posision
        click = pygame.mouse.get_pressed()  #define local button press

        myfont = pygame.font.SysFont(None,50)   #set font
        pygame.draw.rect (gameDisplay, black, [50,50,150,50])   #draw rect to erase last write
        textSurface = myfont.render (str(mouse),True, (yellow))


def screen2():

    pygame.draw.rect (gameDisplay, black, [0,0,795,445])

    pygame.draw.rect (gameDisplay, yellow, [20,20,200,200],1)


while not gameExit:                     #
    for event in pygame.event.get():    # quit loop
        if event.type == pygame.QUIT:   #
            gameExit = True             #

#----------main loop follows----------------------------------

    screen1()   #call screen1 routine


    pygame.draw.rect (gameDisplay, black, [0,0,795,445])

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    myfont = pygame.font.SysFont(None,50)

    pygame.draw.rect (gameDisplay, black, [50,50,150,50])
    textSurface = myfont.render (str(mouse),True, (yellow))

#-----------quit routine---------------------

print("Exit succsessful")

Return to “Python”