ÓÐÎÊ 12. Ðàáîòà ñ ãðàôèêîé (ïðîäîëæåíèå)
Ïðèâåäåì
åùå íåñêîëüêî ïðèìåðîâ ðàáîòû ñ ãðàôèêîé. Íèæå ïîêàçàíà ïðîãðàììà,
êîòîðàÿ âûâîäèò íà ýêðàí ïðÿìîóãîëüíèê, çàòåì ïðè íàæàòèè íà ëþáóþ
êëàâèøó ñî ñòðåëêîé, ïåðåìåùàåò åãî â íàïðàâëåíèè ñòðåëêè.
a$ = CHR$(0)
sp$ = a$ + CHR$(77)
sl$ = a$ + CHR$(75)
sv$ = a$ + CHR$(72)
sn$ = a$ + CHR$(80)
SCREEN 9
x = 150: y = 300
M1: LINE (x, y)-(x + 20, y + 20), , B
M2: a$ = INKEY$
IF a$ = "" THEN GOTO M2
COLOR 0
LINE (x, y)-(x + 20, y + 20), , B
COLOR 7
SELECT CASE a$
CASE sp$
x = x + 20: GOTO M1
CASE sl$
x = x - 20: GOTO M1
CASE sv$
y = y - 20: GOTO M1
CASE sn$
y = y + 20: GOTO M1
END SELECT
Êàê óæå ãîâîðèëîñü, êàæäûé ñèìâîë èìååò ñâîé êîä â òàáëèöå ASCII, íî ýòî íå âåðíî äëÿ âñåõ êëàâèøåé. Ñëóæåáíûå êëàâèøè Alt, Shift è äðóãèå, à òàêæå ñî÷åòàíèÿ ñ íèìè íå èìåþò àíàëîãîâ â êîäèðîâêå ASCII. Äëÿ òîãî, ÷òîáû èñïîëüçîâàòü ýòè êëàâèøè äëÿ ðàáîòû ñ ïðîãðàììàìè, èñïîëüçóåòñÿ ôóíêöèÿ INKEY$, êîòîðàÿ âîçâðàùàåò êîä â âèäå îäíîé èëè äâóõ öèôð äëÿ ñëóæåáíûõ êëàâèø. Ïðèâåäåì ïðèìåð ïðîãðàììû.
M1: a$ = INKEY$: IF a$ = "" THEN GOTO M1
IF LEN(a$) = 2 THEN PRINT ASC(LEFT$(a$, 1)), ASC(MID$(a$, 2.1))
IF LEN(a$) = 1 THEN PRINT ASC(a$)
Äàííàÿ
ïðîãðàììà ïîçâîëÿåò âûâåñòè íà ýêðàí ìîíèòîðà äâà êîäà. Ïåðâàÿ ñòðîêà
ñîäåðæèò äâà îïåðàòîðà, ðàçäåëåííûõ äâîåòî÷èåì.  íà÷àëå íàõîäèòñÿ
ìåòêà «Ì1:» è îïåðàòîð, êîòîðûé ââîäèò èç êëàâèàòóðû çíà÷åíèå è çàòåì çàíîñèò åãî â ïåðåìåííóþ à$.
Åñëè çíà÷åíèå íå áûëî ââåäåíî, òî â ýòîé ïåðåìåííîé áóäåò ïóñòîå
çíà÷åíèå. Âòîðîé îïåðàòîð ïðîâåðÿåò, áûëà ëè íàæàòà êëàâèøà ñ
êëàâèàòóðû, åñëè íå áûëà, òî ïåðåìåííàÿ à$ èìååò ïóñòîå çíà÷åíèå è ïðîèçîéäåò ïåðåõîä íà ìåòêó Ì1. Åñëè â ïåðåìåííîé èìååòñÿ çíà÷åíèå, òî áóäåò âûïîëíÿòüñÿ ñëåäóþùèé îïåðàòîð.
Îòìåòèì,
÷òî â ïåðâîé ñòðîêå èìååòñÿ äâà îïåðàòîðà. Ðàíüøå ìû ðàñïîëàãàëè
îïåðàòîðû íà ðàçíûõ ñòðî÷êàõ, òåïåðü áóäåì èõ ñîåäèíÿòü â îäíó. Ýòî
ïðèâîäèò ê ìåíüøåìó ðàçìåðó ïðîãðàììû, ÷òî îáëåã÷àåò åå ÷òåíèå.
Âòîðàÿ ñòðîêà âûâîäèò íà ýêðàí äâå öèôðû êîäà. Âíà÷àëå ïðîèñõîäèò ïðîâåðêà íà äëèíó ïåðåìåííîé à$. Åñëè áûëî ïîëó÷åíî äâå öèôðû, ÷òî ïðîèñõîäèò ïðè íàæàòèè íà ñëóæåáíûå êëàâèøè, òî îíè âûâîäÿòñÿ íà ýêðàí.
 òðåòüåé ñòðîêå ïðîèñõîäèò ïðîâåðêà íà äëèíó ïåðåìåííîé à$ è, åñëè îíà ñîäåðæèò îäíó öèôðó, òî âûâîäèòñÿ åå çíà÷åíèå íà ýêðàí.
Òàêèì
îáðàçîì, ìîæíî íàæàòü íà ëþáóþ êëàâèøó è óçíàòü åå êîä. Åñëè íóæíî
óçíàòü êîä íåñêîëüêèõ êëàâèø, òî ìîæíî ââåñòè öèêë, â êîòîðîì
ïðîèçâîäÿòñÿ âûøåóêàçàííûå äåéñòâèÿ.
Îïåðàòîðû PUT è GET. Ïðè ðàáîòå ñ ãðàôèêîé ìîãóò áûòü ïîëåçíû îïåðàòîðû PUT è GET, êîòîðûå ïîçâîëÿþò ïåðåìåùàòü èçîáðàæåíèå íà ýêðàíå. Ðàññìîòðèì ñëåäóþùóþ ïðîãðàììó.
SCREEN 9
DIM a%(400)
CLS
LINE (20, 20)-(40, 40), , B
GET (20, 20)-(40, 40), a%
FOR i1% = 1 TO 25
FOR i3 = 1 TO 300
i4 = SIN(i3 / 100)
NEXT i3
PUT (i1% * 10 + 20, i1% * 10 + 20), a%
NEXT i1%
Â
äàííîì ïðèìåðå â ÷åòâåðòîé ñòðî÷êå íà ýêðàí âûâîäèòñÿ ïðÿìîóãîëüíèê,
çàòåì â ñëåäóþùåì îïåðàòîðå çàíîñèòñÿ îáëàñòü, ãäå íàõîäèòñÿ
ïðÿìîóãîëüíèê, â ìàññèâ à%.
Äàëåå ñëåäóåò öèêë äëÿ âûâîäà îáëàñòè íà ýêðàí, ïîñòåïåííî ïî ñòðîêàì è
ñòîëáöàì óâåëè÷èâàÿ êîîðäèíàòû íà 10 åäèíèö (ïèêñåëîâ). Êàê è ðàíüøå,
öèêë ïî ïåðåìåííîé i3%
çàïèñàí äëÿ îðãàíèçàöèè çàäåðæêè âûâîäà, òàê êàê â ïðîòèâíîì ñëó÷àå
âûâîä ïðîèçîéäåò î÷åíü áûñòðî è ìû íå çàìåòèì, êàê ðèñóåòñÿ ôèãóðà. Ñàì
âûâîä îáëàñòè ïðîèçâîäèòñÿ â äåñÿòîì îïåðàòîðå ïðîãðàììû - PUT.
Ðàçìåð ìàññèâà à%
íå îáÿçàòåëüíî ðàâåí êîëè÷åñòâó òî÷åê îáëàñòè. Âñåãî êîëè÷åñòâî òî÷åê
îáëàñòè ðàâíî 20õ20=400, ïîýòîìó è ìàññèâ â ïðîãðàììå èìååò ðàçìåðíîñòü
400. Íî â êîìïüþòåðå â îäíîì áàéòå õðàíèòñÿ ïîëîæåíèå íå îäíîé òî÷êè
ýêðàíà, à ÷óòü áîëåå äâóõ, ïîýòîìó ðàçìåðíîñòü ìàññèâà ìîæíî óñòàíîâèòü
êàê 200, òî åñòü â ïîëîâèíó ìåíüøå. Ðàçìåðíîñòü ìîæíî ïîäñ÷èòàòü è
áîëåå òî÷íî ïî ñëåäóþùåé ôîðìóëå: (16 õ êîëè÷åñòâî_òî÷åê) / 5, ãäå 16
êîëè÷åñòâî áèòîâ â ïåðåìåííûõ ìàññèâà, à 5 - êîëè÷åñòâî áèòîâ,
èñïîëüçóåìûõ äëÿ îäíîãî ïèêñåëà. Åñëè ðàçìåðíîñòü ìàññèâà áóäåò ìåíüøå
íåîáõîäèìîé, òî íà ýêðàíå ïîÿâèòñÿ ñîîáùåíèå «Illegal function call» (íåïðàâèëüíûé âûçîâ ôóíêöèè).
Íà ñàìîì äåëå ïðîèñõîäèò íå ïðîñòî íàëîæåíèå, à âûïîëíÿåòñÿ îïåðàöèÿ XOR
(èñêëþ÷èòåëüíîå ÈËÈ), ïðè êîòîðîì ïðîèñõîäèò âûáîð öâåòà ïî àëãîðèòìó:
åñëè òî÷êà íà ýêðàíå è íàêëàäûâàåìàÿ òî÷êà ðàâíû, òî áóäåò ÷åðíûé öâåò,
åñëè íåò, òî áåëûé. Âîñïîëüçóåìñÿ ýòèì â ñëåäóþùåé ïðîãðàììå.
SCREEN 9
DIM a%(127)
a%(0) = 9
CLS
LINE (20, 20)-(40, 40), , B
GET (20, 20)-(40, 40), a%
FOR i1% = 1 TO 25
FOR i3 = 1 TO 300
i4 = SIN(i3 / 100)
NEXT i3
PUT (i1% * 10 + 10, i1% * 10 + 10), a%
PUT (i1% * 10 + 20, i1% * 10 + 20), a%
NEXT i1%
 ýòîé ïðîãðàììå èìåþòñÿ äâà îïåðàòîðà PUT,
îäèí äëÿ íàëîæåíèÿ îáëàñòè íà îáëàñòü, êîòîðàÿ áûëà ðàíåå âûâåäåíà, òî
åñòü çàêðàøèâàíèå åå ÷åðíûì öâåòîì, âòîðàÿ - âûâîä ïðÿìîóãîëüíèêà íèæå
è ïðàâåå íà äåñÿòü òî÷åê ýêðàíà.
Ñóùåñòâóåò îïåðàòîð (PAINT), êîòîðûé ïîçâîëÿåò çàêðàøèâàòü îáëàñòü íà ýêðàíå îïðåäåëåííûì öâåòîì. Ðàññìîòðèì ïðèìåð.
DATA 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DATA 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0
DATA 0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0
DATA 0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0
DATA 0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0
DATA 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0
DATA 0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0
DATA 0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0
DATA 0,0,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0
DATA 0,1,1,1,1,1,0,1,1,1,1,1,0,0,1,1,1,0,0,1,1,1,1,1,0,1,1,1,1,1
DATA 0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1
DATA 0,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1
DATA 0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,0,1,1,1,1,1
DATA 0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,0,1,1,1,1,1
DATA 0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0
DATA 0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0
DATA 0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0
SCREEN 9
CLS
COLOR 8
FOR i1% = 1 TO 31
LINE (i1% * 10, 10)-(i1% * 10, 200)
NEXT i1%
FOR i1% = 1 TO 20
LINE (10, i1% * 10)-(310, i1% * 10)
NEXT i1%
FOR i1% = 1 TO 19
FOR i2% = 1 TO 30
READ a: PAINT (i2% * 10 + 2, i1% * 10 + 2), a, 8
NEXT i2%
NEXT i1%
Äàííàÿ ïðîãðàììà âûâîäèò íà ýêðàí ðèñóíîê âåëîñèïåäà. Äåëàåòñÿ ýòî ñëåäóþùèì îáðàçîì: âíà÷àëå ïðîãðàììû ïðèñóòñòâóþò îïåðàòîðû DATA,
â êîòîðûõ íàõîäÿòñÿ äàííûå, âñåãî 19 îïåðàòîðîâ ïî 30 ÷èñåë â êàæäîé,
îïðåäåëÿþùèå öâåò êâàäðàòèêîâ. Â äàëüíåéøåì ýòè äàííûå áóäóò
ñ÷èòûâàòüñÿ ïðè ïîìîùè îïåðàòîðà READ.
Äàëåå
ñåðûì öâåòîì ðèñóåòñÿ òàáëèöà ñ êîëè÷åñòâîì ÿ÷ååê 19 õ 30.  ñèëó òîãî,
÷òî ëèíèé, êîòîðûå ðèñóþò òàáëèöó, áîëüøå íà åäèíèöó, âñåãî ïîëó÷àåì 20
ãîðèçîíòàëüíûõ è 31 âåðòèêàëüíûõ ëèíèé.
Îïåðàòîð PAINT
ïîçâîëÿåò çàêðàøèâàòü çàìêíóòûé êîíòóð íà ýêðàíå. Äëÿ òîãî, ÷òîáû
èñïîëüçîâàòü äàííûé îïåðàòîð, íóæíî, ÷òîáû êîíòóð áûë îäíîãî öâåòà è
çàìêíóò.
 êîíöå ïðîãðàììû ïðèñóòñòâóåò äâîéíîé öèêë ïî ñòðîêàì è ñòîëáöàì. Äëÿ êàæäîé ÿ÷åéêè äåéñòâóåò îïåðàòîð PAINT,
ãäå óêàçàíà êîîðäèíàòà òî÷êè, êîòîðàÿ íàõîäèòñÿ â ÿ÷åéêå, äàëåå öâåò,
êîòîðûì äîëæíà áûòü çàïîëíåíà ÿ÷åéêà, ñ÷èòûâàåìûé èç îïåðàòîðîâ DATA (à) è íîìåð öâåòà, êîòîðûì ïðîèñõîäèò çàïîëíåíèå (8).
Ïðåäïîëîæèì, ÷òî íóæíî ìåíÿòü âèäû ðèñóíêà. Äëÿ ýòîãî ïåðåïèñûâàþòñÿ îïåðàòîðû DATA
è èçìåíÿåòñÿ ðàçìåðíîñòü ðåøåòêè, ïðè ïîìîùè êîòîðîé âûâîäèòñÿ ðèñóíîê.
Ýòî äåëàòü êàæäûé ðàç òðóäîåìêî. Ïîýòîìó ïðîùå èñïîëüçîâàòü ìàññèâ, â
êîòîðîì íàõîäèëèñü áû èñõîäíûå äàííûå.
Ïîìåñòèì â ôàéë ïîä èìåíåì Dan.txt ñëåäóþùèå äàííûå:
22, 32
.............ÆÆÆ................
.............Æ.Æ................
...........ÆÆÆÆÆÆÆ..............
..........ÆÆÆ.Æ.ÆÆÆ.............
..........ÆÆÆÆÆÆÆÆÆ.............
...........ÆÆÆÆÆÆÆ..............
..........ÆÆÆÆÆÆÆÆÆ.............
...ÆÆ....ÆÆÆÆÆÆÆÆÆÆÆ............
..ÆÆÆÆ..ÆÆÆÆÆÆÆÆ.ÆÆÆÆ.......ÆÆÆ.
.ÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆ.ÆÆÆÆ....ÆÆÆÆÆ.
.Æ.....ÆÆÆÆÆÆÆÆÆÆÆ.ÆÆÆ...ÆÆÆÆÆ..
.Æ.....ÆÆÆÆÆÆÆÆÆÆÆ.ÆÆÆÆÆÆÆÆÆÆ...
.Æ.....ÆÆÆÆÆÆÆÆÆÆÆ.ÆÆÆÆÆÆÆÆÆ....
.Æ.....ÆÆÆÆÆÆÆÆÆÆÆ.ÆÆÆÆÆÆÆÆ.....
.Æ.....ÆÆÆÆÆÆÆÆÆÆÆ.ÆÆÆÆÆÆÆ......
.ÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆ.ÆÆÆÆÆÆ.......
..ÆÆÆÆ.ÆÆÆÆÆÆÆÆÆÆÆ.ÆÆÆÆÆ........
...ÆÆ..ÆÆÆÆÆÆÆÆÆÆ.ÆÆÆÆÆ.........
.......ÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆ..........
........ÆÆÆÆÆÆÆÆÆÆÆÆÆ...........
.........ÆÆÆÆÆÆÆÆÆÆÆÆ...........
.........ÆÆÆÆÆÆÆÆÆÆÆÆ...........
Â
ñàìîì íà÷àëå ôàéëà íàõîäÿòñÿ äâà ÷èñëà: êîëè÷åñòâî ñòðîê è ñòîëáöîâ
äàííûõ. Òî÷êà áóäåò îáîçíà÷àòü, ÷òî â ÿ÷åéêå òàáëèöû íóæíî îñòàâèòü
öâåò, à ñèìâîë «æ»- çàêðàñêà ñèíèì öâåòîì. Äëÿ òîãî, ÷òîáû èñïîëüçîâàòü
ýòè äàííûå, âîñïîëüçóåìñÿ ñëåäóþùåé ïðîãðàììîé.
SCREEN 9
CLS
COLOR 8
‘ ïðîðèñîâêà ñåòêè
OPEN "dan.txt" FOR INPUT AS #1
INPUT #1, strok%, stolb%
FOR i1% = 1 TO stolb% + 1
LINE (i1% * 10, 10)-(i1% * 10, (strok% + 1) * 10)
NEXT i1%
FOR i1% = 1 TO strok% + 1
LINE (10, i1% * 10)-((stolb% + 1) * 10, i1% * 10)
NEXT i1%
‘ çàêðàøèâàíèå ÿ÷ååê ñåòêè
FOR i1% = 1 TO strok%
INPUT #1, a$
FOR i2% = 1 TO stolb%
IF MID$(a$, i2%, 1) = "." THEN i3% = 0 ELSE i3% = 1
PAINT (i2% * 10 + 2, i1% * 10 + 2), i3%, 8
NEXT i2%
NEXT i1%
CLOSE #1
×åòâåðòûé îïåðàòîð îòêðûâàåò ôàéë ïîä èìåíåì «dan.txt»,
ãäå íàõîäÿòñÿ äàííûå, à â ñëåäóþùåì ïðîèñõîäèò ñ÷èòûâàíèå êîëè÷åñòâà
ñòðîê è ñòîëáöîâ ðèñóíêà. Äàëåå âûâîäèòñÿ ñåòêà ñåðûì öâåòîì (îïåðàòîð «COLOR 8»), ñíà÷àëà âåðòèêàëüíûå ëèíèè, à çàòåì ãîðèçîíòàëüíûå.
Ïîòîì â öèêëå ïî êîëè÷åñòâó ñòðîê ñ÷èòûâàþòñÿ çíà÷åíèÿ â ïåðåìåííóþ à$, èç êîòîðîé â äðóãîì öèêëå ïî ïåðåìåííîé i2% âûáèðàåòñÿ è àíàëèçèðóåòñÿ çíà÷åíèå ñèìâîëà. Åñëè ýòî òî÷êà, òî â ïåðåìåííóþ i3%
çàíîñèòñÿ çíà÷åíèå öâåòà - 0, èíà÷å - 1. Ïîñëå ýòîãî êëåòêà çàïîëíÿåòñÿ
äàííûì öâåòîì. Â êîíöå ðàáîòû ïðîãðàììû ôàéë ñ äàííûìè çàêðûâàåòñÿ.
Çàäà÷è.
1.
Íàðèñóéòå ìàòðåøêó, êîðàáëèê, ñîëíöå, ëóíó, çâåçäó, ôëàã, äîì, ÿõòó,
äîðîæíûé çíàê, êàêóþ-ëèáî ýìáëåìó, äåðåâî, åëêó, ãîðêó, çàáîð, êíèãó,
êàññåòó, ïðèåìíèê è çàêðàñüòå èõ.
2. Ââåäèòå äàííûå â ôàéë «dan.txt» äëÿ äàëüíåéøåãî âûâîäà íà ýêðàí ñëåäóþùèõ ðèñóíêîâ.
|