码迷,mamicode.com
首页 > 其他好文 > 详细

实验三

时间:2019-04-22 22:41:46      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:step   line   sys   public   for   +=   using   实验   ++   

#include "ball.h"
#include <iostream>
#include <cstdlib>  
using std::cout;
using std::endl;

const int SIZE_X=50;  
const int SIZE_Y=50;

Ball::Ball(int x0, int y0):x(x0), y(y0) {

    for(int line=1; line <= y0-1; line++)
        cout << endl;
    

    for(int col=1; col <= x0-1; col++) 
        cout << " ";
    

    cout << "O" << endl; 
    
}

void Ball::left(int step) {
    x = x-step;
    if(x <= 0)
        x=0;
    

    system("cls");
    

    for(int line=1; line <= y-1; line++)
        cout << endl;
    

    for(int col=1; col <= x-1; col++) 
        cout << " ";
    

    cout << "O" << endl; 
    
}

void Ball::right(int step) {
    x = x+step;
    if(x >= SIZE_X)
        x=SIZE_X;
    

    system("cls");
    

    for(int line=1; line <= y-1; line++)
        cout << endl;
    

    for(int col=1; col <= x-1; col++) 
        cout << " ";
    

    cout << "O" << endl; 
    
} 

void Ball::up(int step) {
    y = y-step;
    if(y <= 0)
        y=0;
    

    system("cls");
    

    for(int line=1; line <= y-1; line++)
        cout << endl;
    

    for(int col=1; col <= x-1; col++) 
        cout << " ";
    

    cout << "O" << endl; 
    
}

void Ball::down(int step) {
    y = y+step;
    if(y >= SIZE_Y)
        y = SIZE_Y;
    

    system("cls");
    

    for(int line=1; line <= y-1; line++)
        cout << endl;
    

    for(int col=1; col <= x-1; col++) 
        cout << " ";
    

    cout << "O" << endl;     
}
#ifndef BALL_H
#define BALL_H

class Ball {
    public:
        Ball(int x0=0, int y0=0);  
        void left(int step=1);        
        void right(int step=1);    
        void up(int step=1);        
        void down(int step=1);     
    private:
        int x;
        int y; 
};

#endif
#include "canvas.h"
#include <cstdlib>

Canvas::Canvas(string bg0, string fg0):bg(bg0), fg(fg0) {
    string color = "color ";
    color += bg0;
    color += fg0;
    system(color.c_str());
} 
void Canvas::changeCanvasBg(string bg0) {
    bg = bg0; 
    
    string color = "color ";
    color += bg;
    color += fg;
    system(color.c_str());
    
}
void Canvas::changeCanvasFg(string fg0) {
    fg = fg0; 
    
    string color = "color ";
    color += bg;
    color += fg;
    system(color.c_str());
    
}
void Canvas::changeCanvasColor(string bg0, string fg0){
    bg = bg0; 
    fg = fg0; 
    
    string color = "color ";
    color += bg;
    color += fg;
    system(color.c_str());    
}
#ifndef CANVAS_H
#define CANVAS

#include <string>
using std::string;

class Canvas {
    public:
        Canvas(string bg0="0", string fg0="A");
        void changeCanvasBg(string bg0);
        void changeCanvasFg(string fg0);
        void changeCanvasColor(string bg0, string fg0); 
    private:
        string bg; 
        string fg;
};

#endif
#include <iostream>
#include "canvas.h"
#include "Ball.h"

int main() {
    Canvas canvas;
    
    Ball ball1(10,10);
    system("pause");
    
    ball1.left(5);
    system("pause");
    
    ball1.up(20);
    system("pause");

    canvas.changeCanvasFg("E");
    system("pause");
    
    canvas.changeCanvasBg("D");  
    system("pause");
    

    return 0;
}

 

实验三

标签:step   line   sys   public   for   +=   using   实验   ++   

原文地址:https://www.cnblogs.com/changtingzao/p/10753343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!