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.