Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. package Mark_1;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Canvas;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.KeyListener;
  12. import java.awt.image.BufferStrategy;
  13. import java.awt.image.BufferedImage;
  14. import java.awt.image.DataBufferInt;
  15.  
  16. import javax.swing.JFrame;
  17.  
  18. import java.awt.BorderLayout;
  19. import java.awt.Container;
  20. import java.awt.Dimension;
  21. import java.awt.event.*;
  22.  
  23. import javax.swing.*;
  24. public class Frame extends Canvas
  25. implements Runnable,
  26. KeyListener,
  27. ActionListener{
  28.  
  29. private static final long serialVersionUID = 1L;
  30. public static final int WIDTH = 160;
  31. public static final int HEIGHT = WIDTH/12*9;
  32. public static final int SCALE = 3;
  33. public static final String NAME = "FRAME";
  34.  
  35.  
  36. private JFrame frame;
  37. public boolean running = false;
  38. public int tickCount = 0;
  39. public Listener listener = new Listener();
  40. private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
  41. private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
  42. public static Ball ball = new Ball();
  43. public static int Paddle = HEIGHT*SCALE/2;
  44. public Frame(){
  45. setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
  46. setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
  47. setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
  48. frame = new JFrame(NAME);
  49. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50. frame.setLayout(new BorderLayout());
  51.  
  52. frame.add(this, BorderLayout.CENTER);
  53.  
  54. frame.pack();
  55. frame.setResizable(false);
  56. frame.setLocationRelativeTo(null);
  57. frame.setVisible(true);
  58. }
  59. public synchronized void start() {
  60. running = true;
  61.  
  62. new Thread(this).start();
  63.  
  64. }
  65. public synchronized void stop(){
  66. running = false;
  67. }
  68. public void run() {
  69. long lastTime = System.nanoTime();
  70. double nsPerTick = 1000000000D/60;
  71. int frames = 0;
  72. int ticks = 0;
  73. long lastTimer = System.currentTimeMillis();
  74. double delta = 0;
  75.  
  76. while(running){
  77. long now = System.nanoTime();
  78. delta +=(now- lastTime)/nsPerTick;
  79. lastTime = now;
  80. boolean shouldRender = false;
  81. while(delta>= 1){
  82. ticks++;
  83. tick();
  84. delta -= 1;
  85. shouldRender = true;
  86. }
  87.  
  88. if(shouldRender){
  89. frames++;
  90. render();
  91. }
  92. if(System.currentTimeMillis() - lastTimer > 1000){
  93. lastTimer += 1000;
  94. System.out.println(frames+","+ticks);
  95. System.out.println(ball.slope[0]+","+ball.slope[1]);
  96. frames = 0;
  97. ticks = 0;
  98. }
  99. }
  100.  
  101. }
  102. public void tick(){
  103. tickCount++;
  104. }
  105. public void path(){
  106.  
  107. if(tickCount == 60){
  108. ball = new Ball();
  109. ball.x = (int) (Math.random()*161);
  110. ball.y = (int) (Math.random()*161);
  111. ball.slope[0] = (int) (Math.random()*3)+1;
  112. ball.slope[1] = (int) (Math.random()*3)+1;
  113. //System.out.println(ball.x+","+ball.y+","+ball.slope[0]+","+ball.slope[1]);
  114. }
  115. else{
  116. //System.out.println(ball.x+","+ball.y+","+ball.slope[0]+","+ball.slope[1]);
  117. ball.x += ball.slope[0];
  118. ball.y += ball.slope[1];
  119.  
  120. if(ball.x > WIDTH*SCALE-1 || ball.x < 0 || ball.y > HEIGHT*SCALE-1 || ball.y < 0){
  121. int dir = (int) (Math.random()*4)+1;
  122. int dirI = -1;
  123.  
  124. if(dir == 1){
  125. if(ball.slope[0]>0)
  126. dirI = 1;
  127.  
  128. ball.slope[0] = (int) ((Math.random()*3)+1)*dirI;
  129. }
  130. else if(dir == 1){
  131.  
  132. if(ball.slope[1]>0)
  133. dirI = 1;
  134.  
  135. ball.slope[1] = (int) ((Math.random()*3)+1)*dirI;
  136. }
  137. }
  138.  
  139. if(ball.x > WIDTH*SCALE){
  140. ball.slope[0] *= -1;
  141. ball.x += ball.slope[0];
  142. }
  143. else if(ball.y > HEIGHT*SCALE){
  144. ball.slope[1] *= -1;
  145. ball.y += ball.slope[1];
  146. }
  147. else if(ball.x < 0){
  148. ball.slope[0] *= -1;
  149. ball.x += ball.slope[0];
  150. }
  151. else if(ball.y < 0){
  152. ball.slope[1] *= -1;
  153. ball.y += ball.slope[1];
  154. }
  155.  
  156.  
  157.  
  158. }
  159.  
  160.  
  161. }
  162. public class Listener implements KeyListener {
  163. int counter = 0 ;
  164. public void keyTyped(KeyEvent e) {
  165.  
  166. }
  167.  
  168. public void keyPressed(KeyEvent e) {
  169. counter++;
  170. if(KeyEvent.getKeyText(e.getKeyCode())== "Down" && Paddle <= HEIGHT*SCALE-30 && counter%60 == 0){
  171. Paddle++;
  172. counter= 0;
  173. }
  174. else if(KeyEvent.getKeyText(e.getKeyCode())== "Up" && Paddle >= 0 && counter%60 == 0){
  175. Paddle--;
  176. counter=0;
  177. }
  178.  
  179.  
  180. }
  181. public void keyReleased(KeyEvent e) {
  182.  
  183.  
  184. }
  185.  
  186. }
  187. public void render(){
  188. BufferStrategy bs = getBufferStrategy();
  189. addKeyListener(listener);
  190. setFocusable(true);
  191. if(bs == null){
  192. createBufferStrategy(3);
  193. return;
  194. }
  195. path();
  196. Graphics g2d = bs.getDrawGraphics();
  197. if(running){
  198. g2d.setColor(Color.decode("#333333"));
  199. g2d.fillRect(0,0,getWidth(),getHeight());
  200. g2d.setColor(Color.decode("#ffffff"));
  201. if(ball.slope[0] != 0)
  202. g2d.fillOval(ball.x, ball.y, 10, 10);
  203. g2d.fillRect(0,Paddle,10,40);
  204. }
  205. else{
  206. g2d.setColor(Color.decode("#ffffff"));
  207. g2d.drawString("GAME OVER", WIDTH*SCALE/2, HEIGHT*SCALE/2);
  208. stop();
  209. }
  210. g2d.dispose();
  211. bs.show();
  212. }
  213.  
  214. @Override
  215. public void actionPerformed(ActionEvent e) {
  216. // TODO Auto-generated method stub
  217.  
  218. }
  219. @Override
  220. public void keyPressed(KeyEvent e) {
  221. // TODO Auto-generated method stub
  222.  
  223. }
  224. @Override
  225. public void keyReleased(KeyEvent e) {
  226. // TODO Auto-generated method stub
  227.  
  228. }
  229. @Override
  230. public void keyTyped(KeyEvent e) {
  231. // TODO Auto-generated method stub
  232.  
  233. }
  234.  
  235.  
  236.  
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement