• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 JavaScript, TS 게시판 게임만들기에서 피해도 게임이 종료됩니다.

게임만들기에서 피해도 게임이 종료됩니다.

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #122437

    송승환
    참가자
    var canvas = document.querySelector('canvas');
    var ctx = canvas.getContext('2d');
    var cactusList = [];
    var timer = 0;
    var jumping = false;
    var jumpingCount = 0;
    canvas.width = window.innerWidth - 100;
    canvas.height = window.innerHeight - 100;
    var character = {
        x: 100,
        y: 200,
        width: 50,
        height: 50,
        draw() {
            ctx.fillStyle = 'black';
            ctx.fillRect(this.x, this.y, this.width, this.height);
        }
    }
    class Enemy {
        constructor() {
            this.x = 1400;
            this.y = 200;
            this.width = 50;
            this.height = 50;
        }
        draw() {
            ctx.fillStyle = 'gray';
            ctx.fillRect(this.x, this.y, this.width, this.height);
        }
    }
    function frame() {
        animation = requestAnimationFrame(frame);
        timer++;
        ctx.clearRect(0, 0, canvas.width, canvas.height)
        if (timer % 80 == 0) {
            var cactus = new Enemy()
            cactusList.push(cactus);
        }
        cactusList.forEach(function (a, i, o) {
            if(a.x + a.width < 0){
                o.splice(i, 1);
            }
            a.x -= 10;
            
            
            contect(character, a)
            
            a.draw()
        });
        if(jumping && jumpingCount == 0){
            if(character.y > 50){
                character.y -= 10;
            }
        }
        if(!jumping || jumpingCount == 1){
            if(character.y < 200){
                character.y += 10
            }
        }
        if(character.y == 200){
            jumpingCount = 0
        }
        character.draw()
    }
    
    
    document.addEventListener('keydown', function(e){
        if(e.code == 'Space'){
            jumping = true
        }
        
    })
    
    
    document.addEventListener('keyup', function(){
        jumping = false;
        jumpingCount++
    })
    frame();
    function contect(character, enemy){
        var xind = enemy.x - (character.x + character.width);
        var yind = enemy.y - (character.y + character.height);
        if(xind < 0 && yind < 0){
            ctx.clearRect(0,0,canvas.width, canvas.height)
            cancelAnimationFrame(animation)  
        }
    }
    제목 없는 동영상 - Clipchamp로 제작 (2)
    
    #122476

    codingapple
    키 마스터
    xind가 0보다 작은경우에 항상 멈추는게 아니라
    0부터 대충 -50 사이면 멈추라고 해봅시다
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠