Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

[task_04_05]Kovaletsssss #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions trunk/ii200106/task_04_05/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Лабораторная работа № 4-5

##


## Реализация
## 1. При запуске приложения мы видим страницу с игрой
![](img/111.png)
## 2. При движении стрелочками персонаж двигается
![](img/121.png)
## 3. При проигрыще,пишет результат
![](img/131.png)
Binary file added trunk/ii200106/task_04_05/assets/box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added trunk/ii200106/task_04_05/assets/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added trunk/ii200106/task_04_05/assets/tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added trunk/ii200106/task_04_05/img/111.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added trunk/ii200106/task_04_05/img/121.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added trunk/ii200106/task_04_05/img/131.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions trunk/ii200106/task_04_05/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Runny</title>
<script type="text/javascript" src="js/phaser.min.js"></script>
<script type="text/javascript" src="js/boot.js"></script>
<script type="text/javascript" src="js/preload.js"></script>
<script type="text/javascript" src="js/gametitle.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/gameover.js"></script>
<style type="text/css">
body {
margin: 0;
overflow: hidden;
}
</style>
<script type="text/javascript">
(function() {

//Create a new game that fills the screen
game = new Phaser.Game(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio, Phaser.AUTO);

//Add all states
game.state.add("Boot", Boot);
game.state.add("Preload", Preload);
game.state.add("GameTitle", GameTitle);
game.state.add("Main", Main);
game.state.add("GameOver", GameOver);

//Start the first state
game.state.start("Boot");

})();
</script>
</head>
<body>
</body>
</html>
15 changes: 15 additions & 0 deletions trunk/ii200106/task_04_05/js/boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Boot = function(game){

};

Boot.prototype = {

preload: function(){

},

create: function(){
this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
this.game.state.start("Preload");
}
}
71 changes: 71 additions & 0 deletions trunk/ii200106/task_04_05/js/gameover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var GameOver = function(game){};

GameOver.prototype = {

create: function(){

this.game.stage.backgroundColor = '479cde';

this.quit = this.game.input.keyboard.addKey(Phaser.Keyboard.ESC);
this.resume = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
this.showScore();
},

update: function () {

if (this.resume.isDown) {
this.restartGame();
}
if (this.quit.isDown) {
// this.quitGame();
}

},

showScore: function () {

var scoreFont = "60px Arial";

this.scoreLabel = this.game.add.text(this.game.world.centerX
, this.game.world.centerY / 2, "0", { font: scoreFont, fill: "#fff" });
this.scoreLabel.anchor.setTo(0.5, 0.5);
this.scoreLabel.align = 'center';
this.game.world.bringToTop(this.scoreLabel);
this.scoreLabel.text = "Your score is " + (score);

this.highScore = this.game.add.text(this.game.world.centerX
, this.game.world.centerY, "0", { font: scoreFont, fill: "#fff" });
this.highScore.anchor.setTo(0.5, 0.5);
this.highScore.align = 'center';
this.game.world.bringToTop(this.highScore);

this.hs = window.localStorage.getItem('HighScore');

if (this.hs == null) {
this.highScore.setText("High score: " + score);
window.localStorage.setItem('HighScore', score)
}
else if (parseInt(this.hs) < score) {
this.highScore.setText("High score: " + (score ));
window.localStorage.setItem('HighScore', score)

}
else {
this.highScore.setText("High score: " + this.hs);
}

this.restart = this.game.add.text(this.game.world.centerX
, this.game.world.centerY * 1.5
, "Press \n Space to retry ", { font: scoreFont, fill: "#fff" });
this.restart.anchor.setTo(0.5, 0.5);
this.restart.align = 'center';
this.game.world.bringToTop(this.restart);
// this.scoreLabel.bringToTop()

},

restartGame: function(){
this.game.state.start("Main");
}

}
13 changes: 13 additions & 0 deletions trunk/ii200106/task_04_05/js/gametitle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var GameTitle = function(game){};

GameTitle.prototype = {

create: function(){

},

startGame: function(){
this.game.state.start("Main");
}

}
202 changes: 202 additions & 0 deletions trunk/ii200106/task_04_05/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
var Main = function(game){

};

var score = 0;

Main.prototype = {

create: function() {

this.tileVelocity = -450;
this.rate = 1500;
score = 0;

this.tileWidth = this.game.cache.getImage('tile').width;
this.tileHeight = this.game.cache.getImage('tile').height;
this.boxHeight = this.game.cache.getImage('box').height;

this.game.stage.backgroundColor = '479cde';


this.game.physics.startSystem(Phaser.Physics.ARCADE);

this.floor = this.game.add.group();
this.floor.enableBody = true;
this.floor.createMultiple(Math.ceil(this.game.world.width / this.tileWidth), 'tile');

this.boxes = this.game.add.group();
this.boxes.enableBody = true;
this.boxes.createMultiple(20, 'box');
this.game.world.bringToTop(this.floor);

this.jumping = false;

this.addBase();
this.createScore();
this.createPlayer();
this.cursors = this.game.input.keyboard.createCursorKeys();

this.timer = game.time.events.loop(this.rate, this.addObstacles, this);
this.Scoretimer = game.time.events.loop(100, this.incrementScore, this);

},

update: function() {

this.game.physics.arcade.collide(this.player, this.floor);
this.game.physics.arcade.collide(this.player, this.boxes, this.gameOver, null, this);

var onTheGround = this.player.body.touching.down;

// If the player is touching the ground, let him have 2 jumps
if (onTheGround) {
this.jumps = 2;
this.jumping = false;
}

// Jump!
if (this.jumps > 0 && this.upInputIsActive(5)) {
this.player.body.velocity.y = -1000;
this.jumping = true;
}

// Reduce the number of available jumps if the jump input is released
if (this.jumping && this.upInputReleased()) {
this.jumps--;
this.jumping = false;
}

},

addBox: function (x, y) {

var tile = this.boxes.getFirstDead();

tile.reset(x, y);
tile.body.velocity.x = this.tileVelocity;
tile.body.immovable = true;
tile.checkWorldBounds = true;
tile.outOfBoundsKill = true;
// tile.body.friction.x = 1000;
},

addObstacles: function () {
var tilesNeeded = Math.floor( Math.random() * (5 - 0));
// var gap = Math.floor( Math.random() * (tilesNeeded - 0));
if (this.rate > 200) {
this.rate -= 10;
this.tileVelocity = -(675000 / this.rate);

}

for (var i = 0; i < tilesNeeded; i++) {

this.addBox(this.game.world.width , this.game.world.height -
this.tileHeight - ((i + 1)* this.boxHeight ));

}
},

addTile: function (x, y) {

var tile = this.floor.getFirstDead();

tile.reset(x, y);
// tile.body.velocity.y = me.vel;
tile.body.immovable = true;
tile.checkWorldBounds = true;
tile.outOfBoundsKill = true;
// tile.body.friction.x = 1000;
},

addBase: function () {
var tilesNeeded = Math.ceil(this.game.world.width / this.tileWidth);
var y = (this.game.world.height - this.tileHeight);

for (var i = 0; i < tilesNeeded; i++) {

this.addTile(i * this.tileWidth, y);

}
},

upInputIsActive: function (duration) {
var isActive = false;

isActive = this.input.keyboard.downDuration(Phaser.Keyboard.UP, duration);
isActive |= (this.game.input.activePointer.justPressed(duration + 1000 / 60) &&
this.game.input.activePointer.x > this.game.width / 4 &&
this.game.input.activePointer.x < this.game.width / 2 + this.game.width / 4);

return isActive;
},

// This function returns true when the player releases the "jump" control
upInputReleased: function () {
var released = false;

released = this.input.keyboard.upDuration(Phaser.Keyboard.UP);
released |= this.game.input.activePointer.justReleased();

return released;
},

createPlayer: function () {

this.player = this.game.add.sprite(this.game.world.width/5, this.game.world.height -
(this.tileHeight*2), 'player');
this.player.scale.setTo(4, 4);
this.player.anchor.setTo(0.5, 1.0);
this.game.physics.arcade.enable(this.player);
this.player.body.gravity.y = 2200;
this.player.body.collideWorldBounds = true;
this.player.body.bounce.y = 0.1;
this.player.body.drag.x = 150;
var walk = this.player.animations.add('walk');
this.player.animations.play('walk', 20, true);

},

createScore: function () {

var scoreFont = "70px Arial";

this.scoreLabel = this.game.add.text(this.game.world.centerX, 70, "0", { font: scoreFont, fill: "#fff" });
this.scoreLabel.anchor.setTo(0.5, 0.5);
this.scoreLabel.align = 'center';
this.game.world.bringToTop(this.scoreLabel);

this.highScore = this.game.add.text(this.game.world.centerX * 1.6, 70, "0", { font: scoreFont, fill: "#fff" });
this.highScore.anchor.setTo(0.5, 0.5);
this.highScore.align = 'right';
this.game.world.bringToTop(this.highScore);

if (window.localStorage.getItem('HighScore') == null) {
this.highScore.setText(0);
window.localStorage.setItem('HighScore', 0);
}
else {
this.highScore.setText(window.localStorage.getItem('HighScore'));
}
// this.scoreLabel.bringToTop()

},

incrementScore: function () {


score += 1;
this.scoreLabel.setText(score);
this.game.world.bringToTop(this.scoreLabel);
this.highScore.setText("HS: " + window.localStorage.getItem('HighScore'));
this.game.world.bringToTop(this.highScore);


},

gameOver: function(){
this.game.state.start('GameOver');
}

};
27 changes: 27 additions & 0 deletions trunk/ii200106/task_04_05/js/phaser.min.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions trunk/ii200106/task_04_05/js/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var Preload = function(game){};

Preload.prototype = {

preload: function(){
this.game.load.image('tile', 'assets/tile.png');
this.game.load.image('box', 'assets/box.png');

this.game.load.spritesheet('player', 'assets/player.png', 24, 24, 9, -2);


},

create: function(){
this.game.state.start("Main");
}
}