1

So I'm trying to write a simple Arkanoid game, using only pgzero (!not pygame), and I want my paddle to move not using my keyboard, but using my mouse, so that the paddle moves left or right following the cursor, how can I do that and how to implement that as a class method?

I tried doing some research, watching multiple tutorials, and read documentation, it didn't help

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
maksonios
  • 15
  • 4

1 Answers1

0

on_mouse_move is a callback function that is automatically called when the mouse is moved. See Pygame Zero - Event Handling Hooks.

Minimal example of drawing a rectangle at the position of the mouse cursor:

import pgzrun

WIDTH = 400
HEIGHT = 400
rect = Rect(0, 0, 30, 30)

def draw():
    screen.fill("black")
    screen.draw.filled_rect(rect, "red")

def on_mouse_move(pos, rel, buttons):
    rect.centerx = pos[0] 
    rect.centery = pos[1] 

pgzrun.go()

Minimal example of drawing an Actor at the position of the mouse cursor:

import pgzrun

WIDTH = 400
HEIGHT = 400
actor = Actor("image_name")

def draw():
    screen.fill("black")
    actor.draw()

def on_mouse_move(pos, rel, buttons):
    actor.x = pos[0]
    actor.y = pos[1]

pgzrun.go()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • that helps a lot. could you also explain, how to make it only move left or right on the bottom of the screen? it moves my paddle up and down, which is not what I need, thank you – maksonios Jan 08 '23 at 11:07
  • @maksonios `rect.centerx = pos[0]` for the 1st example and `apple.x = pos[0]` for the 2nd example. Of course you have to place the objects at the bottom e.g.: `rect.bottom = 400` – Rabbid76 Jan 08 '23 at 11:10