Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Mouse position with touch screen - coordinates wrong

Hi!
I'm currently implementing a small game where the user has to catch a mouse (appearing at random positions on the screen) with a cat. I'm working directly on the canvas and use the PyGame backend. The cat should appear where the user presses the touch screen and if close enough to the mouse he/she gains a point. The current implementation works good when using a mouse, although you don't see a cursor because it is intended to run on a tablet with a touch screen. When using the touch screen the coordinates are somehow far off the touched position and behave weirdly when touched multiple times.
Is there somebody who had a similar problem or any idea what is going wrong here?

Here's the code:

import numpy as np

my_canvas = canvas()
width, height = my_canvas.size
w = width/2
h = height/2
my_keyboard = keyboard(keylist=['space'], timeout=20)
my_mouse = mouse()
my_mouse.show_cursor(show=False)
t_s = self.time()
var.game_points = 0
t_run = 2000


def check_hit(x1, y1, x2, y2):
    dx = abs(x1 - x2)
    dy = abs(y1 - y2)
    d = math.sqrt(dx*dx + dy*dy)
    return d < 50

while game_on:
    pos_x = np.random.randint(-w + 10, w - 10)
    pos_y = np.random.randint(-h + 10, h + 10)

    print self.time()
    key, timestamp = my_keyboard.get_key()
    hit = False
    while(self.time() - t_s < t_run and key == None and hit == False):
        my_mouse.flush()
        button, (x,y), timestamp = my_mouse.get_click(timeout=t_run, visible=True)
        print 'pos', x, y

        my_canvas.clear()
        my_canvas.image(pool['mouse.jpg'], x=pos_x, y=pos_y, scale=0.16)
        my_canvas.image(pool['cat.png'], x=x, y=y, scale=0.5)
        my_canvas.text("Points: <b>%d</b>" % var.game_points, x=w - 100, y=h - 100)
        my_canvas.show()
        if check_hit(x, y, pos_x, pos_y):
            hit = True
            var.game_points += 1
            t_run -= 40
            break

        key, timestamp = my_keyboard.get_key()

    t_s = self.time()
    if not key == None:
        break
Sign In or Register to comment.