Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Mark_1;
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import javax.swing.JFrame;
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.event.*;
- import javax.swing.*;
- public class Frame extends Canvas
- implements Runnable,
- KeyListener,
- ActionListener{
- private static final long serialVersionUID = 1L;
- public static final int WIDTH = 160;
- public static final int HEIGHT = WIDTH/12*9;
- public static final int SCALE = 3;
- public static final String NAME = "FRAME";
- private JFrame frame;
- public boolean running = false;
- public int tickCount = 0;
- public Listener listener = new Listener();
- private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
- private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
- public static Ball ball = new Ball();
- public static int Paddle = HEIGHT*SCALE/2;
- public Frame(){
- setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
- setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
- setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
- frame = new JFrame(NAME);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.add(this, BorderLayout.CENTER);
- frame.pack();
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- public synchronized void start() {
- running = true;
- new Thread(this).start();
- }
- public synchronized void stop(){
- running = false;
- }
- public void run() {
- long lastTime = System.nanoTime();
- double nsPerTick = 1000000000D/60;
- int frames = 0;
- int ticks = 0;
- long lastTimer = System.currentTimeMillis();
- double delta = 0;
- while(running){
- long now = System.nanoTime();
- delta +=(now- lastTime)/nsPerTick;
- lastTime = now;
- boolean shouldRender = false;
- while(delta>= 1){
- ticks++;
- tick();
- delta -= 1;
- shouldRender = true;
- }
- if(shouldRender){
- frames++;
- render();
- }
- if(System.currentTimeMillis() - lastTimer > 1000){
- lastTimer += 1000;
- System.out.println(frames+","+ticks);
- System.out.println(ball.slope[0]+","+ball.slope[1]);
- frames = 0;
- ticks = 0;
- }
- }
- }
- public void tick(){
- tickCount++;
- }
- public void path(){
- if(tickCount == 60){
- ball = new Ball();
- ball.x = (int) (Math.random()*161);
- ball.y = (int) (Math.random()*161);
- ball.slope[0] = (int) (Math.random()*3)+1;
- ball.slope[1] = (int) (Math.random()*3)+1;
- //System.out.println(ball.x+","+ball.y+","+ball.slope[0]+","+ball.slope[1]);
- }
- else{
- //System.out.println(ball.x+","+ball.y+","+ball.slope[0]+","+ball.slope[1]);
- ball.x += ball.slope[0];
- ball.y += ball.slope[1];
- if(ball.x > WIDTH*SCALE-1 || ball.x < 0 || ball.y > HEIGHT*SCALE-1 || ball.y < 0){
- int dir = (int) (Math.random()*4)+1;
- int dirI = -1;
- if(dir == 1){
- if(ball.slope[0]>0)
- dirI = 1;
- ball.slope[0] = (int) ((Math.random()*3)+1)*dirI;
- }
- else if(dir == 1){
- if(ball.slope[1]>0)
- dirI = 1;
- ball.slope[1] = (int) ((Math.random()*3)+1)*dirI;
- }
- }
- if(ball.x > WIDTH*SCALE){
- ball.slope[0] *= -1;
- ball.x += ball.slope[0];
- }
- else if(ball.y > HEIGHT*SCALE){
- ball.slope[1] *= -1;
- ball.y += ball.slope[1];
- }
- else if(ball.x < 0){
- ball.slope[0] *= -1;
- ball.x += ball.slope[0];
- }
- else if(ball.y < 0){
- ball.slope[1] *= -1;
- ball.y += ball.slope[1];
- }
- }
- }
- public class Listener implements KeyListener {
- int counter = 0 ;
- public void keyTyped(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- counter++;
- if(KeyEvent.getKeyText(e.getKeyCode())== "Down" && Paddle <= HEIGHT*SCALE-30 && counter%60 == 0){
- Paddle++;
- counter= 0;
- }
- else if(KeyEvent.getKeyText(e.getKeyCode())== "Up" && Paddle >= 0 && counter%60 == 0){
- Paddle--;
- counter=0;
- }
- }
- public void keyReleased(KeyEvent e) {
- }
- }
- public void render(){
- BufferStrategy bs = getBufferStrategy();
- addKeyListener(listener);
- setFocusable(true);
- if(bs == null){
- createBufferStrategy(3);
- return;
- }
- path();
- Graphics g2d = bs.getDrawGraphics();
- if(running){
- g2d.setColor(Color.decode("#333333"));
- g2d.fillRect(0,0,getWidth(),getHeight());
- g2d.setColor(Color.decode("#ffffff"));
- if(ball.slope[0] != 0)
- g2d.fillOval(ball.x, ball.y, 10, 10);
- g2d.fillRect(0,Paddle,10,40);
- }
- else{
- g2d.setColor(Color.decode("#ffffff"));
- g2d.drawString("GAME OVER", WIDTH*SCALE/2, HEIGHT*SCALE/2);
- stop();
- }
- g2d.dispose();
- bs.show();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyPressed(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement