Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://vk.com/evgenykravchenko0
- ___ ___ ___
- / /\ ___ / /\ / /\
- / /:/_ /__/\ / /:/_ / /:/_
- / /:/ /\ \ \:\ / /:/ /\ / /:/ /\
- / /:/ /:/_ \ \:\ / /:/_/::\ / /:/ /:/_
- /__/:/ /:/ /\ ___ \__\:\ /__/:/__\/\:\ /__/:/ /:/ /\
- \ \:\/:/ /:/ /__/\ | |:| \ \:\ /~~/:/ \ \:\/:/ /:/
- \ \::/ /:/ \ \:\| |:| \ \:\ /:/ \ \::/ /:/
- \ \:\/:/ \ \:\__|:| \ \:\/:/ \ \:\/:/
- \ \::/ \__\::::/ \ \::/ \ \::/
- \__\/ ~~~~ \__\/ \__\/
- ___
- /__/\ ___ ___
- \ \:\ / /\ / /\
- \ \:\ / /:/ / /:/
- _____\__\:\ /__/::\ /__/::\
- /__/::::::::\ \__\/\:\__ \__\/\:\__
- \ \:\~~\~~\/ \ \:\/\ \ \:\/\
- \ \:\ ~~~ \__\::/ \__\::/
- \ \:\ /__/:/ /__/:/
- \ \:\ \__\/ \__\/
- \__\/
- #include <iostream>
- #include <whycpp/application_listener.h>
- #include <whycpp/palette.h>
- #include <whycpp/drawing.h>
- #include <whycpp/buttons.h>
- #include <whycpp/input.h>
- #include <whycpp/whycpp.h>
- #include <whycpp/time.h>
- #include <whycpp/lifecycle.h>
- using namespace std;
- class Hero {
- public:
- float x;
- float y;
- int width;
- int height;
- float speedX = 1;
- float speedY = 1;
- const RGBA color = PALETTE[3];
- Hero(int x, int y, int width, int height) : x(x), y(y), width(width), height(height) {}
- void Draw(Context &ctx) {
- DrawRect(ctx, x , y , 10, 10, color);
- }
- void MoveRight(Context &ctx) {
- if ( x < GetDisplayWidth(ctx) - 11)
- x += speedX / 4;
- }
- void MoveLeft(Context &ctx) {
- if (x > 0)
- x -= speedX / 4;
- }
- void MoveUP(Context &ctx) {
- if (y > 0)
- y -= speedY / 4;
- }
- void MoveDown(Context &ctx) {
- if (y < GetDisplayHeight(ctx) - 11)
- y += speedY / 4;
- }
- };
- class Ball {
- public:
- bool isGameOver;
- const shared_ptr<Hero> hero;
- float x;
- float y;
- int radius = 4;
- const RGBA color = PALETTE[13];
- float speedX;
- float speedY;
- Ball(int x, int y, int radius, float speedX, float speedY, const shared_ptr<Hero> hero)
- : hero(hero), x(x), y(y), radius(radius), speedX(speedX), speedY(speedY) {}
- void Draw(Context &ctx) {
- DrawRectFill(ctx, static_cast<int>(x - radius / 2), static_cast<int>(y - radius / 2), radius, radius, color);
- }
- void Move(Context &ctx) {
- CheckCollisions(ctx, x + speedX, y + speedY);
- x += speedX;
- y += speedY;
- }
- private:
- void CheckCollisions(Context &ctx, float _x, float _y) {
- if (_y > GetDisplayHeight(ctx)) {
- onGameOver();
- }
- //Walls
- if (_x < 0 || _x > GetDisplayWidth(ctx)) {
- speedX *= -1;
- }
- if (_y < 0 || _y > GetDisplayHeight(ctx)) {
- speedY *= -1;
- }
- //Hero
- auto hitY = (hero->y - hero->height / 2 - _y) < 0;
- auto lx = hero->x - hero->width / 2;
- auto rx = hero->x + hero->width / 2;
- auto hitX = (lx < _x) && (rx > _x);
- if (hitX && hitY) {
- speedY *= -1;
- }
- }
- void onGameOver() {
- isGameOver = true;
- }
- };
- class Game : public ApplicationListener {
- public:
- shared_ptr<Hero> hero;
- shared_ptr<Ball> ball;
- int ditch = 0;
- bool configured = false;
- void OnCreate(Context &ctx) override {
- auto w = GetDisplayWidth(ctx);
- auto h = GetDisplayHeight(ctx);
- hero = make_shared<Hero>(w / 2, h - 10, 50, 6);
- ball = make_shared<Ball>(w / 2, h / 2, 5, 0, 0, hero);
- }
- void OnRender(Context &ctx) override {
- if (!configured && IsClicked(ctx, Button::KEY_SPACE)) {
- configured = true;
- ball->speedX = 0.2;
- ball->speedY = 0.2;
- }
- if (ball->isGameOver) {
- DrawClearScreen(ctx, PALETTE[++ditch % PALETTE_LEN]);
- ExitApp(ctx);
- return;
- }
- DrawClearScreen(ctx, {255, 255, 255, 0});
- hero->Draw(ctx);
- if (IsPressed(ctx, Button::KEY_RIGHT)) {
- hero->MoveRight(ctx);
- }
- if (IsPressed(ctx, Button::KEY_LEFT)) {
- hero->MoveLeft(ctx);
- }
- if (IsPressed(ctx, Button::KEY_UP)) {
- hero->MoveUP(ctx);
- }
- if (IsPressed(ctx, Button::KEY_DOWN)) {
- hero->MoveDown(ctx);
- }
- ball->Draw(ctx);
- ball->Move(ctx);
- }
- };
- int main() {
- RunApp<Game>();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement