WebWebWeb项目---拳皇(下)
xujiaojiao本文章借鉴https://www.lzhiscoding.xyz
项目实战–拳皇
实现攻击效果
在Player类中把人物背景渲染出来,以及添加一个矩形来表示挥拳范围,便于碰撞检测
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
| render() {
this.ctx.fillStyle = 'blue'; this.ctx.fillRect(this.x, this.y, this.width, this.height);
if (this.direction > 0) { this.ctx.fillStyle = 'red'; this.ctx.fillRect(this.x + 120, this.y + 40, 100, 20); } else { this.ctx.fillStyle = 'red'; this.ctx.fillRect(this.x + this.width - 120 - 100, this.y + 40, 100, 20); }
let status = this.status;
if (this.status === 1 && this.direction * this.vx < 0) { status = 2; }
let obj = this.animations.get(status); if (obj && obj.loaded) { if (this.direction > 0) { let k = parseInt(this.frame_current_cnt / obj.frame_rate) % obj.frame_cnt; let image = obj.gif.frames[k].image; this.ctx.drawImage(image, this.x, this.y + obj.offset_y, image.width * obj.scale, image.height * obj.scale); } else { this.ctx.save(); this.ctx.scale(-1, 1); this.ctx.translate(-this.root.game_map.$canvas.width(), 0);
let k = parseInt(this.frame_current_cnt / obj.frame_rate) % obj.frame_cnt; let image = obj.gif.frames[k].image;
this.ctx.drawImage(image, this.root.game_map.$canvas.width() - this.x - this.width, this.y + obj.offset_y, image.width * obj.scale, image.height * obj.scale);
this.ctx.restore(); } } if (status === 4) { if (this.frame_current_cnt == obj.frame_rate * (obj.frame_cnt - 1)) { this.status = 0; } }
this.frame_current_cnt++; }
|
实现攻击函数、碰撞检测函数和被攻击函数,另外还有render函数需要特判一下被攻击的状态
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| import { MyGameObject } from '/static/js/MyGameObject/base.js';
export class Player extends MyGameObject { constructor(root, info) { super(); this.root = root; this.id = info.id; this.x = info.x; this.y = info.y; this.width = info.width; this.height = info.height; this.color = info.color; this.vx = 0; this.vy = 0; this.speedx = 400; this.speedy = -1000; this.ctx = this.root.game_map.ctx; this.gravity = 50; this.direction = 1; this.status = 3; this.pressed_Keys = this.root.game_map.controller.pressed_Keys; this.animations = new Map(); this.frame_current_cnt = 0; }
start() { }
move() { if (this.status === 3) { this.vy += this.gravity; } this.x += this.vx * this.timedelta / 1000; this.y += this.vy * this.timedelta / 1000;
if (this.y > 450) { this.y = 450; this.vy = 0; this.status = 0; } if (this.x < 0) { this.x = 0; } else if (this.x + this.width > this.root.game_map.$canvas.width()) { this.x = 1280 - this.width; } }
update_control() { let w, a, d, space; if (this.id === 0) { w = this.pressed_Keys.has('w'); a = this.pressed_Keys.has('a'); d = this.pressed_Keys.has('d'); space = this.pressed_Keys.has(' '); } else { w = this.pressed_Keys.has('ArrowUp'); a = this.pressed_Keys.has('ArrowLeft'); d = this.pressed_Keys.has('ArrowRight'); space = this.pressed_Keys.has('Enter'); }
if (this.status === 0 || this.status === 1) { if (space) { this.status = 4; this.vx = 0; this.frame_current_cnt = 0; } else if (w) { if (d) { this.vx = this.speedx; } else if (a) { this.vx = -this.speedx; } else { this.vx = 0; } this.vy = this.speedy; this.status = 3; } else if (d) { this.vx = this.speedx; this.status = 1; } else if (a) { this.vx = -this.speedx; this.status = 1; } else { this.vx = 0; this.status = 0; } } }
update_direction() { let players = this.root.players; if (players[0] && players[1]) { let me = this, you = players[1 - this.id]; if (me.x < you.x) me.direction = 1; else me.direction = -1; } }
is_attack() { this.status = 5; this.frame_current_cnt = 0; }
is_collision(r1, r2) { if (Math.max(r1.x1, r2.x1) > Math.min(r1.x2, r2.x2)) return false; if (Math.max(r1.y1, r2.y1) > Math.min(r1.y2, r2.y2)) return false; return true; }
update_attack() { if (this.status === 4 && this.frame_current_cnt === 18) { let me = this, you = this.root.players[1 - this.id]; let r1; if (this.direction > 0) { r1 = { x1: me.x + 120, y1: me.y + 40, x2: me.x + 120 + 100, y2: me.y + 40 + 20, }; } else { r1 = { x1: me.x + me.width - 120 - 100, y1: me.y + 40, x2: me.x + me.width - 120 - 100 + 100, y2: me.y + 40 + 20, }; } let r2 = { x1: you.x, y1: you.y, x2: you.x + you.width, y2: you.y + you.height, };
if (this.is_collision(r1, r2)) { you.is_attack(); } } }
update() { this.update_control(); this.move(); this.update_direction(); this.update_attack(); this.render(); }
render() { let status = this.status;
if (this.status === 1 && this.direction * this.vx < 0) { status = 2; }
let obj = this.animations.get(status); if (obj && obj.loaded) { if (this.direction > 0) { let k = parseInt(this.frame_current_cnt / obj.frame_rate) % obj.frame_cnt; let image = obj.gif.frames[k].image; this.ctx.drawImage(image, this.x, this.y + obj.offset_y, image.width * obj.scale, image.height * obj.scale); } else { this.ctx.save(); this.ctx.scale(-1, 1); this.ctx.translate(-this.root.game_map.$canvas.width(), 0);
let k = parseInt(this.frame_current_cnt / obj.frame_rate) % obj.frame_cnt; let image = obj.gif.frames[k].image;
this.ctx.drawImage(image, this.root.game_map.$canvas.width() - this.x - this.width, this.y + obj.offset_y, image.width * obj.scale, image.height * obj.scale);
this.ctx.restore(); } }
if (status === 4 || status === 5) { if (this.frame_current_cnt == obj.frame_rate * (obj.frame_cnt - 1)) { this.status = 0; } }
this.frame_current_cnt++; } }
|
添加生命值,死亡时更换倒地动画,并且render函数特判倒地状态,另外倒地后人物朝向不变和倒地后无法被再次攻击
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| import { MyGameObject } from '/static/js/MyGameObject/base.js';
export class Player extends MyGameObject { constructor(root, info) { super(); this.root = root; this.id = info.id; this.x = info.x; this.y = info.y; this.width = info.width; this.height = info.height; this.color = info.color; this.vx = 0; this.vy = 0; this.speedx = 400; this.speedy = -1000; this.ctx = this.root.game_map.ctx; this.gravity = 50; this.direction = 1; this.status = 3; this.pressed_Keys = this.root.game_map.controller.pressed_Keys; this.animations = new Map(); this.frame_current_cnt = 0; this.hp = 100; }
start() { }
move() { if (this.status === 3) { this.vy += this.gravity; } this.x += this.vx * this.timedelta / 1000; this.y += this.vy * this.timedelta / 1000;
if (this.y > 450) { this.y = 450; this.vy = 0; this.status = 0; } if (this.x < 0) { this.x = 0; } else if (this.x + this.width > this.root.game_map.$canvas.width()) { this.x = 1280 - this.width; } }
update_control() { let w, a, d, space; if (this.id === 0) { w = this.pressed_Keys.has('w'); a = this.pressed_Keys.has('a'); d = this.pressed_Keys.has('d'); space = this.pressed_Keys.has(' '); } else { w = this.pressed_Keys.has('ArrowUp'); a = this.pressed_Keys.has('ArrowLeft'); d = this.pressed_Keys.has('ArrowRight'); space = this.pressed_Keys.has('Enter'); }
if (this.status === 0 || this.status === 1) { if (space) { this.status = 4; this.vx = 0; this.frame_current_cnt = 0; } else if (w) { if (d) { this.vx = this.speedx; } else if (a) { this.vx = -this.speedx; } else { this.vx = 0; } this.vy = this.speedy; this.status = 3; } else if (d) { this.vx = this.speedx; this.status = 1; } else if (a) { this.vx = -this.speedx; this.status = 1; } else { this.vx = 0; this.status = 0; } } }
update_direction() { if (this.status === 6) return; let players = this.root.players; if (players[0] && players[1]) { let me = this, you = players[1 - this.id]; if (me.x < you.x) me.direction = 1; else me.direction = -1; } }
is_attack() { if (this.status === 6) return;
this.status = 5; this.frame_current_cnt = 0; this.hp = Math.max(0, this.hp - 10); if (this.hp <= 0) { this.status = 6; this.frame_current_cnt = 0; } }
is_collision(r1, r2) { if (Math.max(r1.x1, r2.x1) > Math.min(r1.x2, r2.x2)) return false; if (Math.max(r1.y1, r2.y1) > Math.min(r1.y2, r2.y2)) return false; return true; }
update_attack() { if (this.status === 4 && this.frame_current_cnt === 18) { let me = this, you = this.root.players[1 - this.id]; let r1; if (this.direction > 0) { r1 = { x1: me.x + 120, y1: me.y + 40, x2: me.x + 120 + 100, y2: me.y + 40 + 20, }; } else { r1 = { x1: me.x + me.width - 120 - 100, y1: me.y + 40, x2: me.x + me.width - 120 - 100 + 100, y2: me.y + 40 + 20, }; } let r2 = { x1: you.x, y1: you.y, x2: you.x + you.width, y2: you.y + you.height, };
if (this.is_collision(r1, r2)) { you.is_attack(); } } }
update() { this.update_control(); this.move(); this.update_direction(); this.update_attack(); this.render(); }
render() { let status = this.status;
if (this.status === 1 && this.direction * this.vx < 0) { status = 2; }
let obj = this.animations.get(status); if (obj && obj.loaded) { if (this.direction > 0) { let k = parseInt(this.frame_current_cnt / obj.frame_rate) % obj.frame_cnt; let image = obj.gif.frames[k].image; this.ctx.drawImage(image, this.x, this.y + obj.offset_y, image.width * obj.scale, image.height * obj.scale); } else { this.ctx.save(); this.ctx.scale(-1, 1); this.ctx.translate(-this.root.game_map.$canvas.width(), 0);
let k = parseInt(this.frame_current_cnt / obj.frame_rate) % obj.frame_cnt; let image = obj.gif.frames[k].image;
this.ctx.drawImage(image, this.root.game_map.$canvas.width() - this.x - this.width, this.y + obj.offset_y, image.width * obj.scale, image.height * obj.scale);
this.ctx.restore(); } }
if (status === 4 || status === 5 || status === 6) { if (this.frame_current_cnt == obj.frame_rate * (obj.frame_cnt - 1)) { if (status === 6) { this.frame_current_cnt--; } else { this.status = 0; } } }
this.frame_current_cnt++; } }
|
修改所有状态都会受到重力,特判只有在跳跃状态落地后改为静止状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| move() { this.vy += this.gravity;
this.x += this.vx * this.timedelta / 1000; this.y += this.vy * this.timedelta / 1000;
if (this.y > 450) { this.y = 450; this.vy = 0; if (this.status === 3) this.status = 0; } if (this.x < 0) { this.x = 0; } else if (this.x + this.width > this.root.game_map.$canvas.width()) { this.x = 1280 - this.width; } }
|
实现血条功能和计时器
固定画面在html里写,减少canvas渲染的内容,用js控制html,写在GameMap类
在GameMap类的构造函数的末尾添加以下代码:
1 2 3 4 5 6 7 8
| this.root.$kof.append($(` <div class="kof-head"> <div class="kof-head-hp-0"></div> <div class="kof-head-timer">60</div> <div class="kof-head-hp-1"></div> </div> `));
|
设置血条定时器的样式
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 79 80 81 82 83
| #kof { width: 1280px; height: 720px;
background-image: url('/static/images/background/background.gif'); background-size: 200% 100%; background-position: top; position: absolute; }
#kof>.kof-head { width: 100%; height: 80px; position: absolute; top: 0; display: flex; align-items: center; }
#kof>.kof-head>.kof-head-hp-0 { height: 40px; width: calc(50% - 60px); margin-left: 20px; border: white 5px solid; border-right: none; box-sizing: border-box; }
#kof>.kof-head>.kof-head-timer { height: 60px; width: 80px; background-color: orange; border: white 5px solid; box-sizing: border-box; color: white; font-size: 30px; font-weight: 800; text-align: center; line-height: 50px; user-select: none; }
#kof>.kof-head>.kof-head-hp-1 { height: 40px; width: calc(50% - 60px); border: white 5px solid; border-left: none; box-sizing: border-box; }
|
实现血条按照百分比缩减
先把布局和样式写好
GameMap类
1 2 3 4 5 6 7 8 9 10 11 12
| this.root.$kof.append($(` <div class="kof-head"> <div class="kof-head-hp-0"> <div><div></div></div> </div> <div class="kof-head-timer">60</div> <div class="kof-head-hp-1"> <div><div></div></div> </div> </div> `));
|
css文件
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 79
| #kof { width: 1280px; height: 720px;
background-image: url('/static/images/background/background.gif'); background-size: 200% 100%; background-position: top; position: absolute; }
#kof>.kof-head { width: 100%; height: 80px; position: absolute; top: 0; display: flex; align-items: center; }
#kof>.kof-head>.kof-head-hp-0 { height: 40px; width: calc(50% - 60px); margin-left: 20px; border: white 5px solid; border-right: none; box-sizing: border-box; }
#kof>.kof-head>.kof-head-timer { height: 60px; width: 80px; background-color: orange; border: white 5px solid; box-sizing: border-box; color: white; font-size: 30px; font-weight: 800; text-align: center; line-height: 50px; user-select: none; }
#kof>.kof-head>.kof-head-hp-1 { height: 40px; width: calc(50% - 60px); border: white 5px solid; border-left: none; box-sizing: border-box; }
#kof>.kof-head>.kof-head-hp-0>div { background-color: red; height: 100%; width: 100%; float: right; }
#kof>.kof-head>.kof-head-hp-1>div { background-color: red; height: 100%; width: 100%; }
#kof>.kof-head>.kof-head-hp-0>div>div { background-color: lightgreen; height: 100%; width: 100%; float: right; }
#kof>.kof-head>.kof-head-hp-1>div>div { background-color: lightgreen; height: 100%; width: 100%; }
|
再实现js逻辑,Player类中
在构造函数最后
1 2
| this.$hp = this.root.$kof.find(`.kof-head-hp-${this.id}>div`); this.$hp_div = this.$hp.find('div');
|
在is_attack函数实现逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| is_attack() { if (this.status === 6) return;
this.status = 5; this.frame_current_cnt = 0; this.hp = Math.max(0, this.hp - 10);
this.$hp_div.animate({ width: this.$hp.parent().width() * this.hp / 100 }, 300); this.$hp.animate({ width: this.$hp.parent().width() * this.hp / 100 }, 600);
if (this.hp <= 0) { this.status = 6; this.frame_current_cnt = 0; } }
|
实现定时器功能
在GameMap类构造函数定义时间
1 2 3 4
| this.time_left = 60000;
this.$timer = this.root.$kof.find(".kof-head-timer");
|
在update函数里更新时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| update() { this.time_left -= this.timedelta; if (this.time_left < 0) { this.time_left = 0; let [a, b] = this.root.players; if (a.status !== 6 && b.status !== 6) { a.status = b.status = 6; a.frame_current_cnt = b.frame_current_cnt = 0; a.vx = b.vx = 0; } } this.$timer.text(parseInt(this.time_left / 1000));
this.render(); }
|
在Player类的is_attack函数里将死亡后的速度归零
1 2 3 4 5
| if (this.hp <= 0) { this.status = 6; this.frame_current_cnt = 0; this.vx = 0; }
|


xujiaojiao
生活明朗,万物可爱
此文章版权归xujiaojiao所有,如有转载,请注明来自原作者