Advertisement
Guest User

Untitled

a guest
Apr 12th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. package cs218087.CSRun;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.awt.*;
  6. import java.awt.event.KeyAdapter;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.MouseAdapter;
  9. import java.awt.event.MouseEvent;
  10.  
  11. public class Keying extends JPanel{
  12. private static final long serialVersionUID = 1L;
  13. public Rectangle character;
  14. public Rectangle bottomBox;
  15. public int charW = 24;
  16. public int charH = 36;
  17. public int f3counter = 1;
  18.  
  19. public boolean right = false;
  20. public boolean left = false;
  21. public boolean jumping = false;
  22. public boolean obstacleEndOfScreen = false;
  23.  
  24. public int obstacleX = 565;
  25. public int levelCounter = 0;
  26. public int obstacleSpeed = 1;
  27.  
  28. public boolean f3menu = false;
  29.  
  30. public long jumpingTime = 90;
  31.  
  32. public Point mouse;
  33.  
  34. public Keying(Display f, Images i){
  35. character = new Rectangle(180, 180, charW, charH);
  36. bottomBox = new Rectangle(0, 350, 9000, 30);
  37. f.addKeyListener(new KeyAdapter(){
  38. public void keyPressed(KeyEvent e){
  39. if(e.getKeyCode() == KeyEvent.VK_D){
  40. right = true;
  41. }
  42. if(e.getKeyCode() == KeyEvent.VK_A){
  43. left = true;
  44. }
  45. if(e.getKeyCode() == KeyEvent.VK_M){
  46. }
  47. if(e.getKeyCode() == KeyEvent.VK_SPACE){
  48. jumping = true;
  49. new Thread(new thread()).start();
  50. }
  51. if(e.getKeyCode() == KeyEvent.VK_F3){
  52. f3counter++;
  53. if(f3counter % 2 == 0){
  54. f3menu = true;
  55. }else{
  56. f3menu = false;
  57. }
  58. }
  59. }
  60. public void keyReleased(KeyEvent e){
  61. if(e.getKeyCode() == KeyEvent.VK_D){
  62. right = false;
  63. }
  64. if(e.getKeyCode() == KeyEvent.VK_A){
  65. left = false;
  66. }
  67. }
  68. });
  69. f.addMouseListener(new MouseAdapter() {
  70. public void mouseClicked(MouseEvent e){
  71. mouse = new Point(e.getX(), e.getY() - 25);
  72.  
  73. if(e.getButton() == MouseEvent.BUTTON1){
  74. character.x = mouse.x;
  75. character.y = mouse.y;
  76. }
  77. }
  78. });
  79. }
  80.  
  81. public void paintComponent(Graphics g){
  82. if(Main.f.i.imagesLoaded == true){
  83. super.paintComponent(g);
  84. g.drawImage(Main.f.i.bg, 0, 0, 600, 400, null);
  85. Point pt1 = new Point(character.x, character.y + character.height);
  86. if(!bottomBox.contains(pt1) && !jumping){
  87. character.y++;
  88. }
  89. g.setColor(Color.WHITE);
  90. g.fillRect(character.x, character.y, character.width, character.height);
  91. g.fillRect(bottomBox.x, bottomBox.y, bottomBox.width, bottomBox.height);
  92. g.setColor(Color.PINK);
  93. g.fillRect(obstacleX, bottomBox.y - 60, 20, 60);
  94. if(right == true && character.x != Main.w - charW){
  95. character.x += 1;
  96. }
  97. if(left == true && character.x != 0){
  98. character.x -= 1;
  99. }
  100. if(jumping == true){
  101. character.y = (int) (character.y - 0.1);
  102. }
  103. if(f3menu == true){
  104. g.drawString("CSRun 1.0", 30, 40);
  105. g.drawString("Made by Cs218087", 30, 52);
  106. g.drawString("Level: " + levelCounter/10, 30, 64);
  107. }
  108. obstacleX = (int) (obstacleX - obstacleSpeed);
  109. if(obstacleX <= 0){
  110. obstacleX = 600;
  111. levelCounter++;
  112. }
  113. repaint();
  114. }
  115. }
  116. public class thread implements Runnable{
  117. @Override
  118. public void run() {
  119. try{
  120. Thread.sleep(jumpingTime);
  121. jumping = false;
  122. }catch (Exception e){
  123. e.printStackTrace();
  124. new Thread(this).start();
  125. System.exit(0);
  126. }
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement