Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Commit

Permalink
增加野怪,增加背景音乐。
Browse files Browse the repository at this point in the history
下一次更新考虑实现多线程播放音乐,防止播放音乐时引起卡顿。
  • Loading branch information
Wider committed May 3, 2020
1 parent b8d0ee2 commit 2200177
Show file tree
Hide file tree
Showing 31 changed files with 473 additions and 281 deletions.
65 changes: 45 additions & 20 deletions BaseObject.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "BaseObject.h"

Vector Vector::operator = (const Vector& rhs) {
Vector Vector::operator = (const Vector& rhs) {
_x = rhs._x;
_y = rhs._y;
return *this;
}
Vector Vector::operator + (const Vector& rhs) const {
Vector Vector::operator + (const Vector& rhs) const {
return Vector(_x + rhs._x, _y + rhs._y);
}
Vector Vector::operator - (const Vector& rhs) const {
Vector Vector::operator - (const Vector& rhs) const {
return Vector(_x - rhs._x, _y - rhs._y);
}
Vector Vector::operator * (const double rhs) const {
Vector Vector::operator * (const double rhs) const {
return Vector(_x * rhs, _y * rhs);
}
Vector Vector::operator / (const double rhs) const {
Vector Vector::operator / (const double rhs) const {
return Vector(_x / rhs, _y / rhs);
}
Vector Vector::operator += (const Vector& rhs) {
Expand All @@ -33,15 +33,15 @@ Vector Vector::operator /= (const double rhs) {
*this = *this / rhs;
return *this;
}
bool Vector::operator == (const Vector& rhs) const {
bool Vector::operator == (const Vector& rhs) const {
if(_x == rhs._x && _y == rhs._y) return true;
else return false;
}
bool Vector::operator == (const double rhs) const {
bool Vector::operator == (const double rhs) const {
if(_x == rhs && _y == rhs) return true;
else return false;
}
bool Vector::operator != (const Vector& rhs) const {
bool Vector::operator != (const Vector& rhs) const {
if(*this == rhs) return false;
else return true;
}
Expand All @@ -51,14 +51,14 @@ void Vector::x(double x) { _x = x; }
void Vector::y(double y) { _y = y; }
void Vector::set(double x, double y) { _x = x, _y = y; }

void Figure::addFigure(string name, vector<IMAGE> imgs, vector<IMAGE> masks, function<bool(void)> tigger) {
void Figure::addFigure(string name, vector<IMAGE> imgs, vector<IMAGE> masks, function<bool(void)> tigger) {
if(figures.empty() && !imgs.empty()) {
_width = imgs.front().getwidth();
_height = imgs.front().getheight();
}
figures.push_back(FigureData{name, imgs, masks, tigger});
}
void Figure::update(int x, int y) {
void Figure::update(int x, int y) {
for(auto& i : figures)
{
if(i.tigger()) {
Expand All @@ -81,7 +81,7 @@ void Figure::update(int x, int y) {
}
}
}
void Figure::turn() {
void Figure::turn() {
for(auto& i : figures)
{
for(auto& j : i.images) rotateFlip(&j);
Expand All @@ -92,36 +92,61 @@ string Figure::status()
{
return _status;
}
void Figure::status(string s)
void Figure::status(string s)
{
_status = s;
}
int Figure::width() {
int Figure::width() {
return _width;
}
int Figure::height() {
int Figure::height() {
return _height;
}

int BaseObject::width() {
int BaseObject::counter = 0;

int BaseObject::width() {
return _width;
}
int BaseObject::height() {
int BaseObject::height() {
return _height;
}
void BaseObject::width(int w) {
int BaseObject::get_id()
{
return id;
}
string BaseObject::type()
{
return _type;
}
void BaseObject::width(int w) {
_width = w;
}
void BaseObject::height(int h) {
void BaseObject::height(int h) {
_height = h;
}
void BaseObject::update(double time) {
void BaseObject::type(string t)
{
_type = t;
}
void BaseObject::update(double time) {
if(killed)return;
position += (velocity * time + acceleration * time * time / 2);
//position += velocity * time;
velocity += acceleration * time;
}
void BaseObject::show(Vector& offset) {
void BaseObject::show(Vector& offset) {

figure.update(static_cast<int>(round(position.x()) + offset.x()), static_cast<int>(round(position.y()) + offset.y()));
}


void BaseObject::kill()
{
killed = true;
}

bool BaseObject::is_killed()
{
return killed == true;
}
21 changes: 19 additions & 2 deletions BaseObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,42 @@ class Figure {
int height();
};


class BaseObject
{
private:
int _width;
int _height;
string _type;
int id;
static int counter;
bool killed;
public:
Position position;
Velocity velocity;
Acceleration acceleration;
Figure figure;
BaseObject():_width(0),_height(0),position(), velocity(), acceleration(0, GRAVITY), figure(){}
BaseObject():
id(counter++),_width(0),_height(0),_type(),position(), velocity(), acceleration(0, GRAVITY), figure(), killed(0) {}
BaseObject(const double x, const double y):
id(counter++), _width(0), _height(0), _type(), position(x,y), velocity(), acceleration(0, GRAVITY), figure(), killed(0) {}
BaseObject(const double x, const double y, int width, int height):
id(counter++), _width(width), _height(height), _type(), position(x, y), velocity(), acceleration(0, GRAVITY), figure(), killed(0) {}
BaseObject(const double x, const double y, int width, int height, string type):
id(counter++), _width(width), _height(height), _type(type), position(x, y), velocity(), acceleration(0, GRAVITY), figure(), killed(0) {}
~BaseObject() { counter--; }
int side_crash = 0;
int width();
int height();
int get_id();
string type();
void width(int w);
void height(int h);
void type(string t);
void update(double time);
void show(Vector& offset);
virtual void kill();
bool is_killed();
virtual void init() = 0;
};


Expand Down
1 change: 1 addition & 0 deletions Bullet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Bullet.h"
7 changes: 7 additions & 0 deletions Bullet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include "BaseObject.h"
class Bullet:
public BaseObject
{
};

40 changes: 40 additions & 0 deletions Controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "Controller.h"

void Controller::play_music(string type)
{
if(type == "welcome") {
mciSendString(_T("close all"), NULL, 0, NULL);//界岺杰唐稜있
mciSendString(_T("open \"assets\\audios\\bg.mp3\" alias welcome"), NULL, 0, NULL);//뻑短稜있
mciSendString(_T("play welcome repeat"), NULL, 0, NULL);//琦뻔꺄렴
}
else if(type == "gaming") {
mciSendString(_T("close all"), NULL, 0, NULL);//界岺杰唐稜있
mciSendString(_T("open \"assets\\audios\\bg.mp3\" alias gaming"), NULL, 0, NULL);//교쒼稜있
mciSendString(_T("play gaming"), NULL, 0, NULL);//琦뻔꺄렴
}
else if(type == "accele") {
mciSendString(_T("stop gaming"), NULL, 0, NULL);//界岺杰唐稜있
mciSendString(_T("open \"assets\\audios\\bg_accele.mp3\" alias accele"), NULL, 0, NULL);//교쒼稜있속醵
mciSendString(_T("play accele"), NULL, 0, NULL);//琦뻔꺄렴
}
else if(type == "jump") {
mciSendString(_T("close jump"), NULL, 0, NULL);
mciSendString(_T("open \"assets\\audios\\jump.mp3\" alias jump"), NULL, 0, NULL);//契禿稜있
mciSendString(_T("play jump"), NULL, 0, NULL);//琦뻔꺄렴
}
else if(type == "win") {
mciSendString(_T("close all"), NULL, 0, NULL);//界岺杰唐稜있
mciSendString(_T("open \"assets\\audios\\win.mp3\" alias win"), NULL, 0, NULL);//價적稜있
mciSendString(_T("play win"), NULL, 0, NULL);//琦뻔꺄렴
}
else if(type == "mario death") {
mciSendString(_T("close all"), NULL, 0, NULL);//界岺杰唐稜있
mciSendString(_T("open \"assets\\audios\\mario_death.mp3\" alias mario_death"), NULL, 0, NULL);//呵겨稜있
mciSendString(_T("play mario_death"), NULL, 0, NULL);//琦뻔꺄렴
}
else if(type == "enemy death") {
mciSendString(_T("close enemy_death"), NULL, 0, NULL);
mciSendString(_T("open \"assets\\audios\\enemy_death.mp3\" alias enemy_death"), NULL, 0, NULL);//柰밍价空稜있
mciSendString(_T("play enemy_death"), NULL, 0, NULL);//琦뻔꺄렴
}
}
26 changes: 26 additions & 0 deletions Controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#ifndef _CONTROLLER_H_
#define _CONTROLLER_H_
#include "Global.h"
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")
#include <windows.h>

class Controller
{
private:

public:
void welcome();
void win();
void lose();
void play_music(string type);
void show_welcome_ui();
void show_gaming_ui();
void show_lose_ui();
void show_help_ui();
};


#endif
81 changes: 81 additions & 0 deletions Enemy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "Enemy.h"

void Enemy::init()
{
LPCTSTR img_src, mask_src;
img_src = _T("assets\\images\\enemy.png");
mask_src = _T("assets\\images\\enemy_mask.png");
int figure_num = 4;
IMAGE origin, organ_mask;
loadimage(&origin, img_src);
loadimage(&organ_mask, mask_src);
int width = origin.getwidth() / figure_num;
int height = origin.getheight();
this->width(width);
this->height(height);
vector<IMAGE> imgs, masks;
IMAGE temp;

imgs.clear(); masks.clear();
for(int i = 0; i < 2; i++) {
SetWorkingImage(&origin);
getimage(&temp, 0 + width * i, 0, width, height);
imgs.push_back(temp);
SetWorkingImage(&organ_mask);
getimage(&temp, 0 + width * i, 0, width, height);
masks.push_back(temp);
}
figure.addFigure("running", imgs, masks, [this]()->bool {
if(!is_killed()) return true;
return false;
});



imgs.clear(); masks.clear();
for(int i = 2; i < 4; i++) {
SetWorkingImage(&origin);
getimage(&temp, 0 + width * i, 0, width, height);
imgs.push_back(temp);
SetWorkingImage(&organ_mask);
getimage(&temp, 0 + width * i, 0, width, height);
masks.push_back(temp);
}
figure.addFigure("killed", imgs, masks, [this]()->bool {
if(is_killed()) return true;
return false;
});
SetWorkingImage(NULL);

}

void Enemy::kill()
{
BaseObject::kill();
kill_tick = steady_clock::now();
}



void Enemy::move_range(double a, double b)
{
range_left = a;
range_right = b;
}

double Enemy::move_range_left()
{
return range_left;
}

double Enemy::move_range_right()
{
return range_right;
}

bool Enemy::is_removed()
{
if(is_killed()&&duration<double, std::milli>(steady_clock::now() - kill_tick).count() > 750)
return true;
return false;
}
20 changes: 20 additions & 0 deletions Enemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include "BaseObject.h"
class Enemy: public BaseObject
{
private:
double range_left, range_right;
time_point<steady_clock> kill_tick;
public:
Enemy():BaseObject(unit_length * 4, unit_length * 11),range_left(0),range_right(0){}
Enemy(const double x, const double y,const double range_left,const double range_right):
BaseObject(x,y),range_left(range_left),range_right(range_right){}
void init();
void kill();
void move_range(double a, double b);
double move_range_left();
double move_range_right();
bool is_removed();

};

2 changes: 2 additions & 0 deletions Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#define WINDOWS_WIDTH 700
#define WINDOWS_HEIGHT 504

#define unit_length 36

#define DEBUG
#ifdef DEBUG
#include<iostream>
Expand Down
Loading

0 comments on commit 2200177

Please sign in to comment.