I am trying to move the mouse continuously with pyautogui so, I used a loop and moveTo function of pyautogui to move the mouse continuously to random points but I don't know to smooth the paths so that they don't look like intersecting lines. I found out that I can use Bezier curves but I couldn't find any explanation on how to use that.
import pyautogui
x_dim, y_dim = pyautogui.size()
for i in range(1,50):
pyautogui.moveTo(random.randint(0, x_dim), random.randint(0, y_dim), duration = 0.25)
Also is there any function that allows us to restrict our mouse movement to an element on a website?
Related
I am making a fun python program to automate couple clicks for me. I want to use pyAutoGui library provided by python to make this program. I am struggled to find the coordinates of elements that I want to click. Are there any ways that I can find coordinates of my elements?
You can use pyautogui.mouseInfo().
MouseInfo is an application to display the XY position and RGB color information of the pixel currently under the mouse. Works on Python 2 and 3. This is useful for GUI automation planning.
The full documentation is at https://mouseinfo.readthedocs.io/en/latest/
Point Position (for Windows) is a simple tool that lets you pick the coordinates for any point on your screen (using X,Y axis). Simply point one of the four corner arrows at the spot on your screen that you want to define and click the button to display the X/Y coordinates.
At the program start, you should disable all turtle animations and make the turtle hidden. Next your program should implement the following requirements.
Prompt the user for a couple of inputs, the grid size N and a difficulty level between 1 to 3.
Draw an N × N grid as shown in this figure. Put the game title, start button and score on the top.
Handle mouse clicks in the turtle window. Clicking on the start button should, well, start the game but other clicks should be ignored.
Implement the gameplay, which includes displaying ten squares on the grid one after the other. Display the current square number (1 to 10) at the top. Each square should occupy a random box on the grid. A square would only stay on screen for a period of 2, 1.5 or 1 seconds, corresponding to difficulty levels of 1, 2 and 3 respectively.
Let the user play the game with mouse. Objective of the game is to shoot (click) as many squares as possible. A hit increases the score and a miss deducts the score by one. Only the clicks inside grid boundaries should be entertained. If a square is successfully hit, its colour should change to give user an indication of success.
Once the time is elapsed for all ten squares, replace the box number at the top with the ‘finished’ label.
Note that turtle graphics do not provide support for erasing a part of drawing (or text). To mimic erasing, you should repaint the desired area with a white (or whatever your background color) filled rectangle.
To show and hide the target squares at regular time intervals, do not use a loop. This job can be managed via task scheduling as shown below in the sample codes.
Constraints
You can only import the following library modules: turtle, threading, random, math, sys
In many online examples of turtle graphics applications, multiple turtle objects are used on the same window. You are NOT allowed to proceed that way. Use a single turtle for all drawings.
You should follow good programming practices, for example using named constants, creating several reusable functions (top down design) and minimizing the use of global variables. A few global variables will be essential though, for example, to store the data required by multiple functions.
Suggested dimensions and locations of elements
Window size: 750 × 800
Grid Size: 700 × 700
Bottom left corner of grid: (–350, –375)
Size of target squares: Depends on size of a grid box. Leave 10 pixels margin around sides, so that squares do not touch the grid lines.
Game title text location: (–350, 345) left aligned
Start button / current square number location: (0, 345) centre aligned
Score display location (350, 345) right aligned
So I dont have code because its not an issue with coding as I already have the program done and working however I noticed that:
In gaming menus mouse sensitivity doesnt matter because I set coords to click/move and since its not sensitivity related it works just fine.
However,
When moving a game camera(character view) if I do for example 'move 1000 pixels to the right' this will be affected by mouse sensitivity & ingame sensitivity is there no way to make this work in all resolutions / mouse sensitivity?
Essentially I need to be able to make my friends 'camera"/'character view' to move as much as mine while having a different mouse DPI
Instead of moving in a relative way move to a fixed point of your display for example:
import pyautogui
import time
time.sleep(3)
pyautogui.moveTo(100, 100)
This will move your mouse to 100 pixels along 100 pixels down on your monitor. Other examples:
# To make it drag along the screen
# (x, y, time taken)
pyautogui.moveTo(100, 100, 5)
# To make it hold while moving
# The 5 slows it
pyautogui.dragTo(100, 100, 5)
# To check if the next move pos is on display
# This will return false
pyautogui.onScreen(0, -1)
# To Move only x/y
pyautogui.moveTo(None, 500)
Allthough pyautogui should only move directional to the pixels irrespective of the DPI so may you please attach your code.
Thanks for taking the time to read this.
Right now I'm making a really basic tile based game. The map is a large amount of 16x16 tiles, and the character image is 16x16 as well. My character has its own class that is an extension of the sprite class, and the x and y position is saved in terms of the tile position.
To note I am fairly inexperienced with pygame.
My question is, I am planning to have character movement restricted to one tile at a time, and I'm not sure how to make it so that, even if the player hits the directional key dozens of time quickly, (WASD or arrow keys) it will only move from tile to tile at a certain speed. How could I implement this generally with pygame? (Similar to game movement of like Pokemon or NexusTk). One movement would result in a player being in a tile. They couldn't stop halfway between tiles for example.
Thanks for your time! Ryan
You store your characters location as a grid coordinate. So if he's at (2,0) he is rendered at (32,0). The game then animates him moving between tiles, but, he's either on one or the other. While in the move state, you render an (x,y) offset between 0 to tilewidth.
It sounds like you want one move per keypress, if time elapsed / animation has completed. So:
On keypress, toggle to: animating state
Set destination tile coordinate
draw offset, between 0 and tilewidth, depending on time elapsed. offset = (elapsed_ms / 1000.) * tile_w would scale between 0 to 16 if time is less<= 1 second.
Once time elapsed is >= animation length (I chose 1000. above), switch to stationary state.
If keypress happens while in animation state, ignore it.
Pygame example: using numpy for map array.
I would like to 'lock' the mouse into a specific Y coordinate on the Tkinter Canvas, so that when a user moves the mouse they can traverse the X axis but not the Y (the mouse would slide left and right but never up/down). Is there was a way to do this and how is it done?
You might be able to do something like that in a game library, but to the best of my knowledge, this is not supported in a GUI library. You should try PyGame or Kivy.