gpt4 book ai didi

java - 当x和y位置进入另一个位置时如何执行操作?

转载 作者:行者123 更新时间:2023-12-01 12:58:20 26 4
gpt4 key购买 nike

我正在尝试制作一个有趣的小赛车游戏。到目前为止,我有两个成功移动的矩形,并且我有一个 map 设置供它们竞速通过。我的 map 也是由矩形组成的。之前我犯了一个错误,没有给我的两个赛车手一个具体的客观名称。所以它们只是移动的两个位置。现在我想做的是,让矩形墙实际上成为墙,这样它们就不会只是穿过它们。我听说如果我把墙做成阵列(不知道如何),我就可以掩盖我的错误,这样它们就不会穿过它们。它是否正确?还有其他方法可以做到这一点吗?到目前为止,情况如下: enter image description here

谢谢,这是我的代码。第一类是框架和黑色矩形的信息。第二类是蓝色矩形和墙壁的信息。

头等舱:

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);

}
}

最佳答案

How would I list rectangles?

请参阅 Custom Painting Approaches 中的 DrawOnCompnent 示例。它向您展示了如何从 ArrayList 进行绘制。

关于java - 当x和y位置进入另一个位置时如何执行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23717475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com