Guest User

Untitled

a guest
Dec 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5.  
  6. interface TPersonage extends Runnable {
  7. public void step();
  8. public void decrement();
  9. }
  10.  
  11. abstract class Personage extends JButton implements TPersonage {
  12. int x, y, dx, dy;
  13. public Personage(int x, int y) {
  14. super();
  15. this.x = x;
  16. this.y = y;
  17. x = 250;
  18. y = 250;
  19. //this.c = c;
  20. setSize(25, 25);
  21. setLocation(x, y);
  22. //setBackground(c);
  23. new Thread(this).start();
  24. }
  25.  
  26. public void step() {
  27. x += dx;
  28. y += dy;
  29. setLocation(x, y);
  30. }
  31.  
  32. public abstract void decrement();
  33.  
  34. public void run() {
  35. while (true) {
  36. try {
  37. Thread.sleep(100);
  38. }
  39. catch (Exception e) {
  40. }
  41. decrement();
  42. step();
  43. }
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50. class Main {
  51. public static void main(String args[]){
  52. new MyFrame ("Simple PacMan");
  53. }
  54. }
  55. class MyFrame extends JFrame implements WindowListener {
  56. MainCharacter mc;
  57. public MyFrame (String title){
  58. super (title);
  59. setSize(1250, 720);
  60. getContentPane().setLocation(0,0);
  61. setLocation(0, 0);
  62. setBackground(new Color(255, 218, 185));
  63. setResizable(false);
  64. getContentPane().setLayout(null);
  65. getContentPane().add(mc = new MainCharacter(575,250));
  66. addWindowListener(this);
  67. setVisible(true);
  68. mc.setFocusable(true);
  69. }
  70. public void windowClosing(WindowEvent we){
  71. System.exit(0);
  72. }
  73. public void windowClosed(WindowEvent we){}
  74. public void windowOpened(WindowEvent we){}
  75. public void windowActivated(WindowEvent we){}
  76. public void windowDeactivated(WindowEvent we){}
  77. public void windowIconified(WindowEvent we){}
  78. public void windowDeiconified(WindowEvent we){}
  79. }
  80.  
  81. class MainCh extends Personage implements KeyListener {
  82. int code;
  83. public MainCh(int x, int y) {
  84. super(x, y);
  85.  
  86. addKeyListener(this);
  87. setFocusable(true);
  88. }
  89.  
  90. public void keyReleased(KeyEvent e) {
  91. int val = e.getKeyCode();
  92.  
  93. if (val >= 37 && val <= 40) {
  94. code = val;
  95. }
  96.  
  97. System.out.println(val);
  98. }
  99.  
  100. public void keyPressed(KeyEvent e) {
  101. }
  102.  
  103. public void keyTyped(KeyEvent e) {
  104. }
  105.  
  106. public void decrement() {
  107. switch (code) {
  108. case 37:
  109. dx = -40;
  110. dy = 0;
  111. break;
  112. case 38:
  113. dx = 0;
  114. dy = -40;
  115. break;
  116. case 39:
  117. dx = 40;
  118. dy = 0;
  119. break;
  120. case 40:
  121. dy = 40;
  122. dx = 0;
  123. break;
  124. }
  125.  
  126.  
  127. y += dy;
  128. }
  129.  
  130. public int getX() {
  131. return x;
  132. }
  133.  
  134. public int getY() {
  135. return y;
  136. }
  137. }
Add Comment
Please, Sign In to add comment