I have a problem making T-Rex game bot. The code works fine for a few seconds but then the game gets over.
I have used Selenium and The Robot class for this project.
MyCode>>
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\BHEL\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://trex-runner.com/");
Robot robo = new Robot();
robo.mouseMove(460, 520);
while(true) {
//now i have used for loops to sense the cactus in a square area
for(int i = 0; i<=10;i++) {
for(int j=0;j<=10;j++) {
if(robo.getPixelColor(460-i, 520-j).equals(new Color(83,83,83))) {
robo.keyPress(KeyEvent.VK_UP);
robo.keyRelease(KeyEvent.VK_UP);
Thread.sleep(200-j-i);//Stay in the air for a while
robo.keyPress(KeyEvent.VK_DOWN);
robo.keyRelease(KeyEvent.VK_DOWN);
}
}
}
}
}
The WebSite url for T-Rex game that i used https://trex-runner.com .
I have seen some other tutorials that did the same thing that i did.
Thanks for your help :)
Could be that the coordinates aren't too precise. You could use System.out.println(MouseInfo.getPointerInfo().getLocation()); to get the coordinates of your mouse on your monitor and then change your x and y value to those. That method would get me to 400 points. To get further you have to add more conditions so the robot responds better to the obstacles.
Simple code:
while (true) {
//Got the coordinates from the mouse location ^^^
if (robo.getPixelColor(580 ,530).equals(new Color(83,83,83))) {
robo.keyPress(KeyEvent.VK_SPACE);
robo.keyRelease(KeyEvent.VK_SPACE);
Thread.sleep(150);//Stay in the air for a while
robo.keyPress(KeyEvent.VK_DOWN);
robo.keyRelease(KeyEvent.VK_DOWN);
}
}
Related
I got used to my touch pad, that allows to scroll smoothly and very exactly, but I can not to simulate it by Java robot - mousewheel is getting only integer parameters and a scrolling carried by steps. Can I simulate smoothly scrolling in Java?
robot.mouseWheel(a); // int a
The unit of scrolls will always be by "notches of the wheel" (before you ask: that's how the measurement is named in the docs). This is simply how it's implemented by the hardware (to be more specific: the mouse). How many pixels are scrolled per "notch" is nothing but OS-configuration. You can't mess with that with pure java and I wouldn't recommend it, even if it was possible.
What you can do nevertheless is to slow down the speed at which the robot scrolls:
import java.awt.Robot;
public class Test{
public static void main(String[] args)
throws Exception
{
//time to switch to a specific window where the robot ought to be tested
try{ Thread.sleep(2000); }catch(InterruptedException e){}
Robot r = new Robot();
for(int i = 0; i < 20; i++){
//scroll and wait a bit to give the impression of smooth scrolling
r.mouseWheel(1);
try{ Thread.sleep(50); }catch(InterruptedException e){}
}
}
}
I am currently using Java's Robot Class within a DirectX game programmed in C++. I can successfully use the Robot class's mouseMove method, but when I try to use the mouse left click input event, nothing happens. I have tried different time spacings between the release and press to no avail. Note: I am currently running eclipse in administrator mode. Here is the code:
public class test {
public static void main(String [] args) throws AWTException, I nterruptedException{
Robot r = new Robot();
Thread.sleep(3000);
for(int i = 0; i<20; i++){
r.mouseMove(100+i*50, 550);
Thread.sleep(1);
}
Thread.sleep(1000);
r.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(50);
r.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(50);
r.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(50);
r.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(50);
}
}
Any idea on how to get the mouse click to register?
In some games you just cant do this.Depends on the engine and implementation , for instance in source engine games your events will be registered(probably,based on my experience example :CS:GO) , bud unreal engine games might not register anything.
Its really common for game developers to actually block some calls of WIN32 mouse_event , which is what Robot API uses.So there is not much you can do to go around it(with Robot api).
import kareltherobot.*;
public class main
{
public static Robot ho;
public static void main (String[] args){
Robot ho = new Robot(1, 1, Directions.East, 10000);
for(int i = 0; i < 10000; i++){
ho.putBeeper();
ho.move();
}
}
static
{
World.reset();
World.setVisible(true);
World.setSize(100, 100);
World.setDelay(2);
}
}
Here is my code for karel the robot. I'm taking Java in high school and suck at it, I am trying to make karel go through a 100 by 100 world and put beepers on every square, but I don't know how to do this.
Karel API:
the move command moves the robot one space in the direction it's facing.
The put beeper command places a beeper in that space it is currently in.
Don't worry about the 2d array, that is too advanced. I remember karel is the first thing you learn in compsci. If I remember correctly, don't they have a turnleft or turnright method?You could make it move, put beepers, and then turn.
I am the programmer for my FRC Team 4468 and we are using mecanum wheels this year. We are trying to control the robot with two joysticks, one for moving in a direction (mecStick), and another for rotation (rotStick) using this line of code.
myDrive.mecanumDrive_Cartesian(mecStick.getX(), mecStick.getX(), rotStick.getY(), 0);
The robot will move in the Y direction (fowards, backwards), and will rotate but won't move in the X axis. Could someone tell me what i'm doing wrong please.
Thanks :-)
public class RobotTemplate extends SimpleRobot {
RobotDrive myDrive = new RobotDrive(1,2,3,4);
Joystick mecStick = new Joystick(1);
Joystick rotStick = new Joystick(2);
public void robotInit() {
}
public void autonomous() {
}
public void operatorControl() {
//myDrive.setSafetyEnabled(true);
myDrive.mecanumDrive_Cartesian(mecStick.getX(), mecStick.getX(), rotStick.getY(), 0);
Timer.delay(0.01);
}
}
Looks like you pass mecStick.getX() twice, one should probably be mecStick.getY(). I'm not familiar with the RobotDrive class, so I'm not sure which should be switched. The WPILib Javadoc is your friend, you can find a copy hosted by team 2168 at http://team2168.org/javadoc/. Look for RobotDrive on the left bottom list, and check there.
Best of luck from 1902, Exploding Bacon!
I am programming a chess game in java, and at the moment I am building a basic interface. It is simply an 8x8 array of buttons that will display in a window. I have coded for these buttons, and have gotten the board to display properly. However, when I connect this with the rest of the game, the game window crashes upon running and I have to force quit the java application. This is my code:
package Chess_Game;
import javax.swing.SwingUtilities;
import Chess_Interface.Iboard;
public class Game_Tester
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Game G = new Game();
Iboard I = new Iboard(G.getBoard().getArray(), G.getSides());
I.setVisible(true);
while(!(G.isGameOver()))
{
boolean redo = true;
while(redo)
{
int row = 0;
int col = 0;
int nRow = 0;
int nCol = 0;
System.out.println("Click the piece you want to move.");
while(!(I.getBool())){}
if(I.getBool())
{
row = I.getRow();
col = I.getCol();
I.setBool(false);
}
System.out.println("Click the place you want to move to.");
while(!(I.getBool())){}
if(I.getBool())
{
nRow = I.getRow();
nCol = I.getCol();
I.setBool(false);
}
if(G.canMove(row, col, nRow, nCol))
{
G.move(row, col, nRow, nCol, I);
redo = false;
}
else
{
System.out.println("You cant move there! Try again!");
}
}
I.updateBoard(G.getBoard().getArray(), G.getSides());
}
}
});
}
}
The board displays properly when I comment out the main while loop (and everything inside of it), and I assume the problem lies somewhere inside there, but I have been unable to find it. I have also looked online for similar game loop problems, but all of those have been for games involving frame rates and movement across a java swing frame, something that is not present in my code.
Any help would be greatly appreciated.
You have several loops such as
while(!(I.getBool())){}
which could potentially run forever if I does not respond as expected. You could start by printing something out within these loops, and within the following blocks if(I.getBool()){...} to see at what point your application gets stuck.
Checking the user interface in a loop like this is not good practice. It is better to use Listeners to respond to the user interface.
Nor is running the main application on the Swing thread using SwingUtilities.invokeLater(new Runnable(), even though it avoids potential problems of updating the GUI from another thread.
In fact, this may be your root problem, as running the main application loop on the Swing thread (the thread used to run the GUI) like this probably prevents the GUI from ever responding properly. You are putting a task (the entire game) onto the GUI's queue, but that task never completes while(!(G.isGameOver())).