0
我正在試着做一個小型的賽車遊戲的樂趣。到目前爲止,我有兩個成功移動的矩形,並且我有一個地圖設置讓他們參與比賽。 我的地圖也是由矩形組成的。現在,我之前犯了一個錯誤,沒有給我的兩位選手一個特定的客觀名稱。所以他們只是兩個移動的地點。現在我想要做的是,使矩形牆實際上是牆壁,所以他們不只是通過它們。我聽說如果我把牆變成數組(不知道怎麼做),我可以掩蓋我的錯誤,所以他們不會通過它們。它是否正確?有沒有其他方法可以做到這一點? 這裏是如何看起來像至今: 如何在x和y位置進入另一個位置時執行動作?
謝謝你,這裏是我的代碼。 頭等艙是框架和黑色矩形的信息。 第二課是藍色矩形和牆壁的信息。
頭等艙:
import java.awt.BorderLayout;
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 javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MyGame extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(5, this);
int x = 0, y = 0, velx =0, vely =0, g = 0;
private Color color;
public MyGame() {
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(1300, 750);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillRect(x, y, 50, 30);
}
@Override
public void actionPerformed(ActionEvent e) {
if (x < 0) //stops us from going backwards past x = 0
{
velx = 0;
x = 0;
}
if (y < 0) //stops us from going to the sky
{
vely = 0;
y = 0;
}
if (y > 725) // stops us from going through the ground
{
vely = 0;
y = 725;
}
if (x > 1250) // stops us from going through the wall
{
velx = 0;
x = 1250;
}
x += velx;
y += vely;
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
{
if (code == KeyEvent.VK_DOWN) {
vely = 2; // removing velx = 0 allows us to go vertically and horizontlly at the same time
velx = 0;
}
if (code == KeyEvent.VK_UP) {
vely = -2; // same goes for here
velx = 0;
}
if (code == KeyEvent.VK_LEFT) {
vely = 0;
velx = -2;
}
{
if (code == KeyEvent.VK_RIGHT) {
vely = 0;
velx = 2;
}
}
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main (String arge[]){
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Incoming());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
二等:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Incoming extends MyGame {
private Color color;
int x = 0, y = 0;
int velx = 0, vely = 0;
public Incoming() {
color = Color.BLUE;
Rectangle rect = new Rectangle();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillRect(x, y, 50, 30);
g.setColor(Color.blue);
g.drawRect(0, 100, 80, 30);
g.drawRect(80, 100, 80, 30);
g.drawRect(160, 100, 80, 30);
g.drawRect(240, 100, 80, 30);
g.drawRect(320, 100, 80, 30);
g.drawRect(400, 100, 80, 30);
g.drawRect(480, 100, 80, 30);
g.drawRect(560, 100, 80, 30);
g.drawRect(640, 100, 80, 30);
g.drawRect(720, 100, 80, 30);
g.drawRect(800, 100, 80, 30);
g.drawRect(880, 100, 80, 30);
g.drawRect(960, 100, 80, 30);
g.drawRect(1040, 100, 80, 30);
g.drawRect(1040, 250, 80, 30);
g.drawRect(1120, 250, 80, 30);
g.drawRect(1200, 250, 80, 30);
g.drawRect(960, 250, 80, 30);
g.drawRect(880, 250, 80, 30);
g.drawRect(800, 250, 80, 30);
g.drawRect(720, 250, 80, 30);
g.drawRect(640, 250, 80, 30);
g.drawRect(560, 250, 80, 30);
g.drawRect(480, 250, 80, 30);
g.drawRect(400, 250, 80, 30);
g.drawRect(320, 250, 80, 30);
g.drawRect(240, 250, 80, 30);
g.drawRect(160, 250, 80, 30);
g.drawRect(1040, 400, 80, 30);
g.drawRect(960, 400, 80, 30);
g.drawRect(880, 400, 80, 30);
g.drawRect(800, 400, 80, 30);
g.drawRect(720, 400, 80, 30);
g.drawRect(640, 400, 80, 30);
g.drawRect(560, 400, 80, 30);
g.drawRect(480, 400, 80, 30);
g.drawRect(400, 400, 80, 30);
g.drawRect(320, 400, 80, 30);
g.drawRect(240, 400, 80, 30);
g.drawRect(160, 400, 80, 30);
g.drawRect(80, 400, 80, 30);
g.drawRect(0, 400, 80, 30);
g.drawRect(1040, 550, 80, 30);
g.drawRect(1120, 550, 80, 30);
g.drawRect(1200, 550, 80, 30);
g.drawRect(960, 550, 80, 30);
g.drawRect(880, 550, 80, 30);
g.drawRect(800, 550, 80, 30);
g.drawRect(720, 550, 80, 30);
g.drawRect(640, 550, 80, 30);
g.drawRect(560, 550, 80, 30);
g.drawRect(480, 550, 80, 30);
g.drawRect(400, 550, 80, 30);
g.drawRect(320, 550, 80, 30);
g.drawRect(240, 550, 80, 30);
g.drawRect(160, 550, 80, 30);
g.drawRect(1040, 550, 80, 30);
g.drawRect(960, 550, 80, 30);
g.drawRect(880, 550, 80, 30);
g.drawRect(800, 550, 80, 30);
g.drawRect(720, 550, 80, 30);
g.drawRect(640, 550, 80, 30);
g.drawRect(560, 550, 80, 30);
g.drawRect(480, 550, 80, 30);
g.drawRect(400, 550, 80, 30);
g.drawRect(320, 550, 80, 30);
g.drawRect(240, 550, 80, 30);
g.drawRect(160, 550, 80, 30);
}
@Override
public void actionPerformed(ActionEvent e) {
super.actionPerformed(e);
if (x < 0) //stops us from going backwards past x = 0
{
velx = 0;
x = 0;
}
if (y < 0) //stops us from going to the sky
{
vely = 0;
y = 0;
}
if (y > 725) // stops us from going through the ground
{
vely = 0;
y = 725;
}
if (x > 1250) // stops us from going through the wall
{
velx = 0;
x = 1250;
}
if (y < 0.1)
{
y = 50;
}
x += velx;
y += vely;
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int code = e.getKeyCode();
{
if (code == KeyEvent.VK_S) {
vely = 2; // removing velx = 0 allows us to go vertically and horizontlly at the same time
velx = 0;
}
if (code == KeyEvent.VK_W) {
vely = -2; // same goes for here
velx = 0;
}
if (code == KeyEvent.VK_A) {
vely = 0;
velx = -2;
}
{
if (code == KeyEvent.VK_D) {
vely = 0;
velx = 2;
}
}
}
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
}
希望它不會讓你失望或者太過氣餒:代碼有太多「不正確」,很難給出一個適合問答網站的答案。你可以從創建一個'List'開始,然後用這個對應於所有這些'drawRect'調用的矩形填充這個列表。這個列表可以用來代替'drawRect'調用(你可以用循環迭代列表,併爲列表中的每個元素調用'drawRect'),之後它可以用於碰撞檢測,使用' if(rectangle.intersects(playerRectangle))'。然後,你可以寫更多的焦點問題。 –
Marco13
我會如何列出矩形?你能不能更具體一點? – BasicProgrammer
'List rectangles = new ArrayList ()',then'rectangles.add(new Rectangle(0,100,80,30));'。 (這些矩形也可以在循環中創建*最後爲每一行)。但這些都是基礎知識,不確定是否適合在這裏解釋*這樣的事情...... –
Marco13