- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个游戏,其中用户必须匹配由正方形网格组成的随机生成的目标图像。用户可以选择一整行(左侧的圆形选择器)、一整列(顶部的圆形选择器)和一个单独的单元格,这算作一次移动。目前,我的程序允许我选择行、列和单元格(并取消选择它们),但我无法找到一种方法来填充 Canvas 顶部的颜色选择器中选定的单元格。我想知道是否有人可以给我任何关于如何实现这一目标的提示?
final color RED = #D12020;
final color BLUE = #515DD8;
final color GREEN = #21AF20;
final color YELLOW = #F5EF74;
final color ORANGE = #F59219;
final color PURPLE = #B219F5;
final color WHITE = #FFFFFF;
final int ROWS = 12;
final int COLUMNS = 8;
final int BLOCK_SIZE = 40;
int blockSize;
int spacing;
int cornerX;
int cornerY;
int size;
int space;
int cell = -1;
int col = -1;
int row = -1;
color[] colour = {RED, BLUE, GREEN, YELLOW, ORANGE, PURPLE, WHITE};
void setup() {
size(1000, 1000);
background(0);
cornerX = width/8;
cornerY = height/8;
size = height/33;
space = width/100;
targetImage();
}
void draw() {
//background(0);
colourCells();
grid();
selectCell();
columnSelectors();
rowSelectors();
counter();
}
//draws the colour selectors
void colourCells() {
int blockSize = width/20;
int spacing = width/200;
int colourBlocks = 7;
int squareX = blockSize;
for (int i=0; i < colourBlocks; i++) {
stroke(167);
fill(colour[i]);
rect(squareX, 0, blockSize, blockSize);
squareX += spacing+blockSize;
}
}
//draws the grid
void grid() {
cornerX = width/8;
cornerY = height/5;
noFill();
stroke(167);
strokeWeight(1);
for (int i = 0; i<ROWS; i++) {
cornerX = width/8;
for (int j = 0; j<COLUMNS; j++) {
rect(cornerX, cornerY, BLOCK_SIZE, BLOCK_SIZE);
cornerX += BLOCK_SIZE;
}
cornerY += BLOCK_SIZE;
}
}
//changes the selected cell stroke to red (or deselected cell outline back to grey)
void selectCell() {
cornerX = width/8;
cornerY = height/5;
if (cell != -1) {
stroke(RED);
rect(cornerX + (cell % COLUMNS) * BLOCK_SIZE, cornerY + (cell / COLUMNS) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
stroke(167);
}
}
//creates the circular column selectors
void columnSelectors() {
int columnX = (cornerX) + (size/2) + (space/2);
int columnY = (cornerY) - (size/2) - space;
noFill();
for ( int i = 0; i<COLUMNS; i++) {
if (col != -1 && col == i)
stroke(RED);
else
stroke(167);
ellipse(columnX, columnY, size, size);
columnX += size + space;
}
}
//creates the circular row selectors
void rowSelectors() {
int rowX = (cornerX) - (size/2) - space;
int rowY = (cornerY) + (size/2) + (space/2);
noFill();
for ( int j = 0; j<ROWS; j ++) {
if (row != -1 && row == j)
stroke(RED);
else
stroke(167);
ellipse(rowX, rowY, size, size);
rowY += size + space;
}
}
//creates the score counter at the bottom of canvas
void counter() {
float x = width/3;
float y = 4*height/5;
float boxLength = width/3;
float boxHeight = height/20;
int counter = 0;
fill(255);
rect(x, y, boxLength, boxHeight);
String counterText = "Num Moves: " + counter;
fill(0);
textSize(40);
text(counterText, x + (boxLength*1/14.0), y + (boxHeight*3/4.0));
}
//loads in a random target image
void targetImage() {
float y = height/5;
float x = width/2;
String [] file = {"target0.png", "target1.png", "target2.png", "target3.png", "target4.png"};
PImage target;// create a variable that can point to an off-screen buffer
String filename = file[int(random(0, 4))]; //"target" + int(random(0,4)) + ".png";// create filename
target = loadImage(filename);// load image file into off-screen buffer
image(target, x, y, width/3.125, height/2.08);// display the buffer on canvas at location x, y
}
//allows the user to select/deselect individual cells
int cellSelected() {
int x;
int y;
int xPos= mouseX - cornerX;
int yPos= mouseY - cornerY;
x = xPos/BLOCK_SIZE;
y = yPos/BLOCK_SIZE;
int num = x + (y*COLUMNS);
if (num == cell)
return -1;
else
return num;
}
//allows the user to select/deselect a column
int selectColumn() {
int x;
int xPos= mouseX - cornerX;
x = xPos/(size+space);
if (x == col)
return -1;
else
return x;
}
//allows the user to select/deselect a row
int selectRow() {
int y;
int yPos= mouseY - cornerY;
y = yPos/(size+space);
if (y == row)
return -1;
else
return y;
}
//mouse click functions for selecting cells/rows/columns/colour
void mouseClicked() {
spacing = width/200;
blockSize = width/20;
if (mouseX > cornerX && mouseY > cornerY && mouseX < cornerX + COLUMNS*BLOCK_SIZE
&& mouseY < cornerY + ROWS*BLOCK_SIZE) {
cell = cellSelected();
}
if (mouseX > cornerX && mouseY > (cornerY - (space + size)) && mouseX < cornerX + COLUMNS*BLOCK_SIZE
&& mouseY < cornerY) {
col = selectColumn();
}
if (mouseX > (cornerX - (space + size)) && mouseY > cornerY && mouseX < cornerX
&& mouseY < cornerY + ROWS*BLOCK_SIZE) {
row = selectRow();
}
if (mouseX < blockSize && mouseX > ((blockSize*colour.length) + (spacing*(colour.length - 1)))) {
for (int c = 0; c < colour.length; c++) {
if (get(mouseX, mouseY) == colour[c]) {
fill(colour[c]);
break;
}
}
}
}
/*
to do
fill colour
increse score counter
*/```
[1]: /image/eyX4g.png
最佳答案
我喜欢你的项目。我特别喜欢你的评论。到目前为止做得很好。我应该去 sleep ,但我会帮你的。而且,让我们面对现实吧,无论如何我都不擅长 sleep 。
首先,正如您可以猜到的,为了给您的解决方案着色,您必须将这些信息存储在某个地方。
然后,为了验证玩家是否找到了正确的答案,您必须能够将他的工作与解决方案进行比较。
我看到您使用了图像来显示目标状态。我建议您改为编码,这样您就可以更轻松地比较它(并且您还可以轻松生成许多不同的“级别”)。
Java 是面向对象的。你没有写任何类(class),所以我想你仍在学习。让我告诉你一件事:类(class)很棒。它们是自己的对象,有自己的方法和变量。您可以通过多种方式实例化它们。就像,比方说......你自己的对象的数组。一个物体可以是……比如说……正方形?一个彩色方 block ?
是的。 您可以定义一个类并让他们自己绘制,而不是绘制大约 100 个正方形。并记住他们自己的颜色。并知道它们是否被点击。
而且,基本上,无论你想要什么。
这是 Square 类的示例:
class Square{
//modal variables
private float xPos = 0;
private float yPos = 0;
private float myWidth = 0;
private float myHeight = 0;
public color myColor = color(0); //myColor and isSelected are public because I want to access them directly from outside the class
public boolean isSelected = false; //public modal variables are not a popular choice, with good reasons, but for now let's just roll with it
//this is a constructor. Every time you instantiate this class, you must call it's constructor. This one can be overloaded with the square's cordinates and size
public Square(int xx, int yy, float ww, float hh) {
xPos = xx;
yPos = yy;
myWidth = ww;
myHeight = hh;
}
//you can call this method to make the square draw itself
public void Render() {
if (isSelected) {
stroke(RED);
} else {
stroke(167);
}
fill(myColor);
rect(xPos, yPos, myWidth, myHeight);
}
//give coordinates to this function to know if this squared has been clicked on
public boolean ClickedOn(float xx, float yy) {
return ((xx > xPos && xx < xPos + myWidth) && (yy > yPos && yy < yPos + myHeight));
}
}
现在,我不打算重写整个内容,但这里有一个最小的、可重现的示例,展示了此类的功能(您可以将以下代码复制并粘贴到处理中并从中学习):
final color RED = #D12020;
final color BLUE = #515DD8;
final color GREEN = #21AF20;
final color YELLOW = #F5EF74;
final color ORANGE = #F59219;
final color PURPLE = #B219F5;
final color WHITE = #FFFFFF;
final int ROWS = 12;
final int COLUMNS = 8;
final color[] colour = {RED, BLUE, GREEN, YELLOW, ORANGE, PURPLE, WHITE};
ArrayList <Square> picker, grid, answer; //there are 3 groups of squared to manage
class Square{
//modal variables
private float xPos = 0;
private float yPos = 0;
private float myWidth = 0;
private float myHeight = 0;
public color myColor = color(0); //myColor and isSelected are public because I want to access them directly from outside the class
public boolean isSelected = false; //public modal variables are not a popular choice, with good reasons, but for now let's just roll with it
//this is a constructor. Every time you instantiate this class, you must call it's constructor. This one can be overloaded with the square's cordinates and size
public Square(float xx, float yy, float ww, float hh) {
xPos = xx;
yPos = yy;
myWidth = ww;
myHeight = hh;
}
//you can call this method to make the square draw itself
public void Render() {
if (isSelected) {
stroke(RED);
} else {
stroke(167);
}
fill(myColor);
rect(xPos, yPos, myWidth, myHeight);
}
//give coordinates to this function to know if this squared has been clicked on
public boolean ClickedOn(float xx, float yy) {
return ((xx > xPos && xx < xPos + myWidth) && (yy > yPos && yy < yPos + myHeight));
}
}
//setup() is meant for you to initialize stuff before the main loop, which is draw()
void setup() {
size(1000, 1000);
InitializeSquares();
}
//let's initialize EVERYTHING SQUARE-SHAPED!
public void InitializeSquares(){
float blockSize = width/20;
float cornerX = width/12;
float cornerY = height/8;
float answerCornerX = cornerX * 6.5;
float answerCornerY = cornerY;
float pickerSize = blockSize * 1.5;
float pickerCornerX = cornerX/2;
float pickerCornerY = cornerY - (pickerSize * 1.5);
//the grid:
grid = new ArrayList <Square>();
for (int i = 0; i<ROWS; i++) {
for (int j = 0; j<COLUMNS; j++) {
grid.add(new Square(cornerX + (j*blockSize), cornerY + (i*blockSize), blockSize, blockSize));
}
}
//the target answer:
answer = new ArrayList <Square>();
for (int i = 0; i<ROWS; i++) {
for (int j = 0; j<COLUMNS; j++) {
answer.add(new Square(answerCornerX + (j*blockSize), answerCornerY + (i*blockSize), blockSize, blockSize));
answer.get(answer.size()-1).myColor = colour[floor(random(colour.length))];
}
}
//the color pickers
picker = new ArrayList <Square>();
for (int i = 0; i < colour.length; i++) {
picker.add(new Square(pickerCornerX + (i*pickerSize), pickerCornerY, pickerSize, pickerSize));
picker.get(picker.size()-1).myColor = colour[picker.size()-1];
}
}
void draw() {
background(0); //it's ok, the squares will draw themselves
//let's draw the player's squares
for (Square s : grid) {
s.Render();
}
for (Square s : grid) {
//I'm drawing the selected squared over the unselected ones so we see them better
if (s.isSelected) {s.Render();}
}
//drawing the answer grid
for (Square s : answer) {
s.Render();
}
//drawing the color pickers
for (Square s : picker) {
s.Render();
}
}
void mouseClicked() {
//checking if is the player selecting squares
for (Square s : grid) {
if (s.ClickedOn(mouseX, mouseY)) {
s.isSelected = !s.isSelected;
}
}
//checking if the player is coloring squares. If so, coloring the squared and unselecting them
for (Square p : picker) {
if (p.ClickedOn(mouseX, mouseY)) {
for (Square s : grid) {
if (s.isSelected) {
s.isSelected = false;
s.myColor = p.myColor;
}
}
}
}
}
根据我从您的代码中读到的内容,我相信您将能够使用这些行来实现您的目标。你的代码表明你还有很多东西需要学习,但它仍然显示了很多 promise 。如果你自己完成这一切,那么我也相信你很快就会变得非常擅长。所以我不会列出你所做的每一件错误的小事或其他什么。你会在适当的时候学会的。
不要忘记学习的同时享受乐趣。这已经完成了一半以上的工作了!如果您有疑问,我会留下来。
这是一些基于您原始帖子的代码。它是“能够选择一列、一行和一个单元格”的实现,并在其中放置一些颜色,但没有任何类。
老实说,这比我昨天做的要难得多。
final color RED = #D12020;
final color BLUE = #515DD8;
final color GREEN = #21AF20;
final color YELLOW = #F5EF74;
final color ORANGE = #F59219;
final color PURPLE = #B219F5;
final color WHITE = #FFFFFF;
final int ROWS = 12;
final int COLUMNS = 8;
final int BLOCK_SIZE = 40;
int blockSize;
int spacing;
int cornerX;
int cornerY;
int size;
int space;
int cell = -1;
int col = -1;
int row = -1;
color[] colour = {RED, BLUE, GREEN, YELLOW, ORANGE, PURPLE, WHITE};
//I added these variables. The color array is 2 dimentional, so you can use it like if it had grid coordinates (square [0][2] is third on the first line)
PVector square_selected; //this will hold the grid coordinates of the currently selected square, or -1 for "nothing"
color[][] square_color;
int columnSelected = -1;
int rowSelected = -1;
void setup() {
size(1000, 1000);
background(0);
cornerX = width/8;
cornerY = height/8;
size = height/33;
space = width/100;
targetImage();
//initializing the new array
square_color = new color[COLUMNS][ROWS];
for (int i=0; i<COLUMNS; i++) {
for (int j=0; j<ROWS; j++) {
square_color[i][j] = color(0);
}
}
square_selected = new PVector(-1, -1);
//You might have heard about this or not, but as a general rule global variables are shunned upon. That's because when the program becomes more complex, they become exponentially harder to control,
//and can cause unexpected behavior. No joke, Toyota programmers actually caused death because of these (search for "Toyota spaghetti code" if you're curious)
//So I'm initializing these here and removing the places where the code would change them unexpectedly.
//You may have had a better idea of what you're doing that I do, though, so you can chalk up this decision to my coding preferences.
blockSize = width/20;
spacing = width/200;
}
void draw() {
background(0);
grid();
colourCells();
//selectCell();
columnSelectors();
rowSelectors();
counter();
}
//draws the colour selectors
void colourCells() {
int colourBlocks = 7;
int squareX = blockSize;
for (int i=0; i < colourBlocks; i++) {
stroke(167);
fill(colour[i]);
rect(squareX, 0, blockSize, blockSize);
squareX += spacing+blockSize;
}
}
//draws the grid
void grid() {
for (int i=0; i<ROWS; i++) {
for (int j=0; j<COLUMNS; j++) {
stroke(167);
fill(square_color[j][i]);
rect(cornerX + (BLOCK_SIZE * j), cornerY + (BLOCK_SIZE * i), BLOCK_SIZE, BLOCK_SIZE);
}
}
//drawing the selected square over the others
if (square_selected.x > -1) {
stroke(RED);
fill(square_color[(int)square_selected.x][(int)square_selected.y]);
rect(cornerX + (BLOCK_SIZE * (int)square_selected.x), cornerY + (BLOCK_SIZE * (int)square_selected.y), BLOCK_SIZE, BLOCK_SIZE);
}
}
//creates the circular column selectors
void columnSelectors() {
int columnX = (cornerX) + (size/2) + (space/2);
int columnY = (cornerY) - (size/2) - space;
noFill();
for ( int i = 0; i<COLUMNS; i++) {
if (i == columnSelected) {
stroke(RED);
} else {
stroke(167);
}
ellipse(columnX, columnY, size, size);
columnX += size + space;
}
}
//creates the circular row selectors
void rowSelectors() {
int rowX = (cornerX) - (size/2) - space;
int rowY = (cornerY) + (size/2) + (space/2);
noFill();
for ( int j = 0; j<ROWS; j ++) {
if (j == rowSelected) {
stroke(RED);
} else {
stroke(167);
}
ellipse(rowX, rowY, size, size);
rowY += size + space;
}
}
//creates the score counter at the bottom of canvas
void counter() {
float x = width/3;
float y = 4*height/5;
float boxLength = width/3;
float boxHeight = height/20;
int counter = 0;
fill(255);
rect(x, y, boxLength, boxHeight);
String counterText = "Num Moves: " + counter;
fill(0);
textSize(40);
text(counterText, x + (boxLength*1/14.0), y + (boxHeight*3/4.0));
}
//loads in a random target image
void targetImage() {
float y = height/5;
float x = width/2;
String [] file = {"target0.png", "target1.png", "target2.png", "target3.png", "target4.png"};
PImage target;// create a variable that can point to an off-screen buffer
String filename = file[int(random(0, 4))]; //"target" + int(random(0,4)) + ".png";// create filename
target = loadImage(filename);// load image file into off-screen buffer
//image(target, x, y, width/3.125, height/2.08);// display the buffer on canvas at location x, y
}
//allows the user to select/deselect a column
int selectColumn() {
int x;
int xPos= mouseX - cornerX;
x = xPos/(size+space);
if (x == col)
return -1;
else
return x;
}
//allows the user to select/deselect a row
int selectRow() {
int y;
int yPos= mouseY - cornerY;
y = yPos/(size+space);
if (y == row)
return -1;
else
return y;
}
//mouse click functions for selecting cells/rows/columns/colour
void mouseClicked() {
//user is clicking inside the grid
if (mouseX > cornerX && mouseY > cornerY && mouseX < cornerX + COLUMNS*BLOCK_SIZE
&& mouseY < cornerY + ROWS*BLOCK_SIZE) {
//cell = cellSelected();
square_selected.x = floor((mouseX - cornerX)/BLOCK_SIZE);
square_selected.y = floor((mouseY - cornerY)/BLOCK_SIZE);
}
//user is selectong a column
if (mouseX > cornerX && mouseY > (cornerY - (space + size)) && mouseX < cornerX + COLUMNS*BLOCK_SIZE
&& mouseY < cornerY) {
if (columnSelected == selectColumn()) {
columnSelected = -1;
} else {
columnSelected = selectColumn();
}
}
//user is selecting a row
if (mouseX > (cornerX - (space + size)) && mouseY > cornerY && mouseX < cornerX
&& mouseY < cornerY + ROWS*BLOCK_SIZE) {
if (rowSelected == selectRow()) {
rowSelected = -1;
} else {
rowSelected = selectRow();
}
}
//user is picking a color
if (mouseY < blockSize) {
//validating the color
for (int c = 0; c < colour.length; c++) {
if (get(mouseX, mouseY) == colour[c]) {
//if a square has been selected
if (square_selected.x > -1) {
square_color[(int)square_selected.x][(int)square_selected.y] = colour[c];
square_selected = new PVector(-1,-1);
}
//if a row has been selected
if (rowSelected > -1) {
for (int i = 0; i < COLUMNS; i++) {
square_color[i][rowSelected] = colour[c];
}
rowSelected = -1;
}
//if a column has been selected
if (columnSelected > -1) {
for (int i = 0; i < ROWS; i++) {
square_color[columnSelected][i] = colour[c];
}
columnSelected = -1;
}
break;
}
}
}
}
诀窍是保持一切整洁并避免意外的变化。要认识到这一点,一些良好的工作习惯会有很大帮助。习惯如下:
所以,您将在代码中看到它,但基本上我只是添加几个变量,它们将跟踪方 block 的颜色以及当前选择的单元格或行/列。我还调整了绘制对象的函数,因此颜色取决于属性(在我刚才提到的变量中),而不仅仅是程序在发生时绘制的东西 - 因此循环的新迭代不会删除它们。哦,由于这些更改,我删除了不再需要的功能。
和上次一样,我会留下来,所以如果有任何不明白或你不明白的问题,请不要犹豫,提出问题。玩得开心!
关于java - 如何用选定的颜色填充网格上选定的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58966238/
padding:initial 比 padding:0 有什么优势吗?示例: textarea { padding: 0; } Hello, world! 最佳答案 它们的意思是一
我尝试通过按钮填充 JList,然后在先前填充的 Jlist 上使用 DoubleClick 填充 JTextField。 代码: private void extractUsedVariables
我正在尝试做 var width = ($(this).width() + $(this).css('padding-left') + $(this).css('padding-right' ));
我在导航中添加了悬停效果,遗憾的是悬停也影响了上面的文字。如何在不影响文本位置的情况下向导航添加悬停? 可悲的是,我找不到解决这个问题的方法。 HTML 模板:http://projects.help
我是 F# 初学者,下面代码中的 %-5s 和 %5s 有什么作用?我认为它提供了空间填充,但我不确定它是如何填充的? printfn "%-5s %5s" "a" "b" 当我尝试 prin
我需要选择带狗的用户(带 type 等于“狗”的宠物) var User = Waterline.Collection.extend({ identity: 'user', attribute
我一直在尝试让 Excel 在一组列上应用公式,然后将模式扩展到整个行集。 这导致了以下代码: For i = 0 To avgsheetNames.Count - 1 If Contains(CSt
随着 Flutter 2.0 的发布,FlatButton已被替换为 TextButton . 因此,填充属性不再直接可用,而是作为 ButtonStyle属性(property)。 我的问题是,我该
这似乎是一个简单的问题,但我已经尝试了一个小时,似乎无法弄清楚。 我要做的就是用 Canvas 填充 MainWindow。我找不到任何允许这样做的属性,我能想到的唯一方法是设置 Canvas.Wid
这是a website具有移动 View 。 网站宽度为 640 像素,但 iPhone 以 678 像素渲染文档。在 Android 中看起来很棒。 我添加了视口(viewport)元: 主体 C
我正在使用 GridBagLayout到(当前)显示两行。我知道这种布局对于这项任务来说太过分了,但我正在努力学习如何使用它。问题是我已将两个面板添加到两个单独的行中,并且内容周围存在巨大差距(请参见
我有以下代码已传递给我并创建多边形: var map; function initialize() { var myLatlng = new google.maps.LatLng(-36.4
我在 Jpanel 中有一些项目,然后将其推到顶部并用作基本搜索引擎的工具栏。我遇到一个问题,因为没有足够的空间,所以我的最后一个组合框没有显示。但是,左侧有很多空白空间,我需要移动所有内容来填充 J
我创建了带有阈值的二进制图像。如下图所示如何改变白色形状的颜色以使其可索引? 到目前为止,这是我的代码: void threshold() { cv::Mat src_8uc3_img = c
我有一个 JTable,我想知道是否有更好的方法来填充它,这是我的代码: //Metodo para llenar un jtable con datos de la base public stat
我想要做的是裁剪一个卷以删除所有不相关的数据。例如,假设我有一个 100x100x100 的体积,其中填充了 0,但其中的 50x50x50 体积则填充了 1。如何从原始体积中获得裁剪后的 50x50
因此,我正在创建一种对一组数字进行洗牌的方法,其想法是创建这些数字的总体。因此,我创建了一个循环,对数字进行洗牌,然后将其添加到数组列表中,但是经过一些调试语句后,我发现它确实对数字进行洗牌,但只将最
假设我有这两个类: public class A where T : IEntityWithID, new() { private static EntityInfo entityInfo =
我正在尝试添加用户输入的两个大整数作为字符串。当两个输入字符串的长度不同时,我尝试用零填充较短的数字,但它不起作用。因此,如果我输入 456 和 7,它会给出 3,前面有一些随机字符。感谢您的任何建议
这是我将内容打印到表格 View 的代码 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: Index
我是一名优秀的程序员,十分优秀!