21xrx.com
2024-09-20 05:49:56 Friday
登录
文章检索 我的文章 写文章
C++爱心简单代码大全
2023-06-30 19:24:00 深夜i     --     --
C++ 爱心简单代码 大全

在C++编程的世界中,设计出一个简单且有趣的小项目是做题必修的一环。而近年来,爱心项目逐渐走红,成为了C++初学者们的最爱。在本文中,我们将为您提供C++爱心简单代码大全,希望能够对您的编程学习有所帮助。

1. 打印一个简单的爱心图案

在控制台中使用字符打印出一个简单的爱心图案,代码如下:


#include <iostream>

int main()

{

  std::cout << " ****" << std::endl;

  std::cout << " *  *" << std::endl;

  std::cout << "*   *" << std::endl;

  std::cout << "*   *" << std::endl;

  std::cout << " *  *" << std::endl;

  std::cout << " ****" << std::endl;

  std::cout << "  *" << std::endl;

  std::cout << "  *" << std::endl;

  std::cout << "   *" << std::endl;

  std::cout << "   *" << std::endl;

  std::cout << "    *" << std::endl;

  return 0;

}

2. 绘制一个彩色爱心

使用C++的graphics.h库绘制一个彩色爱心,代码如下:


#include <graphics.h>

int main()

{

  initwindow(800, 600, "Colorful Heart");

  setcolor(RED);

  setfillstyle(SOLID_FILL, RED);

  ellipse(400, 300, 0, 360, 200, 250);

  floodfill(400, 300, RED);

  setcolor(YELLOW);

  setfillstyle(SOLID_FILL, YELLOW);

  ellipse(500, 300, 0, 360, 200, 250);

  floodfill(500, 300, YELLOW);

  setcolor(BLUE);

  setfillstyle(SOLID_FILL, BLUE);

  ellipse(450, 400, 0, 360, 200, 250);

  floodfill(450, 400, BLUE);

  getchar();

  closegraph();

  return 0;

}

3. 制作一个爱心屏保

使用C++的graphics.h库和计时函数制作一个爱心屏保,代码如下:


#include <graphics.h>

#include <time.h>

int main()

{

  initwindow(800, 600);

  srand((unsigned)time(NULL));

  while (1)

  {

    setcolor(rand() % 15 + 1);

    setfillstyle(SOLID_FILL, rand() % 15 + 1);

    ellipse(rand() % 800, rand() % 600, 0, 360, rand() % 100, rand() % 150);

    floodfill(rand() % 800, rand() % 600, rand() % 15 + 1);

    delay(50);

    cleardevice();

  }

  return 0;

}

4. 通过键盘输入让爱心动起来

利用键盘输入和计时函数实现让爱心动起来,代码如下:


#include <graphics.h>

#include <conio.h>

int main()

{

  initwindow(800, 600);

  int x = 200, y = 200;

  while (1)

  {

    if (_kbhit())

    {

      char ch = _getch();

      if (ch == 'a' && x >= 0)

      {

        x -= 10;

      }

      else if (ch == 'd' && x <= 600)

      {

        x += 10;

      }

      else if (ch == 'w' && y >= 0)

      {

        y -= 10;

      }

      else if (ch == 's' && y <= 400)

      {

        y += 10;

      }

    }

    setcolor(RED);

    setfillstyle(SOLID_FILL, RED);

    ellipse(x - 50, y + 50, 0, 360, 50, 75);

    floodfill(x - 50, y + 50, RED);

    setcolor(RED);

    setfillstyle(SOLID_FILL, RED);

    ellipse(x + 50, y + 50, 0, 360, 50, 75);

    floodfill(x + 50, y + 50, RED);

    setcolor(RED);

    setfillstyle(SOLID_FILL, RED);

    ellipse(x, y, 0, 360, 100, 125);

    floodfill(x, y, RED);

    delay(50);

    cleardevice();

  }

  return 0;

}

5. 制作一个简单的游戏

利用键盘输入和碰撞检测制作一个简单的游戏,玩家需要控制一个小爱心躲避障碍物,代码如下:


#include <graphics.h>

#include <conio.h>

bool collision(int x, int y)

{

  if (x >= 300 && x <= 500 && y >= 225 && y <= 350)

  {

    return true;

  }

  return false;

}

int main()

{

  initwindow(800, 600);

  int x = 400, y = 300;

  int score = 0;

  while (1)

  {

    if (_kbhit())

    {

      char ch = _getch();

      if (ch == 'a' && x >= 0)

      {

        x -= 10;

      }

      else if (ch == 'd' && x <= 700)

      {

        x += 10;

      }

      else if (ch == 'w' && y >= 0)

      {

        y -= 10;

      }

      else if (ch == 's' && y <= 550)

      {

        y += 10;

      }

    }

    if (collision(x, y))

    {

      score++;

      cleardevice();

      settextstyle(10, 0, 3);

      setcolor(YELLOW);

      outtextxy(350, 100, "You hit a heart! But you are still alive!");

      settextstyle(10, 0, 1);

      setcolor(WHITE);

      outtextxy(650, 50, "Score: ");

      outtextxy(720, 50, std::to_string(score).c_str());

    }

    else

    {

      setcolor(RED);

      setfillstyle(SOLID_FILL, RED);

      ellipse(x, y, 0, 360, 75, 100);

      floodfill(x, y, RED);

      setcolor(WHITE);

      setfillstyle(SOLID_FILL, WHITE);

      ellipse(400, 275, 0, 360, 100, 125);

      floodfill(400, 275, WHITE);

      setcolor(WHITE);

      setfillstyle(SOLID_FILL, WHITE);

      rectangle(300, 225, 500, 350);

      floodfill(301, 226, WHITE);

    }

    delay(50);

    cleardevice();

  }

  return 0;

}

以上就是C++爱心简单代码大全,希望可以给您的编程学习带来一些帮助。在实际编程中,您还可以结合自己的创意和思想,更好地设计出属于自己的爱心项目。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复