gpt4 book ai didi

rust - Piston 创建一个多个正方形的 block

转载 作者:行者123 更新时间:2023-12-03 11:42:28 25 4
gpt4 key购买 nike

我想为俄罗斯方块创建一个像正方形的正方形这样的东西,就像一个由4个小正方形(4x4)组成的正方形一样,我知道如何创建一个简单的正方形并将其移动,
但是我不知道如何创建一个更复杂的图形(例如4x4正方形或L)并将其像一个简单的正方形一样移动吗?
如果需要的话,这里的代码=>

extern crate glutin_window;
extern crate graphics;
extern crate opengl_graphics;
extern crate piston;

use glutin_window::GlutinWindow;
use graphics::rectangle;
use opengl_graphics::{GlGraphics, OpenGL};
use piston::event_loop::{EventSettings, Events};
use piston::input::{
Button, Key, PressEvent, ReleaseEvent, RenderArgs, RenderEvent, UpdateArgs, UpdateEvent,
};
use piston::window::WindowSettings;
use piston_window::Transformed;
use std::process;

struct Game {
gl: GlGraphics,
pos_x: f64,
pos_y: f64,
}

impl Game {
fn render(&mut self, arg: &RenderArgs) {
use graphics;

let BACKGROUND: [f32; 4] = [0.349019608, 0.349019608, 0.290196078, 1.0];
let COO: [f32; 4] = [0.364705882, 0.717647059, 0.870588235, 0.8];

let square_c = rectangle::square(100.0, 100.0, 30.0);
let pos_x = self.pos_x as f64;
let pos_y = self.pos_y as f64;

println!("pos x = {}", pos_x);
println!("pos y = {}", pos_y);

self.gl.draw(arg.viewport(), |c, gl| {
graphics::clear(BACKGROUND, gl);
rectangle(COO, square_c, c.transform.trans(pos_x, pos_y), gl); // deplace le carre de -200 vers la gauche
});
}

fn press(&mut self, args: &Button) {
if let &Button::Keyboard(key) = args {
match key {
Key::Up => self.pos_y -= 10.0,
Key::Down => self.pos_y += 10.0,
Key::Left => self.pos_x -= 10.0,
Key::Right => self.pos_x += 10.0,
_ => {
println!("other1");
}
}
}
}

fn release(&mut self, args: &Button) {
if let &Button::Keyboard(key) = args {
match key {
Key::Up => {
println!("Up release");
}
Key::Down => {
println!("Down release");
}
Key::Left => {
println!("Left release");
}
Key::Right => {
println!("Right release");
}
_ => {
println!("other release");
}
}
}
}
}

fn main() {
let opengl = OpenGL::V3_2;

let mut window: GlutinWindow = WindowSettings::new("Tetris Game", [400, 800])
.graphics_api(opengl)
.exit_on_esc(true)
.build()
.unwrap();

let mut game = Game {
gl: GlGraphics::new(opengl),
pos_x: 200.0,
pos_y: 0.0,
};

let mut events = Events::new(EventSettings::new());
while let Some(e) = events.next(&mut window) {
if let Some(r) = e.render_args() {
game.render(&r);
}
if let Some(b) = e.press_args() {
game.press(&b);
}
if let Some(b) = e.release_args() {
game.release(&b);
}
}
}

最佳答案

如果要创建一个由4个正方形组成的正方形,则只需重复已有的操作即可,即调用rectangle 4次并相应地定位矩形。

const RED:    [f32; 4] = [1.0, 0.0, 0.0, 1.0];
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
const YELLOW: [f32; 4] = [1.0, 1.0, 0.0, 1.0];

let (x, y) = (50.0, 50.0);

rectangle(RED, rectangle::square( 0.0, 0.0, 30.0), c.transform.trans(x, y), gl);
rectangle(GREEN, rectangle::square(30.0, 0.0, 30.0), c.transform.trans(x, y), gl);
rectangle(BLUE, rectangle::square( 0.0, 30.0, 30.0), c.transform.trans(x, y), gl);
rectangle(YELLOW, rectangle::square(30.0, 30.0, 30.0), c.transform.trans(x, y), gl);
上面呈现了一个4个矩形,看起来像这样:
square from 4 squares
如果要创建L形,则只需放置2个矩形即可创建该形状。
rectangle(RED,   [0.0,  0.0, 15.0, 30.0], c.transform.trans(x, y), gl);
rectangle(GREEN, [0.0, 30.0, 30.0, 15.0], c.transform.trans(x, y), gl);
您也可以将位置直接烘烤成矩形形状,而不是使用 .trans(x, y),即,对于最后一个位置,将是 [x, y + 30.0, 30.0, 15.0]
L shape
相反,如果要从一系列点创建单个多边形,则可以使用 polygon() 。以下代码会创建与以前相同的L形,只是这次它是完全红色的。
polygon(
RED,
&[
[ 0.0, 45.0],
[ 0.0, 0.0],
[15.0, 0.0],
[15.0, 30.0],
[30.0, 30.0],
[30.0, 45.0],
],
c.transform.trans(x, y),
gl,
);
piston2d-graphics crate's documentation中,您可以看到其他绘制功能,例如 ellipse() image() text()

关于rust - Piston 创建一个多个正方形的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65278610/

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