原生js結合html5製作小飛龍的簡易跳球

來源:互聯網
上載者:User

原生js結合html5製作小飛龍的簡易跳球
這篇文章主要介紹了原生js結合html5製作小飛龍的簡易跳球的方法和代碼分享,推薦給大家,有需要的小夥伴可以參考下。

 

 

示範地址:http://runjs.cn/detail/yjpvqhal

html代碼

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

<html>

<head>

<meta charset="utf-8"/>

<title>小飛龍的跳球</title>

</head>

<body onload="init()">

<canvas id="game" width="400" height="400" style="border:1px solid #c3c3c3">

你的遊覽器不支援html5的畫布元素,請升級到IE9+或使用firefox、chrome這類進階的智能遊覽器!

</canvas>

<script>

var canvas = document.getElementById('game');

var ctx = canvas.getContext('2d');

var grad;

//盒子的起始位置和大小以及球的半徑

var box = {x:20,y:20,w:350,h:350,r:20};

//球的中心位置和位移位置

var inbox = {//box內的限制範圍

bx:(box.w+box.x-box.r),

by:(box.h+box.y-box.r),

ix:box.x+(box.r*2),

iy:box.y+(box.r*2)

};

//球的初始位置和變化位置

var ball = {x:50,y:50,vx:4,vy:8};

var img = new Image();

img.src = 'images/qiuqiu.png';

var hue = [[255,0,0],[255,255,0],[0,255,0],[0,255,255],[0,0,255],[255,0,0]];

function init(){

grad = ctx.createLinearGradient(box.x,box.y,box.w,box.h);

for(var i=0;i<hue.length;i++){

var color = 'rgb('+hue[i][0]+','+hue[i][1]+','+hue[i][2]+')';

grad.addColorStop(i/hue.length,color);

}

ctx.lineWidth = box.r;

ctx.fillStyle = 'rgb(200,0,50)';

ctx.fillStyle = grad;

moveBall();

setInterval(moveBall,50);

}

//碰撞檢測並重新確定球的位置

function moveBallEndCheck(){

var nx = ball.x + ball.vx;

var ny = ball.y + ball.vy;

if(nx > inbox.bx){//當前x大於上邊框邊界

ball.vx = -ball.vx;//球的變化x座標當前當前變化x座標的負數

nx = inbox.bx;//當前位置為上邊框的位置

}

if(nx < inbox.ix){//當前位置小於下邊框

nx = inbox.ix;//當前位置為下邊框的x

ball.vx = -ball.vx;//球的變化x座標翻轉取負

}

if(ny > inbox.by){

ny = inbox.by;

ball.vy = -ball.vy;

}

if(ny < inbox.iy){

ny = inbox.iy;

ball.vy = -ball.vy;

}

ball.x = nx;

ball.y = ny;

}

function moveBall(){

ctx.clearRect(box.x,box.y,box.w,box.h);

moveBallEndCheck();

ctx.beginPath();

//console.log(ball.x+"\t"+ball.y+"\t"+ball.vx+"\t"+ball.vy+"\t"+(ball.x-box.r)+"\t"+(ball.y-box.r));

ctx.drawImage(img,(ball.x-box.r),(ball.y-box.r));

ctx.fillRect(box.x,box.y,box.r,box.h);

ctx.fillRect((box.x+box.w-box.r),box.y,box.r,box.h);

ctx.fillRect(box.x,box.y,box.w,box.r);

ctx.fillRect(box.x,(box.y+box.h-box.r),box.w,box.r);

ctx.closePath();

ctx.fill();

}

</script>

</body>

</html>

示範圖片

以上所述就是本文的全部代碼,希望大家能夠喜歡。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.