今年/去年,我在学校开设了编程课程,作为期末考试,我们应该做一个小型而简单的游戏。从去年8月到大约两个月前,我们在使用Java之前在Dr.Racket中进行了编码。由于我们每周只有大约三个小时的学习时间,因此我在Java方面还不是特别出色。我的游戏基座有一个球拍和球,它们在窗口中绘制。在进入积木领域的过程中,我完全陷入了将单个积木作为对象的困境,以使碰撞检测功能对我有用。
对于碰撞检测的一些帮助也将不胜感激。
我在程序中画出了一个砖块字段,但是由于它不是数组,所以我的老师说我应该考虑创建一个数组,而不是为我简化事情。我以前使用此代码来绘制砖块字段:
class Bricks{
final int rows = 4;
final int columns = 9;
final int bx = 0;
final int by = 0;
final int width = 80;
final int height = 15;
final int gap = 2;
final Color color = Color.darkGray;
void renderOn(Graphics g){
int x = bx, y = by;
g.setColor(color);
for(int row = 1; row <= rows; row++){
for(int col = 1; col <= columns; col++){
g.fillRect(x, y, width, height);
x += width + gap;
}
if(row%2 == 0)
x = bx;
else
x = bx - width/2 - gap/2;
y += height + gap;
}
}
现在,我只需要“按一下”按钮,即可正确进行游戏开发,以便在课程中获得成绩。
完整的游戏代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Main extends JPanel {
static int WIDTH = 640;
static int HEIGHT = 400;
List<Ball> theBalls = new ArrayList<>();
Padel padel = new Padel(WIDTH/2 -40 , HEIGHT-15, 10, 80, 12, Color.darkGray);
Bricks bricks = new Bricks();
Antal antal = new Antal(0);
public static void main(String[] args) {
new Main();
}
Main() {
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
JFrame frame = new JFrame("Breakout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE && antal.bollar <1){
theBalls.add(new Ball(WIDTH/2 - 5, HEIGHT-30, 3, -3, 15, Color.red));
antal.bollar++;
}
if (e.getKeyCode() == KeyEvent.VK_ENTER && antal.bollar == 1){
theBalls.add(new Ball(WIDTH/2 - 5, HEIGHT-30, 3, -3, 15, Color.orange));
antal.bollar++;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT && padel.x > 0){
padel = padel.move_left();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT && padel.x < WIDTH-padel.width){
padel = padel.move_right();
}
}
});
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
Timer timer = new Timer(10, e -> this.repaint());
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
theBalls = theBalls.stream().map(b -> {
Ball newBall = b.move(getWidth(), getHeight());
newBall.renderOn(g);
return newBall;
}).collect(Collectors.toList());
padel.renderOn(g);
bricks.renderOn(g);
}
}
class Ball {
final int x, y, xdir, ydir, width;
final Color color;
Ball(int x, int y, int xdir, int ydir, int width, Color color) {
this.x = x;
this.y = y;
this.xdir = xdir;
this.ydir = ydir;
this.width = width;
this.color = color;
}
int nextXdir(int width) {
if (x < 0 && xdir < 0 || x > width - this.width && xdir > 0)
return -xdir;
else
return xdir;
}
int nextYdir(int height) {
if (y < 0 && ydir < 0 || y > height - this.width && ydir > 0)
return -ydir;
else
return ydir;
}
Ball move(int width, int height){
return new Ball(
this.x + this.xdir,
this.y + this.ydir,
nextXdir(width),
nextYdir(height),
this.width,
this.color);
}
void renderOn(Graphics g) {
g.setColor(color);
g.fillOval(x, y, width, width);
}
}
class Padel {
final int x, y, speed, width, height;
final Color color;
Padel(int x, int y, int speed, int width, int height, Color color){
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
this.color = color;
}
Padel move_left(){
return new Padel(
this.x - this.speed,
this.y,
this.speed,
this.width,
this.height,
this.color);
}
Padel move_right(){
return new Padel(
this.x + this.speed,
this.y,
this.speed,
this.width,
this.height,
this.color);
}
void renderOn(Graphics g){
g.setColor(color);
g.fillRect(x, y, width, height);
}
}
class Antal{
int bollar;
Antal(int bollar){
this.bollar = bollar;
}
}
class Bricks {
final int rows = 4;
final int columns = 9;
final int bx = 0;
final int by = 0;
final int width = 80;
final int height = 15;
final int gap = 2;
final Color color = Color.darkGray;
void renderOn(Graphics g) {
int x = bx, y = by;
g.setColor(color);
for (int row = 1; row <= rows; row++) {
for (int col = 1; col <= columns; col++) {
g.fillRect(x, y, width, height);
x += width + gap;
}
if (row % 2 == 0)
x = bx;
else
x = bx - width / 2 - gap / 2;
y += height + gap;
}
}
}