12-02-2024, 11:58 PM
(12-02-2024, 10:59 AM)kevin Wrote: ...but I think a similar problem was discussed here:
https://www.naalaa.com/forum/thread-138....ght=Return
Thank you for your response, Kevin. I'll learn the code at the provided link above.
Meanwhile, just another simple silly game is in progress ....
Code:
'=====================================================
' ALIENS TOOK MY COWS (updated)
' Aliens are coming...
' They are abducting your cows.
' Now, they challenge you,everytime they tell you ...
' a number of alien's spaceship, find out its
' coordinate and they'll return your cow one by one
' ... Good luck !
'
' REFERENCES
' - Some codes of the Attackers,Galaga Style by Marcus
' - Cow, author:reivaxcorp
' https://opengameart.org/content/cow-run-cycle
'
'=====================================================
'-----------------
' INITIALIZATION
'-----------------
set window "Aliens Took My Cows", 500, 224, false, 2
set redraw off
'color definition
black = [0,0,0]
white = [255,255,255]
red = [255,0,0]
yellow = [255,254,0]
green = [0,200,0]
'constants
constant ECOLS =10, EROWS = 5
'variables
visible enm1Img
divet = 12
divetalien = 1
tmp = []
offsx = 25 ; offsy = 40
countred = 0 'count how many wrong answers
countgreen = 0 'count how many right answers
'whichx = 0 ; whichy = 0
'enemies class
CreateAssets()
randomize time()
enms = dim(ECOLS, EROWS)
for y = 0 to EROWS-1 for x = 0 to ECOLS-1
e = [
img: enm1Img, 'image
a: 0, 'angle
pos: Point(0, 0), 'position
c: fill(Point(0, 0), 4), 'curve
p: unset, 'parameter
mark:"yellow" 'green = right answer, red = wrong answer
]
enms[x][y] = e
next
'info box
info = []
info.x = 0
info.y = 0
info.number = 0
info.guessx = 0
info.guessy = 0
info.posx = 0
info.posy = 0
info.start = true
info.theend = false
'-----------
' MAIN LOOP
'-----------
while not keydown(KEY_ESCAPE, true)
'divet animation #1
if countred >=3 then divetalien = EROWS*ECOLS
divet = divet - divetalien
if divet <= 0
clear tmp
for y = 0 to EROWS - 1 for x = 0 to ECOLS - 1
if enms[x][y] and enms[x][y].p = unset
tmp[sizeof(tmp)] = enms[x][y]
endif
next
if sizeof(tmp)
e = tmp[rnd(sizeof(tmp))]
e.p = 0
e.c[0].x = e.pos.x; e.c[0].y = e.pos.y
e.c[1].x = rnd(242) ; e.c[1].y = 400
divet = 120 + rnd(3)*60
'whichx = (e.pos.x - offsx) / 16
'whichy = (e.pos.y - offsy) / 16
endif
endif
'divet animation #2
for y = 0 to EROWS - 1
for x = 0 to ECOLS - 1
e = enms[x][y]
if e
gx = x*16 + offsx ; gy = y*16 + offsy
if typeof(e.p)
e.p = e.p + 0.005
if e.p >= 1
e.p = unset
e.pos.x = gx; e.pos.y = gy
else
e.c[3].x = gx; e.c[3].y = gy
if info.start = true or countred>=3 then
EvalCBC(e.pos, e.c, e.p)
else
e.pos.x = gx; e.pos.y = gy 'return alien to formation
endif
endif
else
e.pos.x = gx
e.pos.y = gy
endif
endif
next
next
'clear screen
set color black
cls
'reference numbers on columns and rows
set color white
for i = 0 to ECOLS-1
set caret 27+16*i,20 ; write i
next
for i = 0 to EROWS-1
set caret 10,41+16*i ; write i
next
'draw enemies
set color white
for y = 0 to EROWS - 1 for x = 0 to ECOLS - 1
e = enms[x][y]
e.row = y
e.col = x
if e.mark="green" then
set color green
else if e.mark = "red" then
set color red
else
set color yellow
endif
draw image e.img, e.pos.x, e.pos.y
next
'infobox on start
if info.start = true and info.theend = false then
set color white
set caret 220,20
wln "Aliens are coming..."
wln "They are abducting your cows."
wln "Now, they challenge you,"
wln "everytime they tell you ..."
wln "a number of alien's spaceship,"
wln "find out its coordinate and"
wln "they'll return your cow"
wln "one by one... Good luck !"
wln
wln "Press ENTER to continue "
redraw
endif
wln
if info.theend = true then
set color white
set caret 220,20
wln "THE END"
wln "Aliens thank you for the cows"
wln "..."
info.start = true
endif
if countgreen >= 10 then
set color white
set caret 220,20
wln
wln "You get back all your cows"
wln "Thank you for playing"
wln "this silly game :)"
temp=rln()
end
endif
'infobox on game play
if (keydown(KEY_RETURN,true) or info.start=false) and info.theend=false then
info.start = false
'clear info box
set color black; draw rect 220,20,width()-220,height()-20,true
set color white
'guess coordinate
set caret 220,20; write "Enemy number : "; info.number=rnd(1,50);write info.number
set caret 220,40; write "Enemy["+info.number+"].x = "; info.guessx = rln(1,TYPE_NUMBER)
set caret 220,60; write "Enemy["+info.number+"].y = "; info.guessy = rln(1,TYPE_NUMBER)
'calculate the real coordinate
if info.number%10 > 0 then
info.posx = info.number%10-1; info.posy = floor(info.number/10)
else
info.posx = 9; info.posy = floor(info.number/10)-1
endif
'mark green if guess coordinate is true
if info.posx = info.guessx and info.posy = info.guessy then
info.coor = "("+info.posx+","+info.posy+")"
e=enms[info.posx][info.posy]
e.mark = "green"
countgreen = countgreen + 1
else
info.coor = "Try Again"
e=enms[info.posx][info.posy]
e.mark = "red"
countred = countred + 1
endif
if countred >= 3 then
info.theend = true
endif
'display info.coor
set caret 220,80; wln info.coor
wln
wln "Press ESC to continue"
do;redraw; wait 1;until keydown(KEY_ESCAPE,true)
endif
redraw
fwait 60
wend
'------------
' FUNCTIONS
'------------
function Point(x, y)
return [x: x, y: y]
endfunc
function EvalCBC(dst, curve, param)
b0 = (1 - param)^3
b1 = 3*(1 - param)^2*param
b2 = 3*(1 - param)*param^2
b3 = param^3
dst.x = b0*curve[0].x + b1*curve[1].x + b2*curve[2].x + b3*curve[3].x
dst.y = b0*curve[0].y + b1*curve[1].y + b2*curve[2].y + b3*curve[3].y
endfunc
function CreateAssets()
bitdata =[[0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0],
[1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]]
enm1Img = CreateBitmap(bitdata,255, 224, 0)
endfunc
function CreateBitmap(data, r, g, b)
img = createimage(sizeof(data[0]), sizeof(data))
set image img
for y = 0 to sizeof(data) - 1 for x = 0 to sizeof(data[0]) - 1
set color data[y][x]*r, data[y][x]*g, data[y][x]*b
set pixel x, y
next
set image primary
set image colorkey img, 0, 0, 0
return img
endfunc