PDA

View Full Version : نمونه کد توپ متحرک در کنسول با ++C



علیرضا.ا
پنج شنبه 02 مهر 1394, 09:29 صبح
سلام
یه کدی نوشتم گفتم بزارم شاید کسی لازمش داشته باشه...
این کد در کنسول یه مستطیل ایجاد میکنه و یه ستاره در نقطه ای تصادفی درون مستطیل قرار میگیره. بعد در یه جهت تصادفی حرکت میکنه و اگه به دیواره های مستطیل برخورد کرد تغییر جهت میده ( و همچنین رنگ کنسول عوض میشه)

اگه نظری چیزی در مورد برنامه دارین بگین خوشحال میشم :قلب::لبخندساده:

عکسی از محیط برنامه:

http://barnamenevis.org/attachment.php?attachmentid=135457&stc=1

سورس برنامه:

// Code By Al!reza Afzal Aghaei
// alirezaafzalaghaei@gmail.com
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#define WIDTH 70
#define HEIGHT 20
#define TIME 85
using namespace std;


class Ball {
private:
short x;
short y;
short direction;
unsigned char ch;
public:
Ball();
Ball(short, short, short,unsigned char);
void setX(short);
void setY(short);
void setXY(short, short);
void setDirection(short);
void setEmoji(unsigned char);
void setColor();
void Draw();
void Down_Right();
void Top_Right();
void Down_Left();
void Top_Left();
void move(short);
void bounce();
void start(short);
bool isFirstX();
bool isFirstY();
bool isLastX();
bool isLastY();
short getX();
short getY();
};


Ball::Ball()
{
x = y = 0;
direction = 2;
ch = 219;
}


Ball::Ball(short x, short y, short d = 2, unsigned char ch = '#')
{
setXY(x, y);
setDirection(d);
setEmoji(ch);
Draw();
}


void Ball::Draw()
{
system("cls");
for (int j = 0; j <= HEIGHT; j++)
{
for (int i = 0; i < WIDTH;i++)
{
if (i == WIDTH - 1)
cout << "+\n";
else if (j == HEIGHT)
cout << "+";
else if (i == x && j == y)
cout << ch;
else
cout << " ";
}
}
}


void Ball::setX(short p)
{
if (p >= 0 && p <= WIDTH)
x = p;
}


void Ball::setY(short q)
{
if (q >= 0 && q <= HEIGHT)
y = q;
}


void Ball::setXY(short p, short q)
{
setX(p);
setY(q);
}


void Ball::setDirection(short d)
{
if (d > 0 && d < 5)
direction = d;
}


void Ball::setEmoji(unsigned char c) {
if (c >= 0 && c < 256)
ch = c;
}


void Ball::setColor()
{
short i = rand() % 15;
vector<string> colors = {"0", "1", "2", "3", "4", "5", "6", "8", "9", "A", "B", "C", "D", "E", "F" };
system(("color " + colors[i] + "7").c_str());


}


void Ball::Down_Right()
{
setDirection(4);
setX(++x);
setY(++y);
Draw();
}


void Ball::Down_Left()
{
setDirection(3);
setX(--x);
setY(++y);
Draw();
}


void Ball::Top_Left()
{
setDirection(2);
setX(--x);
setY(--y);
Draw();
}


void Ball::Top_Right()
{
setDirection(1);
setX(++x);
setY(--y);
Draw();
}


void Ball::move(short s)
{
if (s == 1)
{
Top_Right();
Sleep(TIME);
}
else if (s == 2)
{
Top_Left();
Sleep(TIME);
}
else if (s == 3)
{
Down_Left();
Sleep(TIME);
}
else if (s == 4)
{
Down_Right();
Sleep(TIME);
}
}


void Ball::start(short d)
{
if (d == 1)
while (!isLastX() && !isFirstY())
move(d);
else if (d == 2)
while (!isFirstX() && !isFirstY())
move(d);
if (d == 3)
while (!isFirstX() && !isLastY())
move(d);
if (d == 4)
while (!isLastX() && !isLastY())
move(d);
setColor();
}


void Ball::bounce()
{
start(direction);
while (true)
{
if (isFirstX() && direction == 3)
start(4);
else if (isFirstX() && direction == 2)
start(1);
else if (isFirstY() && direction == 1)
start(4);
else if (isFirstY() && direction == 2)
start(3);
else if (isLastX() && direction == 4)
start(3);
else if (isLastX() && direction == 1)
start(2);
else if (isLastY() && direction == 3)
start(2);
else if (isLastY() && direction == 4)
start(1);
if (isFirstX() && isFirstY())
start(4);
else if (isLastX() && isFirstY())
start(3);
else if (isFirstX() && isLastY())
start(1);
else if (isLastX() && isLastY())
start(2);
}
}


bool Ball::isFirstX()
{
return x == 0;
}


bool Ball::isFirstY()
{
return y == 0;
}


bool Ball::isLastX()
{
return x==WIDTH-2;
}


bool Ball::isLastY()
{
return y == HEIGHT-1;
}


short Ball::getX()
{
return x;
}


short Ball::getY()
{
return y;
}


int main()
{
srand(unsigned(time(NULL)));
int X = rand() % WIDTH;
int Y = rand() % HEIGHT;
int Direction = rand() % 4 + 1;
Ball b(X,Y,Direction,'O');
b.bounce();
return 0;
}


فایل اجرایی برنامه:

# پیوست شد