protected override void Initialize() { //Init the background grid backgroundGrid = new Grid(this, "backgroundFile.txt"); //Init the background brick texture bgBrick = Content.Load<Texture2D>(@"Textures/GreyBlock"); //Init the snake snake = new Snake(this, backgroundGrid); //Init the snake texture snakeTexture = Content.Load<Texture2D>(@"Textures/RedBlock"); base.Initialize(); }
//Constructor public Snake(Game1 game, Grid grid) { //Get the game this.game = game; //Get the current grid this.grid = grid; //Set the starting direction direction = 'R'; //Init the body size bodyRow = new int[grid.numCellsHigh * grid.numCellsLong]; bodyCol = new int[grid.numCellsHigh * grid.numCellsLong]; //Init the snake size snakeLength = 6; curLength = 1; for (int i = 0; i < bodyRow.Length; i++) { bodyRow[i] = 0; bodyCol[i] = 0; } //Find the body for (int i = 0; i < grid.numCellsHigh; i++) { for (int j = 0; j < grid.numCellsLong; j++) { if (grid.charGrid[i, j] == 's') { bodyRow[0] = i; bodyCol[0] = j; startRow = i; startCol = j; } } } //Init the speed and timer speed = 5; currentTick = 0; //Init the state of the game lost = false; //Init the powerup EnablePowerups(); powerUpEnabled = true; }
//this is called every frame, it checks the grid (bricks in this game) public void Update(Grid grid) { bool coll = false; ball.X += (int)ballVelocity.X; if (grid.hasCollided(ball) == true) // Check if collided with left or right of brick { ballVelocity.X *= -1; coll = true; } ball.Y += (int)ballVelocity.Y; if (grid.hasCollided(ball) == true && coll == false) // Check if collided with top or bottom of brick { ballVelocity.Y *= -1; } }