You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:graphics.h中line函数循环时无法执行的问题

Troubleshooting line() Function Failure in Looped Bar Chart Drawing (Code::Blocks 17.12 + graphics.h)

Hey there! Let's dig into why your line() calls stop working after the first loop iteration when drawing bar charts with the old graphics.h library in Code::Blocks 17.12. Here are the most common issues and fixes to get your charts updating correctly:

1. You're not clearing the previous frame's graphics

If you don't wipe the old drawing before rendering a new one, leftover pixels or stale drawing state can prevent new lines from appearing (or make them invisible under old content).

Fix: Add cleardevice() at the start of your loop. This clears the entire graphics window and resets the drawing context to a clean state:

while (/* your loop condition */) {
    cleardevice(); // Clear old content first
    // Your bar chart drawing code here...
}

2. Coordinate calculations or color settings are broken in the loop

It's easy to accidentally use stale variables for line coordinates, or set the line color to match the background (making lines invisible).

Fixes:

  • Double-check that variables used for line() coordinates (like bar heights, x-positions) are properly updated each loop iteration.
  • Explicitly set the line color at the start of each loop to avoid unexpected state changes:
    setcolor(WHITE); // Or any color contrasting your background
    

3. You're not flushing the graphics buffer

Some versions of graphics.h require manual buffer flushing to render new drawing commands, especially in tight loops. Without this, your new lines might stay in the buffer and never show up.

Fix: Add flushgraph() after your drawing code to push the new content to the screen. Pair it with a small delay to make updates visible:

// After drawing all bars/lines
flushgraph();
delay(500); // Adjust delay time as needed (milliseconds)

4. conio.h functions are interfering with the graphics context

If you're using getch() or kbhit() in the loop, they can sometimes block or alter the graphics library's state, especially if called at the wrong time.

Fix: Ensure input handling doesn't interrupt the drawing flow. For example, use kbhit() to check for input without blocking:

if (kbhit()) {
    char key = getch();
    if (key == 27) { // ESC key to exit
        break;
    }
}

Example Working Code Snippet

Here's a simplified version of your loop with all fixes applied:

#include <graphics.h>
#include <conio.h>
#include <iostream>
using namespace std;

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, ""); // Initialize graphics window
    setbkcolor(BLUE); // Set a distinct background color

    int barHeight1 = 100, barHeight2 = 150;
    while (true) {
        cleardevice();
        setcolor(WHITE);

        // Draw bar lines
        line(50, 400, 50, 400 - barHeight1);
        line(100, 400, 100, 400 - barHeight2);

        // Update bar heights for next iteration (simulate data change)
        barHeight1 = (barHeight1 + 20) % 300;
        barHeight2 = (barHeight2 + 30) % 300;

        flushgraph();
        delay(1000);

        // Exit on ESC press
        if (kbhit() && getch() == 27) break;
    }

    closegraph();
    return 0;
}

Quick Additional Checks

  • Verify your graphics.h library is properly installed in Code::Blocks (include paths linked, libraries added to the project).
  • Make sure any file reading logic (you mentioned fstream) is correctly reloading data each loop, so you're not drawing with stale values.

内容的提问来源于stack exchange,提问作者Beginer001234

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:00:20