c# - how to move an object through a grid -
i'm having trouble coming solution on how use array snake game. have head 20x20 pixels on 400x400 size window.
areagrid = new vector2[columns, rows]; (int x = 0; x < columns; x++) { (int y = 0; y < rows; y++) { areagrid[x, y] = new vector2(x * 20, y * 20); console.writeline("areagrid[{0},{1}] = {2}", x, y, areagrid[x, y]); } }
so naturally, there 400 "blocks" head of snake plus tail can occupy. have head drawn in array @ [5, 5] coords 100, 100 on grid. want head move 20 pixels @ time new point in array. examply, movement right place head @ [5, 6] in array , @ 120, 100 on grid. dont know how that. how implement movement thru array within update method?
use list block coords:
list<vector2> snake = new list() { {5,5}, {5,6}, {5,7}, {6,7}, {7,7} }
and render it:
var transform = matrix.createscale(20) * matrix.createtranslation(offset); spritebatch.begin(null,null,..., tranform); foreach (var pos in snake) spritebatch.draw(white_1x1_texture, pos, null, snakecolor); spritebatch.end();
or
spritebatch.begin(); foreach (var pos in snake) { var box = new rectangle(offset.x + pos.x * 20, offset.y + pos.y*20, 20,20); spritebatch.draw(white_1x1_texture, box, null, snakecolor); } spritebatch.end();
Comments
Post a Comment