The program does not start properly.

Asked 2 years ago, Updated 2 years ago, 31 views

I created a program like the one below, but when I try to move people, the background disappears or the UFO disappears.
Actually, I'm trying to create something that moves people, but keeps the background intact, and keeps the UFO moving without operating it.

Please tell me why it disappears, and I would appreciate it if you could tell me how to do it later.

#include<stdio.h>
# include <handy.h>
# include <time.h>
# include <math.h>

// A function that depicts a human beings
void humanA1 (int x1, int x2);
void humanA2 (int x1, int x2);
void humanB1 (int x1, int x2);
void humanB2 (int x1, int x2);

void UFO(inta);
void view (void);

// void plane1 (inta, intgid1);
// void plane2(intgid2);
// void Flag (inta, intgid1, intgid2);

int main() {
    int x1 = 0;
    int x2 = 0;
    inta = 0;
    intv;
    int key;
    int count1 = 0;
    int count2 = 0;
    //  intgid1 = HgImageLoad("war_stealth_aircraft.png");
    //  intgid2 = HgImageLoad("war_stealth_aircraft2.png");

    HgOpen (600,600);
    // Background
    view();

    for(;;){

        key = HgGetChar();
        HgClear();
        // Background
        view();
        // UFO
        UFO(a);
        a = a-10;
        HgClear();

        if(a==-400){
            a = a + 10;
            HgClear();
            UFO(a);
            HgSleep(0.05);
        } else if(a==540) {
            a = a-10;
            HgClear();
            UFO(a);
            HgSleep(0.05);
        }

        // Move to the left
        if(key=='a'){
            printf("%d\n", (500+a));
            count1++;

            if(count1%2==0){
                humanA2 (x1, x2);
                x1 = x1-10;
            } else {
                humanB2 (x1, x2);
                x1 = x1-10;
            }
        }

        // Move to the right
        if(key=='d'){
            printf("%d\n", (500+a));
            count2++;

            if(count2%2==0){
                humanA1 (x1, x2);
                x1 = x1 + 10;
            } else {
                humanB1 (x1, x2);
                x2 = x2 + 10;
            }    
        }
    }

    HgGetChar();
    HgClose();

    return 0;
}

void humanA1(intx1, intx2) {// Rightward
    HgSetFillColor(HG_ORANGE);
    HgCircleFill (300+x1+x2,75,25,0); // Head
    HgLine (300+x1+x2,50,300+x1+x2,25); // fuselage
    HgLine (275+x1+x2,38,325+x1+x2,38); // Shoulder to wrist
    HgLine (275+x1+x2,38,275+x1+x2,25); // Left hand
    HgLine (325+x1+x2,38,325+x1+x2,50); // Migi hand
    HgLine (300+x1+x2,25,275+x1+x2,0); // Left foot
    HgLine (300+x1+x2,25,325+x1+x2,25); // Big thigh
    HgLine (325+x1+x2,25,325+x1+x2,0); // Right foot
}
void humanA2(int x1, int x2) {//left-facing
    HgSetFillColor(HG_ORANGE);
    HgCircleFill (300+x1+x2,75,25,0); // Head
    HgLine (300+x1+x2,50,300+x1+x2,25); // fuselage
    HgLine (275+x1+x2,38,325+x1+x2,38); // Shoulder to wrist
    HgLine (275+x1+x2,38,275+x1+x2,50); // Left hand
    HgLine (325+x1+x2,38,325+x1+x2,25); // Migi hand
    HgLine (300+x1+x2,25,275+x1+x2,25); // Left thigh
    HgLine (275+x1+x2,25,275+x1+x2,0); // Left foot
    HgLine (300+x1+x2,25,325+x1+x2,0); // Right foot
}

void humanB1(intx1, intx2) {// Rightward
    HgSetFillColor(HG_ORANGE);
    HgCircleFill (300+x1+x2,75,25,0); // Head
    HgLine (300+x1+x2,50,300+x1+x2,25); // fuselage
    HgLine (300+x1+x2,38,275+x1+x2,30); // Left wrist
    HgLine (275+x1+x2,30,290+x1+x2,25); // Left hand
    HgLine (300+x1+x2,38,325+x1+x2,30); // Migi wrist
    HgLine (325+x1+x2,30,325+x1+x2,35); // Right hand
    HgLine (300+x1+x2,25,290+x1+x2,0); // Left foot
    HgLine (300+x1+x2,25,325+x1+x2,13); // Right ankle
    HgLine (325+x1+x2,13,312+x1+x2,0); // Right foot
}

void humanB2(int x1, int x2) {//left-facing
    HgSetFillColor(HG_ORANGE);
    HgCircleFill (300+x1+x2,75,25,0); // Head
    HgLine (300+x1+x2,50,300+x1+x2,25); // fuselage
    HgLine (300+x1+x2,38,275+x1+x2,30); // Left wrist
    HgLine (275+x1+x2,30,290+x1+x2,35); // Left hand
    HgLine (300+x1+x2,38,325+x1+x2,30); // Migi wrist
    HgLine (325+x1+x2,30,310+x1+x2,25); // Right hand
    HgLine (300+x1+x2,25,280+x1+x2,13); // Left thigh
    HgLine (280+x1+x2,13,290+x1+x2,0); // Left foot
    HgLine (300+x1+x2,25,310+x1+x2,0); // Right foot
}

void UFO(inta){
    HgSetFillColor(HG_GRAY);
    HgFanFill (500+a, 550, 30, 0, M_PI, 1);
    HgOvalFill(500+a,530,50,25,0,1);
    HgBoxFill (470+a, 465, 60, 40, 1);
}

void view (void) {
    HgSetFillColor(HG_GREEN);
    HgFanFill(50, 200, 300, 1.25*M_PI, 1.75*M_PI, 1);
    HgFanFill (300, 300, 500, 1.25*M_PI, 1.75*M_PI, 1);
    HgFanFill (550, 200, 300, 1.25*M_PI, 1.75*M_PI, 1);
}

c

2022-09-30 17:31

1 Answers

Is it "Handy Graphic"?I don't know much about it, so I'll give you advice only from an algorithmic point of view.
Typical pseudocodes for drawing updates in response to user input are:

//-----------------------------------------------
Variable position variable; // X/Y position of UFO person, etc.

for {//(1)
  Clear all ( );
  Drawing all (position variables); // The easiest way to draw all the background, UFO, people, etc. every time

 Input results = Waiting for user input ();

  switch {

  // ·····································

  }// Back to (1)
}
//-----------------------------------------

Unlike the algorithm above, the published code is
"Drawing only when there is an action."


2022-09-30 17:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.