import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.util.*;
public class myGame extends JComponent {
public int w = 400;
public int z = 400;
Random ar = new Random();
public int x = ar.nextInt(400);
Random har = new Random();
public int y = har.nextInt(400);
Random r = new Random();
Random rand = new Random();
Random newx;
Random newy;
int resetTimes = 0;
int ex = r.nextInt(400);
int why = rand.nextInt(400);
Image petergriffin;
public void drawImg(){
BufferedImage petergriffin = null;
try {
petergriffin = ImageIO.read(new File("Users/MW/Downloads/petergriffin.png"));
} catch (IOException e) {
}
}
public void ResetIt(){
if(x <= ex+17.5 && y <= why+17.5 && x >= ex-17.5 && y >= why-17.5){
newx = new Random();
newy = new Random();
int newex = newx.nextInt(450);
int newwhy = newy.nextInt(450);
x = newex;
y = newwhy;
resetTimes++;
repaint();
}
}
KeyListener kw = new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if (k == KeyEvent.VK_W){
y=y-10;
repaint();
ResetIt();
}
if (k == KeyEvent.VK_S){
y = y+10;
repaint();
ResetIt();
}
if(k == KeyEvent.VK_A){
x=x-10;
repaint();
ResetIt();
}
if(k == KeyEvent.VK_D) {
x=x+10;
repaint();
ResetIt();
}
if(k == KeyEvent.VK_Q) {
x=x-10;
y=y-10;
repaint();
ResetIt();
}
if(k == KeyEvent.VK_E) {
x=x+10;
y=y-10;
repaint();
ResetIt();
}
};
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
};
myGame() {
JFrame f = new JFrame("SquareMove 1.0");
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(this);
f.add(p);
f.setSize(500, 500);
f.setVisible(true);
JLabel l = new JLabel("Users/MW/Downloads/petergriffin.png");
p.add(l);
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addKeyListener(kw);
p.setBackground(Color.blue);
}
public void paintComponent(Graphics g) {
g.fillRect(x,y,25,25);
g.drawRect(ex, why,70,70);
g.setColor(Color.red);
g.fillRect(x, y, 50, 50);
g.setColor(Color.white);
g.drawString("enter",ex+5,why-10);
g.setColor(Color.white);
g.drawString("Score:"+resetTimes,20,20);
g.drawString("Use WASD to move around", 150, 20);
petergriffin = new ImageIcon("Users/MW/Downloads/petergriffin.png").getImage();
g.drawImage(petergriffin,x,y,null);
if(resetTimes > 5 && resetTimes <8){
g.drawString("Welcome to level two", 20, 50);
}
if(resetTimes>8& resetTimes<20){
g.drawString("Welcome to level three",20,50);
}
}
public static void main(String[] args) {
myGame d = new myGame();
d.drawImg();
d.ResetIt();
new myGame();
}
}