Skip to content

Instantly share code, notes, and snippets.

@InspiritedCoder
Created December 14, 2018 18:07
Show Gist options
  • Save InspiritedCoder/5770564debf58e93ed100cd62c3630d9 to your computer and use it in GitHub Desktop.
Save InspiritedCoder/5770564debf58e93ed100cd62c3630d9 to your computer and use it in GitHub Desktop.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
interface TPersonage extends Runnable {
public void step();
public void decrement();
}
abstract class Personage extends JButton implements TPersonage {
int x, y, dx, dy;
public Personage(int x, int y) {
super();
this.x = x;
this.y = y;
x = 250;
y = 250;
//this.c = c;
setSize(25, 25);
setLocation(x, y);
//setBackground(c);
new Thread(this).start();
}
public void step() {
x += dx;
y += dy;
setLocation(x, y);
}
public abstract void decrement();
public void run() {
while (true) {
try {
Thread.sleep(100);
}
catch (Exception e) {
}
decrement();
step();
}
}
}
class Main {
public static void main(String args[]){
new MyFrame ("Simple PacMan");
}
}
class MyFrame extends JFrame implements WindowListener {
MainCharacter mc;
public MyFrame (String title){
super (title);
setSize(1250, 720);
getContentPane().setLocation(0,0);
setLocation(0, 0);
setBackground(new Color(255, 218, 185));
setResizable(false);
getContentPane().setLayout(null);
getContentPane().add(mc = new MainCharacter(575,250));
addWindowListener(this);
setVisible(true);
mc.setFocusable(true);
}
public void windowClosing(WindowEvent we){
System.exit(0);
}
public void windowClosed(WindowEvent we){}
public void windowOpened(WindowEvent we){}
public void windowActivated(WindowEvent we){}
public void windowDeactivated(WindowEvent we){}
public void windowIconified(WindowEvent we){}
public void windowDeiconified(WindowEvent we){}
}
class MainCh extends Personage implements KeyListener {
int code;
public MainCh(int x, int y) {
super(x, y);
addKeyListener(this);
setFocusable(true);
}
public void keyReleased(KeyEvent e) {
int val = e.getKeyCode();
if (val >= 37 && val <= 40) {
code = val;
}
System.out.println(val);
}
public void keyPressed(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void decrement() {
switch (code) {
case 37:
dx = -40;
dy = 0;
break;
case 38:
dx = 0;
dy = -40;
break;
case 39:
dx = 40;
dy = 0;
break;
case 40:
dy = 40;
dx = 0;
break;
}
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment