Created
May 25, 2019 21:00
-
-
Save ahmednasserpro/19963932ca0315038c8cbf4154d64e73 to your computer and use it in GitHub Desktop.
HitBalloonGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class HitBalloonGame extends JFrame { | |
private ShootPanel p = new ShootPanel(); | |
HitBalloonGame() { | |
add(p); | |
p.setFocusable(true); | |
} | |
private class ShootPanel extends JPanel { | |
private int armXDegree = 0; | |
private boolean isBulletFired = false; | |
private int xBullet = 0; | |
private int yBullet = 0; | |
private int position = 0; | |
private Timer timer = new Timer(100, new TimerListener()); | |
private int balloonX = 0; | |
private int balloonY = 0; | |
private boolean isBalloonDrawed = false; | |
private boolean isOverlaps = false; | |
ShootPanel() { | |
// Add left and right key listeners | |
addKeyListener(new KeyAdapter() { | |
@Override | |
public void keyPressed(KeyEvent e) { | |
if (e.getKeyCode() == KeyEvent.VK_LEFT) { | |
if (armXDegree > -40) { | |
armXDegree -= 5; | |
repaint(); | |
} | |
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { | |
if (armXDegree < 40) { | |
armXDegree += 5; | |
repaint(); | |
} | |
} else if (e.getKeyCode() == KeyEvent.VK_UP) { | |
if (!isBulletFired) { | |
position = armXDegree; | |
} | |
isBulletFired = true; | |
timer.start(); | |
} | |
} | |
}); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
// Draw the arm | |
Polygon polygon = new Polygon(); | |
polygon.addPoint(getWidth() / 2, getHeight()); | |
polygon.addPoint(getWidth() / 2 + 10, getHeight()); | |
polygon.addPoint(getWidth() / 2 + armXDegree + 10, getHeight() - 40); | |
polygon.addPoint(getWidth() / 2 + armXDegree, getHeight() - 40); | |
g.fillPolygon(polygon); | |
if (distance(balloonX, balloonY, getWidth() / 2 + position + xBullet, | |
getHeight() - 50 + yBullet) <= 20) { | |
isOverlaps = true; | |
} | |
// Check if the bullet overlaps with the balloon | |
if (isOverlaps) { | |
g.drawOval(balloonX - 10, balloonY - 10, 20, 20); | |
g.drawOval(balloonX + 10, balloonY + 10, 20, 20); | |
g.drawOval(balloonX - 15, balloonY - 20, 20, 20); | |
} else { | |
// Draw Bullet if it is fired | |
if (isBulletFired) { | |
g.fillOval(getWidth() / 2 + position + xBullet, getHeight() - 50 + yBullet, 10, 10); | |
} | |
// Draw the balloon | |
if (!isBalloonDrawed) { | |
balloonX = (int) (Math.random() * getWidth()); | |
balloonY = getRandomY(getHeight()); | |
isBalloonDrawed = true; | |
} | |
g.drawOval(balloonX, balloonY, 30, 30); | |
} | |
} | |
private int distance(int x1, int y1, int x2, int y2) { | |
return (int) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); | |
} | |
private int getRandomY(int height) { | |
int randomY = (int) (Math.random() * height); | |
while (true) { | |
if (randomY < height - 80) | |
break; | |
randomY = (int) (Math.random() * height); | |
} | |
return randomY; | |
} | |
private class TimerListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (position < 0) { | |
xBullet -= 5; | |
} else if (position == 0) { | |
xBullet -= 0; | |
} else { | |
xBullet += 5; | |
} | |
yBullet -= 5; | |
if (!isOverlaps) | |
repaint(); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
JFrame frame = new HitBalloonGame(); | |
frame.setSize(300, 200); | |
frame.setLocationRelativeTo(null); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment