-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
144 lines (131 loc) · 3.47 KB
/
Board.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.mjcareers.gaming;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics; // awt package class
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
import com.mjcareers.gaming.sprites.Enemy;
import com.mjcareers.gaming.sprites.Player;
public class Board extends JPanel
{
Timer timer;
BufferedImage backgroundImage; // to store img
Player player;
Enemy enemies[] = new Enemy[3];
public Board()
{
setSize(1300, 700);
loadBackgroundImage();
player = new Player();
loadEnemies();
gameLoop();
setFocusable(true); // to get focus on board
bindEvents();
}
private void gameOver(Graphics pen)
{
if(player.outOfScreen())
{
pen.setFont(new Font("times",Font.BOLD,30));
pen.setColor(Color.RED);
pen.drawString("Game WIN", 1300/2, 700/2);
timer.stop();
return;
}
for(Enemy enemy : enemies)
{
if(isCollide(enemy))
{
pen.setFont(new Font("times",Font.BOLD,30));
pen.setColor(Color.RED);
pen.drawString("Game over", 1300/2, 700/2);
timer.stop();
}
}
}
private boolean isCollide(Enemy enemy)
{
int xDistance = Math.abs(player.x - enemy.x);
int yDistance = Math.abs(player.y - enemy.y);
int maxH = Math.max(player.h, enemy.h);
int maxW = Math.max(player.w, enemy.w);
return xDistance <= maxW-80 && yDistance <= maxH-80;
}
private void bindEvents() {
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT) { // on right arrow key press only move
player.speed = 20;
// TODO Auto-generated method stub
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
player.speed = -20;
}
}
@Override
public void keyReleased(KeyEvent e) {
player.speed = 0;
// TODO Auto-generated method stub
}
}
);
}
private void loadEnemies()
{
int x = 300;
int speed = 5;
int gap = 300;
for(int i = 0;i<enemies.length;i++)
{
enemies[i] = new Enemy(x, speed);
x = x + gap;
speed = speed + 5;
}
}
private void gameLoop()
{
timer = new Timer(50,(e)->repaint()); // timer using the thread concept
timer.start();
}
private void loadBackgroundImage() {
try {
backgroundImage=ImageIO.read(Board.class.getResource("game-bg.jpeg"));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("background Image Not Found.....");
System.exit(1); // to stop the operation exit 0 means no problem , exit 1 there exist some problem
e.printStackTrace();
}
}
private void printEnemies(Graphics pen)
{
for(Enemy enemy : enemies)
{
enemy.draw(pen);
enemy.move();
}
}
// paintcomponent overridden
@Override
public void paintComponent(Graphics pen) // reference variable pen of graphics , after constructer all this is created
{
// all printing logic will be here
super.paintComponent(pen); // clean up canvas
pen.drawImage(backgroundImage,0,0,1500, 920,null);// left corner to right corner , image observer null as not needed
player.draw(pen);
player.move();
printEnemies(pen);
gameOver(pen);
}
}