0

Trying to make mobile joystick, went well until i stumbled on a roadblock, that is trying to make a player move.

the problem is, the player position isn't really moving, its only set on the position of the given x,y mouse pos, thus giving the player position on the center.

from pygame import *;
import pygame;
import math;
import os;

pygame.init();
os.chdir("/storage/emulated/0/Insanity");

ingame = True;
source_dir = os.path.dirname(os.path.abspath(__file__));
screen = pygame.display.set_mode((1280,720));

def get_velo(x,y, dist):
    vec1,vect2 = 0,0;
    vec1 = y / dist;
    vec2 = x / dist;
    
    return vec1,vec2;

class sprite(pygame.sprite.Sprite):
    def __init__(self,img, size, pos):
        self.img = pygame.image.load(img[1]);
        self.size = pygame.transform.scale(self.img, (size[0],size[1])).convert_alpha();
        # HITBOX
        self.rect = self.size.get_rect(center=pos);
        self.pos = self.rect.move(pos);
        
    def display(self):
        screen.blit(self.size, self.pos);
        
HW, HH = 1280/2, 720/2;
AREA = 1280*720;

joystick_body = sprite(["", "circle.png"], [100,100], (0,0));
joystick_control = sprite(["", "circle.png"], [30,30], (0,0));

joystick_body.pos = [0,630];
radius = joystick_body.size.get_rect().center[0]; 

my_velo = 0;

## PLAYER
player = sprite(["", "player.png"], [100,100], (0,0));
movespeed = 10;
while ingame:
    screen.fill((255,255,255));
    ## JOYSTICK
    joystick_body.display();
    x, y = pygame.mouse.get_pos();
    
    # CIRCLE COLLISION
    v1 = pygame.math.Vector2(0, 630);
    v2 = pygame.math.Vector2(x,y);
    
    if v1.distance_to(v2) < radius + radius:
        joystick_control.pos = (x,y);
        joystick_control.rect.clamp_ip(joystick_body.rect);
        vec1,vec2 = x / radius, y / radius# x / v1.distance_to(v2), y / v1.distance_to(v2)
        #vec1,vec2 = (x,y, v1.distance_to(v2));
        
        player.pos = (vec1*movespeed,vec2*movespeed);
        #joystick_control.pos = joystick_body.pos + (joystick_control.pos - joystick_body.pos).clamped(radius);
    else:
        joystick_control.pos = (33,664);
    
    player.display();
    joystick_control.display();
    pygame.display.update();

the video: https://www.veed.io/view/442d6abd-c072-41bb-98be-be5b1fb1ba42/223fbe52-0cf9-4724-9393-d7a85f9beb68?sharingWidget=true&panel=share

I tried pygame.vector2, rect.move() and the same happened, i felt like i need to use the player.pos x and y but i dont know how i would implement it, if there is a pygame version of godot function (move_and_slide()). I expect to make a working mobile joystick

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

0 Answers0