LoginSignup
0
0

More than 3 years have passed since last update.

#はじめてのC++【40日目】

Last updated at Posted at 2019-04-17

はじめに

openframeworksでクラスを作れました。クラスの便利さを体感しましたが、文章に書き表せるほどまだ理解できていない。

OF ブロック崩し

実行画面

スクリーンショット 2019-04-17 21.13.49.png

ソースコード

Bar.h
#ifndef BAR_H
#define BAR_H

#include <stdio.h>
#include "ofMain.h"


class Bar {
public:
  void init( float _width, float _height, float _speed );//初期化

  void update();//更新
  void display();//描画

  void keyPressed(int key);//キーボードの読み取り

  float xPos;//x座標
  float yPos;//y座標
  float width;//長さ
  float height;//高さ
  float speed;//動く速さ

  bool XPlus = true;//右に進む or 左に進む
//  bool YPlus = false;

};

#endif
Bar.cpp
#include "Bar.h"
#include "ofMAin.h"

void Bar::init( float _width, float _height, float _speed ){//初期化

  xPos = ofGetWidth() / 2.0;
  yPos = ofGetHeight() *  ( 8.0 / 10.0 );
  width = _width;
  height = _height;
  speed = _speed;

}

void Bar::update(){//バーを動かしたりする
  //xPos += speed;
  if( XPlus && xPos < ofGetWidth()-110 ){
      xPos += speed;
  }

  if( !XPlus && xPos > 0 ){
      xPos -= speed;
  }

}


void Bar::display(){//描画する
  ofRect( xPos, yPos, width, height);
}

void Bar::keyPressed(int key){

  if ( key == OF_KEY_RIGHT  ){//右のキーを押したら右に進む
    XPlus = true;
  }

  if ( key == OF_KEY_LEFT ){//左のキーを押したら左に進む

    XPlus = false;
  }

}
Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H

#include <stdio.h>
#include "ofMain.h"

class Circle {
public:

  void init( float _y, int _eSize, float _speed ); //初期化するときに使うやつ
  void update();//更新する
  void display();//描画

  //ofVec2f position;//これも使えるかも

  float xPos;
  float yPos;
  float eSize;//円の大きさ
  float speed;

  bool XPlus = true;//衝突判定のときに使う
  bool YPlus = false;

};

#endif
Circle.cpp

#include "Circle.h"

void Circle::init(float _y, int _eSize, float _speed){
  xPos = ofGetWidth() / 2;//真ん中の位置から始める。
  yPos = _y;//yは宣言時の引数
  eSize = _eSize;
  speed = _speed;//スピードも宣言できる。
}

void Circle::update(){


  if ( xPos > ofGetWidth() ){//壁の衝突判定 //バーとの衝突判定はまだできていない
    XPlus = false;
  }

  if ( xPos < 0 ){
    XPlus = true;
  }

  if ( yPos > ofGetHeight() ){
    YPlus = false;
  }

  if ( yPos < 0 ){
    YPlus = true;
  }


  XPlus ? xPos += speed : xPos -= speed;//壁反射
  YPlus ? yPos += speed : yPos -= speed;//壁反射


}

void Circle::display(){
  ofDrawCircle( xPos, yPos, eSize );//円byouga
}
ofApp.h
#pragma once

#include "ofMain.h"
#include "Circle.h"
#include "Bar.h"

class ofApp : public ofBaseApp{
    public:
        void setup();
        void update();
        void draw();

        void keyPressed(int key);
        void keyReleased(int key);
        void mouseMoved(int x, int y);
        void mouseDragged(int x, int y, int button);
        void mousePressed(int x, int y, int button);
        void mouseReleased(int x, int y, int button);
        void mouseEntered(int x, int y);
        void mouseExited(int x, int y);
        void windowResized(int w, int h);
        void dragEvent(ofDragInfo dragInfo);
        void gotMessage(ofMessage msg);

        Circle c1;
        Circle c2;

        Bar bar;


};
ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){

  ofBackground( 255, 255, 255 );
  ofSetCircleResolution(64);
  c1.init(100, 10, 1.0);//y座標,円の大きさ,スピード
  c2.init(200, 15, 3.0);
  bar.init(100,20, 10);//width,height,speed

}

//--------------------------------------------------------------
void ofApp::update(){

  c1.update();
  c2.update();
  bar.update();

}

//--------------------------------------------------------------
void ofApp::draw(){

  ofSetColor(0,0,0);
  c1.display();
  c2.display();
  bar.display();

}


//--------------------------------------------------------------
void ofApp::keyPressed(int key){
 bar.keyPressed(key);
}

感想

同じクラス内の関数に 変数 を共有できるのがクラスを使うメリットの一つかな。
BallとBarの衝突判定もなんとなく実装できそうな気がする。
それにしても、openframeworksって便利ですね。今は、keyボードからの入力で左右に動かしているけど、OpenCVにたいなの使ってカメラから自分の手を認識してBarを動かすのもやってみたい。

参考にしたホームページ

ロベールのC++

template

ずいぶん昔にこのようなコードを書いたことがある。


int Abso(int a){
  return a > 0? a : -a;
}

double Abso(double a){
  return a > 0? a : -a;
}

この場合は、 Absの引数 によって実行される関数が変わってくる。
ですが、ほとんど同じ形なので無駄だと思いますよね。こんなときに、 template を使います!
では、早速使ってみます。

template.cpp
#include <iostream>

template <typename TYPE>
  TYPE Abso(TYPE a)
  {
    return a > 0? a : -a;
  }

int main() {
  std::cout << Abso(-5) << std::endl;
  std::cout << Abso(-7.6) << std::endl;
  std::cout << Abso('c') << std::endl;
}
実行結果
5
7.6
c

 ' c ' もできるのだなぁ。

終わりに

おやすみなさい

0
0
9

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0