Move the box with keyboard and mouse

I want to move the box using W,A,S,D keys and rotate the box in mouse direction (even while moving). Below is the code I’ve written but it isn’t working properly (haven’t yet implemented the mouse functionality).

x = 100
y = 100

def setup():
    size(1500,850)
    
def draw():
    rect(x,y,100,100)

def keyPressed():
    global x, y
    if key == 'w':
        y-=7
    elif key == 'd':
        x+=7
    elif key == 'a':
        x-=7
    else:
        y+=7
1 Like

@GoToLoop has provided a link to some slick code that works well. However, if you’re struggling to understand that, then here’s something simpler. I’ve included some code that rotates the player to face the mouse pointer (the pushMatrix part)

up_pressed = False
left_pressed = False
...

x = 150
y = 150
speed = 5

def setup():
    size(1500, 850)

def draw():
    global x, y
    background(255)

    if up_pressed:
        y -= speed
    if left_pressed:
        x -= speed
    ...
    
    with pushMatrix():
        translate(x, y)
        angle = atan2(mouseX-x, mouseY-y)
        rotate(angle*-1)
        rect(-50, -50 , 100, 100)  # player
        rect(0, 0, 1, 500)         # beam

def keyPressed():
    global up_pressed, left_pressed

    if key == 'w':
        up_pressed = True
    if key == 'a':
        left_pressed = True
    ...

def keyReleased():
    global up_pressed, left_pressed

    if key == 'w':
        up_pressed = False
    if key == 'a':
        left_pressed = False
    ...

You can fill in the blanks :wink:

1 Like

Thank you! How do I vary the beam length with mouse position?

The code I included works only when the shooter is facing right, but I want in all directions.

    with pushMatrix():
        translate(x, y)
        angle = atan2(mouseX-x, mouseY-y)
        rotate(angle*-1)
        fill(255)
        # rect(-50, -50 , 100, 100)  # player
        image(img, -50, -50, 100, 100, 300, 300, 0, 0)
        strokeWeight(0)
        fill(255, 0, 0)
        rect(0, 0, 1.5, mouseX-x)         # beam

Use a line() function instead:

    ...

    with pushMatrix():
        translate(x, y)
        angle = atan2(mouseX-x, mouseY-y)
        rotate(angle*-1)
        image(img, -50, -50, 100, 100, 300, 300, 0, 0)
    
    line(x, y, mouseX, mouseY)  # beam

    ...

Also, how do I add the shooting functionality (on mouse click) while the player moves?

First, you’ll need to consider how you intend to approach this.

Check the reference to decide which of the mouse event functions apply – like, mouseClicked(), or mousePressed(), etc. Think: are you firing a single bullet per click? Or a continuous stream if the button is held down?

If there are multiple bullets on screen, how will you track all of them? A list could prove useful …

But, each bullet has an x-y coordinate, speed/direction, expiry time/distance, and so forth. Should you create a bullet class that includes those attributes?

If you provide some answers to these questions, and maybe some partial code, the forum members can guide your project in the right direction.

It’s single bullet per click

That’s not much of a plan to work with. Anyhow, here’s a little bullets demo that riffs off some code from another post

class bullet(object):
    def __init__ (self, xpos, ypos, speed):
        self.xpos = xpos
        self.ypos = ypos
        self.speed = speed
        
    def show(self):
        rect(self.xpos, self.ypos, 10, 10)

def setup():
    size(500, 500)
    global bullets
    bullets = []
    
def draw():
    background(0)
    for b in bullets:
        b.xpos -= b.speed
        b.show()

def mousePressed():
    bullets.append(bullet(mouseX, mouseY, -3))
    print(len(bullets))  # print the number of bullets

It’ll take some work to integrate. I assume that your bullets must move at angles, so I’d recommend vectors over xpos/ypos/speed values. Also, the bullets list just keeps on growing – as you’ll see in the console. The bullets continue to drift through space somewhere beyond the edge of the display window, never expiring.

This is a Java sketch w/ a Bullet class which I believe is close to what you’re asking: :coffee:
Studio.ProcessingTogether.com/sp/pad/export/ro.91kLmk61vAOZp

I’m thinking about concocting a Python version outta it. :snake:

2 Likes