0

我想为俄罗斯方块创建一个正方形的正方形,例如由 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

1 回答 1

2

如果您想创建一个由 4 个正方形组成的正方形,那么您只需重复已有的内容,即调用rectangle4 次并相应地定位矩形。

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 个矩形,如下所示:

由 4 个正方形组成

如果要创建 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形

如果您想从一系列点创建单个多边形,则可以使用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-graphicscrate 的文档中,您可以看到额外的绘图函数,例如ellipse(),image()text()

于 2020-12-13T18:25:20.363 回答