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

如何在Turbo-C中通过鼠标触发动画?计算机图形学项目求助

Mouse-Triggered Animations in Turbo C for Graphics Projects

Hey there! I remember wrestling with this exact issue back when I was working on Turbo C graphics projects—getting mouse clicks to kick off animations instead of keyboard inputs is totally achievable, you just need to work with DOS interrupts since Turbo C doesn’t have built-in mouse support. Let’s break this down step by step.

Key Concepts to Know

Turbo C relies on the DOS mouse interrupt (int 0x33) for all mouse operations. This interrupt has different functions to initialize the mouse, check button status, get coordinates, and more. Here’s what you’ll need:

  • Initialize the Mouse: First, you have to tell the system you want to use the mouse.
  • Detect Mouse Clicks: Continuously check if a mouse button (like left-click) is pressed.
  • Trigger Animation: Once a click is detected, run your animation logic, then wait for the button to release to avoid repeated triggers.

Full Working Code Example

This code sets up a graphics window, waits for a left-click, then runs a simple moving circle animation at the click position:

#include <graphics.h>
#include <dos.h>
#include <conio.h>

// Initialize mouse and return success status (1 = success, 0 = failure)
int initMouse() {
    union REGS regs;
    regs.x.ax = 0; // Function 0: Initialize mouse
    int86(0x33, &regs, &regs);
    return regs.x.ax;
}

// Get current mouse button status and coordinates
void getMouseState(int *buttonStatus, int *xPos, int *yPos) {
    union REGS regs;
    regs.x.ax = 3; // Function 3: Get mouse state & position
    int86(0x33, &regs, &regs);
    *buttonStatus = regs.x.bx; // Bit 0 = left click, Bit 1 = right click
    *xPos = regs.x.cx;
    *yPos = regs.x.dx;
}

// Sample animation: Moving circle from click position
void playAnimation(int startX, int startY) {
    for (int offset = 0; offset < 100; offset++) {
        cleardevice(); // Clear screen for next frame
        circle(startX + offset, startY, 25); // Draw circle at new position
        delay(30); // Control animation speed (ms)
    }
}

int main() {
    int graphDriver = DETECT, graphMode;
    initgraph(&graphDriver, &graphMode, ""); // Initialize graphics mode

    // Check if mouse initialization worked
    if (!initMouse()) {
        outtextxy(50, 50, "Mouse initialization failed!");
        outtextxy(50, 70, "Make sure mouse driver is loaded (mouse.com/mouse.sys)");
        delay(3000);
        closegraph();
        return 1;
    }

    int buttonState, mouseX, mouseY;
    outtextxy(50, 50, "Left-click anywhere to start animation!");
    outtextxy(50, 70, "Press ESC to exit");

    while (1) {
        getMouseState(&buttonState, &mouseX, &mouseY);

        // Check for left-click (bit 0 of buttonState is set)
        if (buttonState & 1) {
            cleardevice();
            playAnimation(mouseX, mouseY);
            outtextxy(50, 50, "Click again for another animation!");
            
            // Wait for mouse button release to avoid repeated triggers
            while (buttonState & 1) {
                getMouseState(&buttonState, &mouseX, &mouseY);
            }
        }

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

    closegraph();
    return 0;
}

Important Notes

  • Mouse Driver Requirement: In DOS (or DOSBox, which you’re probably using for Turbo C), you need to load the mouse driver first. Run mouse.com in the DOS prompt before launching Turbo C, or add it to your autoexec.bat file.
  • Coordinate Alignment: The mouse coordinates returned by int 0x33 match Turbo C’s graphics mode coordinates (y=0 at the top of the screen), so no conversion is needed.
  • Avoiding Repeat Triggers: The while loop waiting for button release ensures the animation only runs once per click, even if you hold the button down.
  • Animation Speed: Adjust the delay() value to make the animation faster or slower—lower values mean faster movement.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:34:15