JavaScript无法读取以前使用的礼节

时间:2018-08-08 09:52:11

标签: javascript

我正在尝试制作一个简单的游戏,更确切地说是蛇。无论如何,因此在这里我陷入了控制台错误,无法从已被珍贵使用的snake[0].x行中读取snake[score].x = snake[0].x;的正确性

function scoreManager(){
    if(snake[0].x==food.x && snake[0].y==food.y){
        score++;
        snake[score].x = snake[0].x;
        snake[score].y = snake[0].y;
        food = {
            x:Math.floor(Math.random()*30+1)*cell,
            y:Math.floor(Math.random()*29+1)*cell
        };
    }
}

它在if语句中读取就很好(我知道那是因为如果该语句为true,分数就会增加)。

我得到的确切错误是:

> game_engine.js:86 Uncaught TypeError: Cannot set property 'x' of
> undefined
>     at scoreManager (game_engine.js:86)
>     at draw (game_engine.js:110) scoreManager @ game_engine.js:86 draw @ game_engine.js:110 setInterval (async) (anonymous) @
> game_engine.js:127

为什么会这样? 哦,由于某种原因,新的食物坐标也总是相同的:/

这是完整的代码

const cvs = document.getElementById("gameArea");
const ctx = cvs.getContext("2d");

var playGround = new Image();
var foodSprite = new Image();
var sandSprite = new Image();
var snakeSprite = new Image();
var tailSprite = new Image();

playGround.src = './Textures/play_area.png';
foodSprite.src = './Textures/Food.png';
sandSprite.src = './Textures/sand.png';
snakeSprite.src = './Textures/snake_head.png';
tailSprite.src = './Textures/Snake_body.png';

//variables
const cell = 32;
let score = 1;
let direction;

//objects
let snake = [];
snake[0] = { x: 1 * cell, y: 1 * cell };

let food = {
    x:Math.floor(Math.random()*30+1)*cell,
    y:Math.floor(Math.random()*29+1)*cell
};

//controls

document.addEventListener('keydown',directionGo);
function directionGo(event){
    if(event.keyCode == 37 && direction != 'RIGHT'){
        direction = 'LEFT';
    } else
    if(event.keyCode == 38 && direction != 'DOWN'){
        direction = 'UP';
    } else
    if(event.keyCode == 39 && direction != 'LEFT'){
        direction = 'RIGHT';
    } else
    if(event.keyCode == 40 && direction != 'UP'){
        direction = 'DOWN';
    }
}
function directionManager(){
    if(direction=="LEFT"){
        snake[0].x-=cell;
    } else 
    if(direction=="RIGHT"){
        snake[0].x+=cell;
    } else 
    if(direction=="UP"){
        snake[0].y-=cell;
    } else 
    if(direction=="DOWN"){
        snake[0].y+=cell;
    } 
}

//in case u mess up
function gameOver (){
    for (let i = 1; i<snake.length; i++){
        if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
            return true;
        }
    }
    if(snake[0].x == 0*cell || snake[0].x == 32*cell || snake[0].y == 0*cell || snake[0].y == 31*cell){
        return true;
    }
}
function respawn (){
    if(gameOver() == true){
        snake = [];
        snake[0] = { x: 1 * cell, y: 1 * cell };
        score = 0;
        direction='RIGHT';
    }
}

//in case u a gud boy
function scoreManager(){
    if(snake[0].x==food.x && snake[0].y==food.y){
        score++;
        snake[score].x = snake[0].x;
        snake[score].y = snake[0].y;
        food = {
            x:Math.floor(Math.random()*30+1)*cell,
            y:Math.floor(Math.random()*29+1)*cell
        };
    }
}

function tail(){
}

//Drawing function that manages the food , map and the player
function draw() {
    ctx.drawImage(playGround, 0, 0); // ground

    //drawing snake
    ctx.drawImage(snakeSprite, snake[0].x, snake[0].y);
    for(let i = 1; i < snake.length; i++){
        ctx.drawImage(snakeSprite, snake[i].x, snake[i].y);
    }

    //direction o.o
    directionManager();
    scoreManager();

    //food
    ctx.drawImage(foodSprite,food.x,food.y);

    //score
    ctx.fillStyle='white';
    ctx.font = '40px Changa One';
    ctx.fillText(score,16*cell+5,1*cell+15);

    //loops the animation
    //window.requestAnimationFrame(draw);

    respawn();
}

//draw();
let game = setInterval (draw,100);

0 个答案:

没有答案