📄 natureparkgame.cpp
字号:
#include "NatureParkGame.h"// ConstructorNatureParkGame::NatureParkGame() {}NatureParkGame::NatureParkGame(string _player1_name, string _player2_name) : player1_name(_player1_name), player2_name(_player2_name) { // Game is currently running alive = true; // Connect the mouse press signal to our handle_key_press function// key_press = CL_Keyboard::sig_key_down().connect(this, &NatureParkGame::handle_key_press); resources = CL_ResourceManager("resources.xml"); font_fps = CL_Font("my_fnt_1", &resources); font_player_name = CL_Font("my_fnt_2_sh", &resources); font_score = CL_Font("my_fnt_2_sh", &resources); winner = 3; b1 = NULL; b2 = NULL; load_graphics(); srand(time(0)); int seed = rand() % 65536; b1 = new NP_board(&resources, -5, 45, 91, 338, 0, 0, seed); b2 = new NP_board(&resources, -5, 467, 91, 412, 0, 0, seed); memset(down_time, 0, sizeof(down_time)); memset(first_time, 0, sizeof(first_time)); sample_background = CL_SoundBuffer("Game/Sound/sample_background", &resources); sample_background_session = sample_background.play(); sample_background_session.set_looping(true);}// DestructorNatureParkGame::~NatureParkGame() { delete b1; delete b2;}void NatureParkGame::load_graphics() { background = CL_Surface("Game/Graphics/background", &resources);}void NatureParkGame::on_send_bad_units_1(int x) { printf("player1 on_send_bad_units %d\n", x); b2->add_bad_units(x);}void NatureParkGame::on_get_score_1(int x) { printf("player1 on_get_score %d\n", x); player1_score += x;}void NatureParkGame::on_game_over_1() { printf("player1 on_game_over\n"); winner = 2; CL_SoundBuffer sample_win("Game/Sound/sample_win", &resources); sample_win.play();}void NatureParkGame::on_send_bad_units_2(int x) { printf("player2 on_send_bad_units %d\n", x); b1->add_bad_units(x);}void NatureParkGame::on_get_score_2(int x) { printf("player2 on_get_score %d\n", x); player2_score += x;}void NatureParkGame::on_game_over_2() { printf("player2 on_game_over\n"); winner = 1; CL_SoundBuffer sample_win("Game/Sound/sample_win", &resources); sample_win.play();}void NatureParkGame::run() { // Loop while the game is running and the user hasn't pressed escape CL_Slot slot_send_bad_units_1 = b1->sig_send_bad_units.connect(this, &NatureParkGame::on_send_bad_units_1); CL_Slot slot_get_score_1 = b1->sig_get_score.connect(this, &NatureParkGame::on_get_score_1); CL_Slot slot_game_over_1 = b1->sig_game_over.connect(this, &NatureParkGame::on_game_over_1); CL_Slot slot_send_bad_units_2 = b2->sig_send_bad_units.connect(this, &NatureParkGame::on_send_bad_units_2); CL_Slot slot_get_score_2 = b2->sig_get_score.connect(this, &NatureParkGame::on_get_score_2); CL_Slot slot_game_over_2 = b2->sig_game_over.connect(this, &NatureParkGame::on_game_over_2); CL_Slot slot_keyboard_key_down = keyboard.sig_key_down().connect(this, &NatureParkGame::on_key_down); CL_Slot slot_keyboard_key_up = keyboard.sig_key_up().connect(this, &NatureParkGame::on_key_up); while (alive && !CL_Keyboard::get_keycode(CL_KEY_ESCAPE)) { // Game update function paint(); CL_Display::flip(); CL_System::sleep(10); CL_System::keep_alive(); } sample_background_session.stop();}void NatureParkGame::paint() { background.draw(); if (winner == 0) { b1->update(); b1->draw(); b2->update(); b2->draw(); } else if (winner == 1) { font_player_name.draw(50, 52, "Winner"); font_player_name.draw(565, 52, "Loser"); } else if (winner == 2) { font_player_name.draw(50, 52, "Loser"); font_player_name.draw(565, 52, "Winner"); } char tmp1[100]; sprintf(tmp1, "%d", player1_score); string score1(tmp1); int t1 = font_player_name.get_width(score1); font_player_name.draw(235 - t1, 52, score1); char tmp2[100]; sprintf(tmp2, "%d", player2_score); string score2(tmp2); int t2 = font_player_name.get_width(score2); font_player_name.draw(750 - t2, 52, score2); font_fps.draw(0,0, "FPS: " + CL_String::from_int(framerate.get_fps())); font_player_name.draw(50, 28, player1_name); font_player_name.draw(565, 28, player2_name); handle_key_press();}void NatureParkGame::handle_key_press() { if (winner == 0) { int now_time = CL_System::get_time(); int TIME_PERIOD = 70; int FIRST_TIME_PERIOD = 400; if (down_time[CL_KEY_S] != 0) { if (first_time[CL_KEY_S] == 1) { if (now_time - down_time[CL_KEY_S] > FIRST_TIME_PERIOD) { b1->block_move_down(); down_time[CL_KEY_S] = now_time; first_time[CL_KEY_S] = 0; } } else { if (now_time - down_time[CL_KEY_S] > TIME_PERIOD) { b1->block_move_down(); down_time[CL_KEY_S] = now_time; } } } if (down_time[CL_KEY_A] != 0) { if (first_time[CL_KEY_A] == 1) { if (now_time - down_time[CL_KEY_A] > FIRST_TIME_PERIOD) { b1->block_move_left(); down_time[CL_KEY_A] = now_time; first_time[CL_KEY_A] = 0; } } else { if (now_time - down_time[CL_KEY_A] > TIME_PERIOD) { b1->block_move_left(); down_time[CL_KEY_A] = now_time; } } } if (down_time[CL_KEY_D] != 0) { if (first_time[CL_KEY_D] == 1) { if (now_time - down_time[CL_KEY_D] > FIRST_TIME_PERIOD) { b1->block_move_right(); down_time[CL_KEY_D] = now_time; first_time[CL_KEY_D] = 0; } } else { if (now_time - down_time[CL_KEY_D] > TIME_PERIOD) { b1->block_move_right(); down_time[CL_KEY_D] = now_time; } } } if (down_time[CL_KEY_W] != 0) { if (first_time[CL_KEY_W] == 1) { if (now_time - down_time[CL_KEY_W] > FIRST_TIME_PERIOD) { b1->block_rotate(); down_time[CL_KEY_W] = now_time; first_time[CL_KEY_W] = 0; } } else { if (now_time - down_time[CL_KEY_W] > TIME_PERIOD) { b1->block_rotate(); down_time[CL_KEY_W] = now_time; } } } if (down_time[CL_KEY_DOWN] != 0) { if (first_time[CL_KEY_DOWN] == 1) { if (now_time - down_time[CL_KEY_DOWN] > FIRST_TIME_PERIOD) { b2->block_move_down(); down_time[CL_KEY_DOWN] = now_time; first_time[CL_KEY_DOWN] = 0; } } else { if (now_time - down_time[CL_KEY_DOWN] > TIME_PERIOD) { b2->block_move_down(); down_time[CL_KEY_DOWN] = now_time; } } } if (down_time[CL_KEY_LEFT] != 0) { if (first_time[CL_KEY_LEFT] == 1) { if (now_time - down_time[CL_KEY_LEFT] > FIRST_TIME_PERIOD) { b2->block_move_left(); down_time[CL_KEY_LEFT] = now_time; first_time[CL_KEY_LEFT] = 0; } } else { if (now_time - down_time[CL_KEY_LEFT] > TIME_PERIOD) { b2->block_move_left(); down_time[CL_KEY_LEFT] = now_time; } } } if (down_time[CL_KEY_RIGHT] != 0) { if (first_time[CL_KEY_RIGHT] == 1) { if (now_time - down_time[CL_KEY_RIGHT] > FIRST_TIME_PERIOD) { b2->block_move_right(); down_time[CL_KEY_RIGHT] = now_time; first_time[CL_KEY_RIGHT] = 0; } } else { if (now_time - down_time[CL_KEY_RIGHT] > TIME_PERIOD) { b2->block_move_right(); down_time[CL_KEY_RIGHT] = now_time; } } } if (down_time[CL_KEY_UP] != 0) { if (first_time[CL_KEY_UP] == 1) { if (now_time - down_time[CL_KEY_UP] > FIRST_TIME_PERIOD) { b2->block_rotate(); down_time[CL_KEY_UP] = now_time; first_time[CL_KEY_UP] = 0; } } else { if (now_time - down_time[CL_KEY_UP] > TIME_PERIOD) { b2->block_rotate(); down_time[CL_KEY_UP] = now_time; } } } } else { if (down_time[CL_KEY_F2] > 0) { reset_game(); } }}void NatureParkGame::reset_game() { winner = 0; player1_score = 0; player2_score = 0; int seed = rand() % 65536; b1->reset_game(seed); b2->reset_game(seed); CL_SoundBuffer sample_ready("Game/Sound/sample_ready", &resources); CL_SoundBuffer sample_go("Game/Sound/sample_go", &resources); sample_ready.play(); CL_System::sleep(1000); sample_go.play(); b1->start_game(); b2->start_game();}void NatureParkGame::on_key_down(const CL_InputEvent& key) { if (down_time[key.id] == 0) { first_time[key.id] = 1; down_time[key.id] = CL_System::get_time(); }}void NatureParkGame::on_key_up(const CL_InputEvent& key) { down_time[key.id] = 0x7FFFFFFF; handle_key_press(); down_time[key.id] = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -