- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想阻止我的形状向左或向右移动太远以至于超出屏幕,但我在 KeyHandler 中实现的 outOfBounds 方法收到此错误:
发现 1 个错误:文件:.../Tetris.java [行:46]错误:类型 package.Tetromino 未定义方法 outOfBounds(int, int)
这是我的类(class)
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class BigBang extends JComponent implements KeyListener, ActionListener {
Timer timer;
World world;
int score = 0;
JLabel statusbar = new JLabel("Score: " + score);
BigBang(int delay, World world) {
timer = new Timer(delay, this);
this.world = world;
JFrame frame = new JFrame("Tetris");
frame.add(statusbar, BorderLayout.SOUTH);
statusbar.setVisible(true);
frame.getContentPane().add(this );
frame.addKeyListener(this);
frame.setVisible(true);
frame.setSize(Tetris.COLUMNS * Block.SIZE + 27, Tetris.ROWS * Block.SIZE + 60);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void start() {
timer.start();
}
BigBang(World world) {
this(1000, world);
}
public void paintComponent(Graphics g) {
world.draw(g);
}
public void actionPerformed(ActionEvent e) {
world.update();
if (world.hasEnded())
timer.stop();
this.repaint();
score += 5;
statusbar.setText("Score: " + score);
}
public void keyPressed(KeyEvent e) {
world.keyPressed(e);
this.repaint();
}
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}
__
import java.awt.*;
class Block {
final static int SIZE = 20;
int x, y;
Color color;
Block(int x, int y, Color c) {
this.x = x;
this.y = y;
this.color = c;
}
void outOfBounds(int left, int right)
{
if (this.x > right)
this.x = right;
else if (this.x < left)
this.x = left;
}
void draw(Graphics g) {
int xp = this.x * SIZE,
yp = this.y * SIZE;
g.setColor(this.color);
g.fillRect(xp + 85, yp, SIZE, SIZE);
g.setColor(Color.BLACK);
g.drawRect(xp + 85, yp, SIZE, SIZE);
}
void move(int dx, int dy) {
this.x += dx;
this.y += dy;
}
public String toString() {
return "[" + this.x + ", " + this.y + "]";
}
void rotateCW(Point c) {
int newX, newY;
newX = c.x + c.y - this.y;
newY = c.y + this.x - c.x;
this.x = newX;
this.y = newY;
}
boolean equals(Block other) {
return this.x == other.x && this.y == other.y;
}
void setColor(Color c) {
this.color = c;
}
}
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
void move(int dx, int dy) {
this.x += dx;
this.y += dy;
}
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
}
__
import java.awt.*;
import java.util.*;
class SetOfBlocks extends ArrayList<Block> {
void draw(Graphics g) {
for (Block b : this)
b.draw(g);
}
void move(int dx, int dy) {
for (Block b : this)
b.move(dx, dy);
}
void rotateCW(Point center) {
for (Block b : this)
b.rotateCW(center);
}
boolean contains(Block block) {
if (this.size() != 0)
for (Block b : this)
if (b.equals(block))
return true;
return false;
}
boolean subset(SetOfBlocks blocks) {
if (this.size() != 0)
for (Block b : this)
if (! blocks.contains(b))
return false;
return true;
}
boolean equals(SetOfBlocks blocks) {
return this.subset(blocks) && blocks.subset(this);
}
SetOfBlocks intersect(SetOfBlocks other) {
SetOfBlocks result = new SetOfBlocks();
for (Block b : this)
if (other.contains(b))
result.add(b);
return result;
}
SetOfBlocks union(SetOfBlocks other) {
SetOfBlocks result = new SetOfBlocks();
for (Block b : this)
if (! result.contains(b) )
result.add(b);
for (Block b : other)
if (! result.contains(b) )
result.add(b);
return result;
}
int count() {
return this.size();
}
int maxY() {
int result = 0;
for (Block b : this)
if (b.y > result)
result = b.y;
return result;
}
int minX() {
int result = Tetris.COLUMNS;
for (Block b : this)
if (b.x < result)
result = b.x;
return result;
}
int maxX() {
int result = 0;
for (Block b : this)
if (b.x > result)
result = b.x;
return result;
}
void changeColor(Color color) {
for (Block b : this)
b.setColor(color);
}
boolean overflow() {
for (Block b : this)
if (b.y <= 0)
return true;
return false;
}
SetOfBlocks row(int row) {
SetOfBlocks result = new SetOfBlocks();
for (Block b : this)
if (b.y == Tetris.ROWS - row)
result.add(b);
return result;
}
boolean fullRow(int row) {
int a = this.row(row).count();
int b = Tetris.COLUMNS;
// System.out.println( a + " vs " + b );
return a == b;
}
void eliminateRow(int i) { // i == 0 indicates bottom row
SetOfBlocks row = new SetOfBlocks();
for (Block b : this)
if (b.y == (Tetris.ROWS - i))
row.add(b);
for (Block b : row)
this.remove(b);
for (Block b : this)
if (b.y < (Tetris.ROWS - i))
b.move(0, 1);
}
void eliminateFullRows() {
for (int i = 0; i < Tetris.ROWS; ) {
if (this.fullRow(i)) {
// System.out.println("Eliminating: " + i);
this.eliminateRow(i);
} else i++;
}
}
}
__
import java.awt.*;
import java.awt.event.*;
class Tetris implements World {
static final int ROWS = 20;
static final int COLUMNS = 10;
Tetromino t;
SetOfBlocks blocks;
Tetris(Tetromino t, SetOfBlocks s) {
this.t = t;
this.blocks = s;
}
public void draw(Graphics g) {
t.draw(g);
blocks.draw(g);
g.drawRect(5, 0, Tetris.COLUMNS * Block.SIZE, Tetris.ROWS * Block.SIZE);
for (int i = 0; i < COLUMNS; i++)
g.drawLine((20*i) + 5, 400, (20*i) + 5, 0);
for (int i = 0; i < ROWS; i++)
g.drawLine(5, 20*i, 205, 20*i);
}
public void update() {
if (this.landed())
this.touchdown();
else
this.t.move(0, 1);
}
public boolean hasEnded() { return false; }
public void keyPressed(KeyEvent e) {
if (this.landed())
this.touchdown();
int keycode = e.getKeyCode();
switch (keycode) {
case KeyEvent.VK_LEFT:
{ this.t.move(-1, 0);
this.t.outOfBounds(0, 400);}
break;
case KeyEvent.VK_RIGHT:
{ this.t.move( 1, 0); }
break;
case KeyEvent.VK_DOWN:
{ this.t.move( 0, 1); }
break;
case 'r':
{ this.t.rotateCW(); }
break;
case KeyEvent.VK_SPACE:
{ this.jumpDown(); }
break;
}
}
public static void main(String[] args) {
BigBang game = new BigBang(new Tetris(Tetromino.sQuare(), new SetOfBlocks()));
game.start();
}
public void touchdown() {
this.blocks = this.blocks.union(this.t.blocks);
this.blocks.eliminateFullRows();
this.t = Tetromino.pickRandom();
}
void jumpDown() {
if (! this.landed()) {
this.t.move(0, 1);
this.jumpDown();
}
}
boolean landedOnBlocks() {
this.t.move(0, 1);
if (this.t.overlapsBlocks(this.blocks)) {
this.t.move(0, -1);
return true;
} else {
this.t.move(0, -1);
return false;
}
}
boolean landedOnFloor() {
return this.t.blocks.maxY() == Tetris.ROWS - 1;
}
boolean landed() {
return this.landedOnFloor() || this.landedOnBlocks();
}
}
__
class Tetromino {
static Tetromino sQuare() {
return new Tetromino(new Point(0, -1),
makeBlocks(new int[] {0, -1, 0, -2, 1, -1, 1, -2},
Color.GREEN ));
}
static Tetromino liNe() {
return new Tetromino(new Point(1, -1),
makeBlocks(new int[] {0, -1, 1, -1, 2, -1, 3, -1},
Color.BLUE ));
}
static Tetromino l() {
return new Tetromino(new Point(1, -1),
makeBlocks(new int[] {0, -1, 1, -1, 2, -1, 2, -2},
Color.MAGENTA ));
}
static Tetromino MirroredL() {
return new Tetromino(new Point(1, -1),
makeBlocks(new int[] {0, -1, 1, -1, 2, -1, 0, -2},
Color.CYAN ));
}
static Tetromino t() {
return new Tetromino(new Point(1, -1),
makeBlocks(new int[] {0, -1, 1, -1, 2, -1, 1, -2},
Color.ORANGE ));
}
static Tetromino s() {
return new Tetromino(new Point(1, -1),
makeBlocks(new int[] {0, -1, 1, -1, 1, -2, 2, -2},
Color.RED ));
}
static Tetromino z() {
return new Tetromino(new Point(1, -2),
makeBlocks(new int[] {0, -2, 1, -2, 1, -1, 2, -1},
Color.PINK ));
}
static SetOfBlocks makeBlocks(int[] c, Color color) {
SetOfBlocks a = new SetOfBlocks();
a.add(new Block( c[0], c[1], color));
a.add(new Block( c[2], c[3], color));
a.add(new Block( c[4], c[5], color));
a.add(new Block( c[6], c[7], color));
return a;
}
Point center;
SetOfBlocks blocks;
Tetromino(Point center, SetOfBlocks blocks) {
this.center = center;
this.blocks = blocks;
}
void draw(Graphics g) {
this.blocks.draw(g);
}
void move(int dx, int dy) {
this.center.move(dx, dy);
this.blocks.move(dx, dy);
}
public String toString() {
return this.center + " " + this.blocks ;
}
void rotateCW() {
this.blocks.rotateCW(this.center);
}
boolean overlapsBlocks(SetOfBlocks blocks) {
return this.blocks.intersect(blocks).count() > 0;
}
void changeColor(Color color) {
this.blocks.changeColor(color);
}
static Tetromino pickRandom() {
int value = (int)(Math.random() * 7);
if (value == 0) return Tetromino.sQuare();
else if (value == 1) return Tetromino.liNe();
else if (value == 2) return Tetromino.l();
else if (value == 3) return Tetromino.MirroredL();
else if (value == 4) return Tetromino.t();
else if (value == 5) return Tetromino.s();
else return Tetromino.z();
}
}
__
import java.awt.*;
import java.awt.event.*;
interface World {
public void draw(Graphics g);
public void update();
public boolean hasEnded();
public void keyPressed(KeyEvent e);
}
最佳答案
错误消息告诉您确切的问题:
1 error found: File: .../Tetris.java [line: 46] Error: The method outOfBounds(int, int) is undefined for the type package.Tetromino
当您尝试从 KeyHandler
调用 outOfBounds(int,int)
方法时,无法找到该方法
这是您尝试调用它的 case 语句的片段。
case KeyEvent.VK_LEFT:
{ this.t.move(-1, 0);
this.t.outOfBounds(0, 400);}
break;
您正在对命名不当的 t
变量调用该方法(变量名称应该是描述性的,并且似乎是导致您的问题的部分原因),该变量是 Tetromino
类类型的变量。您的 Tetromino
类显然没有定义名为 outOfBounds(int, int)
的方法。因此出现错误。
您的 outOfBounds(int, int)
在您的 Blocks
类上明确定义,但作为 私有(private)
方法,意味着只有 Blocks
类自身可以访问它。
关于java - 阻止物体在俄罗斯方 block 游戏中越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41110329/
大家好,本文实现了相机碰撞检测,使相机不穿墙壁、物体,并给出了思路和代码,感谢大家~ 关键词:数字孪生、three.js、Web3D、WebGL、相机碰撞、游戏相机 我正在承接Web3D数字孪生项
我有一个大小为 12*30 的字符串的图像。我想创建一个动画,让它给人一种拉伸(stretch)字符串的感觉。我通过缩放图像来做到这一点,但我面临的问题是缩放图像没有发生碰撞。它仅出现在原始图像大小的
我的对象列表是如此初始化: $( function() { var $container = $('div.hikashop_products'); $container.isotop
我听说 swing 默认情况下是双缓冲的。我不想让 Swing 双缓冲。我正在使用双缓冲,我想添加一些 Swing 对象(现在只是添加到 JPanel 中的 JButton,然后再添加到 JFrame
几天来我一直在思考最好的解决方案,但似乎无法找到正确的想法。 我有一 block (物体),我想将它们放入尽可能小的空间中。我最终寻找的是这样的东西 http://i.stack.imgur.com/
我的纹理不仅仅是一个盒子或圆形,我的 body 需要与这个形状相同,所以我想结合多个 body 来达到我想要的形状,这甚至可能吗?或者有更好的方法吗?我正在使用带有 libgdx 框架的 java。
我遇到的情况是,我有很多计算机并且需要有唯一的 ID。 他们会通过 API 请求发送其 uniqueId。该对象看起来像 class ID { long timestamp; int id; }
我正在尝试检测一张卡片,但问题是有时图像不好并且有多个背景,如下所示: 没有很好地定义边缘 ![没有很好地定义边缘][1] 示例背景 ![示例背景][2] 我这样做了: gray = cv2.cvtC
我正在尝试从仅包含一辆车和简单背景的图像中分割汽车,如 但是我从我的实现中得到的是这个 和 分别 但它非常容易处理几乎已经分割的图像,例如。 给出类似 的结果 我使用的代码是 import cv2 i
我正在开发一个项目,在该项目中我从另一个对象/函数中引用一个变量。然而我总是返回 false。我不确定我是否正确调用它。 这是验证函数: app.validation = function(){
数组只是伪装的对象吗?为什么/为什么不呢?他们以什么方式(这样/不是)? 我一直认为 JS 中的数组和对象本质上是相同的,主要是因为访问它们是相同的。 var obj = {'I': 'me'}; v
我正在使用 PlayN 构建一个涉及石头的游戏,用户必须在物理世界中移动(通过重力等)。我希望用户能够使用触摸板直接操纵石头,并通过以下方式给它们一个速度拖拽并扔掷它们。 现在我有一个实现,其中每个石
http://jsfiddle.net/goldrunt/jGL84/42/这是来自这个 JS fiddle 的第 84 行。通过取消注释第 141-146 行,可以对球应用 3 种不同的效果。 'b
我学习Linux平台下的OpenGL。最近,我尝试使用 glutBitmapCharacter() 创建的文本作为 glu 或 glut 提供的一些二次对象的纹理。但是,glutBitmapChara
我正在使用 AndEngine 创建一个带有 box2d 扩展名的游戏。我想实现一个条件,当两个物体碰撞时,它们应该被移除或重生。最初我尝试使用 if(sprite1.collidesWith(spr
我制作了一个小型 3d 引擎。 但我在旋转功能方面遇到了一些问题。它们使物体不时拉伸(stretch)。这是数学: this.rotateX = function(angle) { var c
我在 Canvas 上制作了一个矩形 mask ,我需要 mask 外的任何东西都具有 0.8 的不透明度,因此 mask 外的所有对象都被视为不透明请看一下 fiddle 。 http://jsfi
我是 Direct3D 的新手,我在一个项目中从网络摄像头拍照并在它前面绘制一些 3D 对象。 我能够使用正交投影将网络摄像头图像渲染为背景。 //init matrix D3DXMatri
我是一名优秀的程序员,十分优秀!