Source code
package bouncegame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.Timer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GamePlay extends JPanel implements ActionListener,KeyListener{
private Timer timer;
private boolean gameplay =false;
private int score=0;
private int delay=10;
private int ballposx=200;
private int ballposy=200;
private int ballxdir=-1;
private int ballydir=-3;
Timer tm;
private int playerx=440;
GamePlay(){
this.requestFocusInWindow(true);
this.setFocusable(true);
setFocusTraversalKeysEnabled(false);
this.addKeyListener(this);
tm=new Timer(delay,this);
tm.start();
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
//background
g.setColor(Color.BLACK);
g.fillRect(3, 1, 600, 600);
//border
g.setColor(Color.blue);
g.fillRect(0, 0, 3, 600);
g.fillRect(0, 0, 600, 3);
g.fillRect(600, 0, 10,600 );
//paddle
g.setColor(Color.GREEN);
g.fillRect(playerx,560 , 50, 8);
//ball
g.setColor(Color.ORANGE);
g.fillOval(ballposx, ballposy, 10, 10);
//score
g.setColor(Color.WHITE);
g.setFont(new Font("sarif",Font.BOLD,20));
g.drawString("Score "+score, 490, 45);
//game end
if(ballposy>600) {
gameplay=false;
ballxdir=0;
ballydir=0;
g.setColor(Color.RED);
g.setFont(new Font("sarif",Font.BOLD,20));
g.drawString("Game Over ", 300, 300);
g.setFont(new Font("sarif",Font.BOLD,20));
g.drawString("press Enter to resume ", 250, 330);
}
g.dispose();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(gameplay) {
if(new Rectangle(ballposx,ballposy,12,12).intersects(new Rectangle(playerx,540,50,8)) )
{
ballydir=-ballydir;
score+=10;
}
ballposx+=ballxdir;
ballposy+=ballydir;
System.out.println(ballposx+" "+ballposy);
if(ballposx<0) {
ballxdir=-ballxdir;
}
if(ballposy<0) {
ballydir=-ballydir;
}
if(ballposx>580) {
ballxdir=-ballxdir;
}
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
if(playerx>=540) {
playerx=540;
}
else
{
moveRight();
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT) {
if(playerx<=3) {
playerx=3;
}
else
{
moveLeft();
}
}
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
if(gameplay==false) {
gameplay=true;
ballposx=200;
ballposy=200;
ballxdir=-1;
ballydir=-3;
score=0;
repaint();
}
}
}
public void moveLeft() {
// TODO Auto-generated method stub
gameplay=true;
playerx-=7;
}
public void moveRight() {
// TODO Auto-generated method stub
gameplay=true;
playerx+=7;
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
No comments:
Post a Comment