1

Direction detection is not working. In my code, I detect direction by comparing the player's X coordinate to the mouse's X coordinate. I made it in Python with PyGame. If I try to print the direction, output is Left. Like this:

if x > mpos[1]:
    print("Left")
if x < mpos[1]:
    print("Right")

My whole code looks like this:

import pygame
import random

pygame.init()

window = pygame.display.set_mode([1920,1000])

pygame.display.set_caption('Bouncy Ball')

x = 1920 / 2
y = 1000 / 2

count = 0
aft = False


xbox1 = random.randint(0,1920)
ybox1 = -30
game = True
while game:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game = False
    window.fill((255,255,255))
    player = pygame.Rect(x - 128, y - 128, 128, 128)
    platform = pygame.Rect(xbox1 - 30, ybox1 - 128, 256, 60)
    pygame.draw.rect(window, (0,0,255), player)
    pygame.draw.rect(window, (255,0,0), platform)
    pygame.event.get()
    mse = pygame.mouse.get_pressed()
    if mse[0] == True:
        mpos = pygame.mouse.get_pos()
        pygame.draw.circle(window, (175,175,175), (mpos[0], mpos[1]), 64)
        force = abs(mpos[0] - x - 128 + mpos[1] - y - 128) / 100
        aft = True
    if mse[0] == False:
        if aft:
            if x > mpos[1]:
                print("Left")
            if x < mpos[1]:
                print("Right")
            print(mpos)
            print((x,y))
            aft = False
    pygame.display.flip()
    ybox1 = ybox1 + 1
    if ybox1 == 1060:
        ybox1 = -30
        xbox1 = random.randint(0,1920)
    y = y + count
    if count < 5:
        count = count + 1
    if y >= 1100:
        y = 1000 / 2
pygame.quit()

I need to find the direction to send the player flying to the next platform. (Code is incomplete, work in progress)

I tried printing the coordinates of both mouse and player, to find coordinates are correct, but the output is wrong. I tried changing < to > and > to < but that didn't work.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

2 Answers2

2

pygame.mouse.get_pos returns the a tuple with the x and y position of the mouse cursor. So mpos[1] is the y-coordinate. The x coordinate is mpos[0]:

mpos = pygame.mouse.get_pos()
if x > mpos[0]:
    print("Left")
if x < mpos[0]:
    print("Right")

I suggest using the MOUSEDOWN event instead of pygame.mouse.get_pressed() for click detection. See Pygame mouse clicking detection.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

Well, you are using an Y axis.
Heres the fixed code:

import pygame
import random

pygame.init()

window = pygame.display.set_mode([1920,1000])

pygame.display.set_caption('Bouncy Ball')

x = 1920 / 2
y = 1000 / 2

count = 0
aft = False


xbox1 = random.randint(0,1920)
ybox1 = -30
game = True
while game:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game = False
    window.fill((255,255,255))
    player = pygame.Rect(x - 128, y - 128, 128, 128)
    platform = pygame.Rect(xbox1 - 30, ybox1 - 128, 256, 60)
    pygame.draw.rect(window, (0,0,255), player)
    pygame.draw.rect(window, (255,0,0), platform)
    pygame.event.get()
    mse = pygame.mouse.get_pressed()
    if mse[0] == True:
        mpos = pygame.mouse.get_pos()
        pygame.draw.circle(window, (175,175,175), (mpos[0], mpos[1]), 64)
        force = abs(mpos[0] - x - 128 + mpos[1] - y - 128) / 100
        aft = True
    if mse[0] == False:
        if aft:
            if x > mpos[0]: # Notice this!
                print("Left")
            elif x < mpos[0]:
                print("Right")
            print(mpos)
            print((x,y))
            aft = False
    pygame.display.flip()
    ybox1 = ybox1 + 1
    if ybox1 == 1060:
        ybox1 = -30
        xbox1 = random.randint(0,1920)
    y = y + count
    if count < 5:
        count = count + 1
    if y >= 1100:
        y = 1000 / 2
pygame.quit()
Kirill
  • 55
  • 1
  • 4