Make a simple pixel paint slider game

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 7968d0a8ef
commit 1719b29ed7
  1. BIN
      C++ProjectTemplate
  2. 6
      Map.cpp
  3. 10
      Map.h
  4. 6
      Player.cpp
  5. 10
      Player.h
  6. BIN
      baseboard.png
  7. 156
      main.cpp

Binary file not shown.

@ -1,6 +0,0 @@
#include "Map.h"
void Map::test(){
printf("Hello Map\n");
}

10
Map.h

@ -1,10 +0,0 @@
#pragma once
#include <stdio.h>
class Player;
class Map{
public:
Player*p;
void test();
};

@ -1,6 +0,0 @@
#include "Player.h"
void Player::test(){
printf("Hello Player\n");
}

@ -1,10 +0,0 @@
#pragma once
#include <stdio.h>
class Map;
class Player{
public:
Map*map;
void test();
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@ -1,20 +1,156 @@
#include <memory>
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#define FILE_RESOLVE(url, file) emscripten_wget(url, file); emscripten_sleep(0)
#else
#define FILE_RESOLVE(url, file)
#endif
using namespace olc;
struct MyStruct{
int val=4;
friend std::ostream&operator<<(std::ostream&os,MyStruct&rhs){
printf("Called\n");
return os;
}
enum state{
PLAYER_CLICK,
PLAYER_DRAG,
BOARD_MOVE,
RED_WON,
BLUE_WON,
};
state GameState;
vi2d startingpos;
const float PI=3.14159;
float slidedir;
float lastUpdate=0;
const float TIME_PER_UPDATE=0.01f;
float acc=0;
vf2d boardpos;
// Override base class with your custom functionality
class SlideAndConquer : public olc::PixelGameEngine
{
public:
SlideAndConquer()
{
// Name your application
sAppName = "Slide and Conquer";
}
public:
olc::Renderable board;
olc::Renderable originalBoard;
void ResetGame() {
GameState=PLAYER_CLICK;
for (int x=0;x<256;x++) {
for (int y=0;y<256;y++) {
board.Sprite()->SetPixel(x,y,originalBoard.Sprite()->GetData()[y*256+x]);
}
}
board.Decal()->Update();
DrawSprite({0,0},board.Sprite());
}
bool OnUserCreate() override
{
board.Load("baseboard.png");
originalBoard.Load("baseboard.png");
GameState=PLAYER_CLICK;
DrawSprite({0,0},board.Sprite());
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
switch (GameState) {
case PLAYER_CLICK:{
DrawStringDecal({1,1},"CLICK ON THE PLAYFIELD",BLACK,{1,2});
if (GetMouse(0).bPressed) {
GameState=PLAYER_DRAG;
startingpos=GetMousePos();
}
}break;
case PLAYER_DRAG:{
float addedAngle=round((atan2f(GetMousePos().y-startingpos.y,GetMousePos().x-startingpos.x))/(PI/8))*(PI/8);
float dist=(GetMousePos()-startingpos).mag();
DrawDecal({sin(PI/2-addedAngle)*dist/30,cos(PI/2-addedAngle)*dist/30},board.Decal());
DrawPolygonDecal(nullptr,
{
{startingpos.x+sin(PI-addedAngle)*(dist/10),startingpos.y+cos(PI-addedAngle)*(dist/10)},
{startingpos.x+sin(-addedAngle)*(dist/10),startingpos.y+cos(-addedAngle)*(dist/10)},
{startingpos.x+sin(PI/2-addedAngle)*dist,startingpos.y+cos(PI/2-addedAngle)*dist}
},
{
{0,0},
{0,0},
{0,0},
},BLACK);
DrawStringDecal({1,1},"CHOOSE A SLIDE DIRECTION",BLACK,{1,2});
if (GetMouse(0).bPressed) {
slidedir=PI/2-addedAngle;
GameState=BOARD_MOVE;
boardpos={0,0};
}
}break;
case BOARD_MOVE:{
DrawStringDecal({1,1},"WAITING...",BLACK,{1,2});
if ((acc+=fElapsedTime)>TIME_PER_UPDATE) {
acc=0;
boardpos.x+=sin(slidedir);
boardpos.y+=cos(slidedir);
bool AllRed=true;
bool AllBlue=true;
if (boardpos.x>256||boardpos.x+256<0||boardpos.y>256||boardpos.y+256<0) {
GameState=PLAYER_CLICK;
SetDrawTarget(nullptr);
for (int x=0;x<256;x++) {
for (int y=0;y<256;y++) {
Pixel p = GetDrawTarget()->GetData()[y*256+x];
board.Sprite()->SetPixel(x,y,p);
if (p.r<p.b) {
AllRed=false;
}
if (p.b<p.r) {
AllBlue=false;
}
}
}
board.Decal()->Update();
if (AllRed) {
GameState=RED_WON;
}
if (AllBlue) {
GameState=BLUE_WON;
}
break;
} else {
DrawSprite(boardpos,board.Sprite());
}
}
}break;
case RED_WON:{
DrawStringDecal({1,1},"RED WINS! CLICK TO PLAY AGAIN",DARK_BLUE,{1,2});
if (GetMouse(0).bPressed) {
ResetGame();
}
}break;
case BLUE_WON:{
DrawStringDecal({1,1},"BLUE WINS! CLICK TO PLAY AGAIN",DARK_RED,{1,2});
if (GetMouse(0).bPressed) {
ResetGame();
}
}break;
}
return true;
}
};
int main()
{
MyStruct*st=new MyStruct({5});
std::cout<<*st<<std::endl;
return 0;
SlideAndConquer demo;
if (demo.Construct(256, 256, 4, 4))
demo.Start();
return 0;
}
Loading…
Cancel
Save