Botin nopeus
Botin nopeus
Harjoittelen tässä botin tekemistä ja sain jo aikaiseksi jotain, mutta ongelmaksi tuli se, että ensinäkin ohjelmassani on ruudukko jossa on kaksi neliötä toinen on botti ja toinen on nuolinäppäimillä ohjattava ja botin tehtävä on saada tämä nuolinnäppäimillä ohjattava laatikko kiinni. Muuten tämä toimii, mutta koska haluaisin, että botti etenee ruudukossa hitaammin kuin nuolinäppäimillä ohjattava, niin rajoitin botin liikettä wait-komennolla, mutta tämä sitten vaikutti myös tähän nuolinäppäimillä ohjattavaan eli miten voisin rajottaa vain botin nopeutta? Molemmat systeemit on saman silmukan sisällä...
Re: Botin nopeus
Laitappas koodia niin on helpompi auttaa.
Re: Botin nopeus
Eli koodi on seuraavan näkönen:
Code: Select all
Dim x,y,x1,y1,randX,randY,x_distance,y_distance As Integer
x = 126
y = 89
randX = Rand(1,6)
randY = Rand(1,5)
Select randX
Case 1
x1 = 126
Case 2
x1 = 151
Case 3
x1 = 176
Case 4
x1 = 201
Case 5
x1 = 226
Case 6
x1 = 251
EndSelect
Select randY
Case 1
y1 = 89
Case 2
y1 = 114
Case 3
y1 = 139
Case 4
y1 = 164
Case 5
y1 = 189
EndSelect
Repeat
x_distance = (x1-x)/25
y_distance = (y1-y)/25
Color cbWhite
Text 0,0,"X-etäisyys: " + x_distance + " ruutua"
Text 0,10,"Y-etäisyys: " + y_distance + " ruutua"
Color cbWhite
Box 125,88,151,126,0
Line 150,88,150,212
Line 175,88,175,212
Line 200,88,200,212
Line 225,88,225,212
Line 250,88,250,212
Line 125,113,274,113
Line 125,138,274,138
Line 125,163,274,163
Line 125,188,274,188
If x_distance > 0 Then
x = x + 25
Wait 200
ElseIf x_distance < 0 Then
x = x - 25
Wait 200
ElseIf y_distance > 0 Then
y = y + 25
Wait 200
ElseIf y_distance < 0 Then
y = y - 25
Wait 200
EndIf
If KeyUp(203) Then
x1 = x1 - 25
Wait 1
ElseIf KeyUp(205) Then
x1 = x1 + 25
Wait 1
ElseIf KeyUp(200) Then
y1 = y1 - 25
Wait 1
ElseIf KeyUp(208) Then
y1 = y1 + 25
Wait 1
EndIf
Color cbGreen
Box x1,y1,24,24
If x < 125 Or x1 < 125 Then
x = x + 25
x1 = x1 + 25
ElseIf x > 251 Or x1 > 251 Then
x = x - 25
x1 = x1 - 25
ElseIf y < 88 Or y1 < 88 Then
y = y + 25
y1 = y1 + 25
ElseIf y > 189 Or y1 > 189 Then
y = y - 25
y1 = y1 - 25
EndIf
Color cbRed
Box x,y,24,24
DrawScreen
Until EscapeKey()
WaitKey
End
-
- Forum Veteran
- Posts: 2396
- Joined: Tue Aug 28, 2007 4:20 pm
Re: Botin nopeus
Tuon rand-jutun vois muuten toteuttaa purkattomammin tyyliin:Kun siinä kerran näyttäisi olevan sääntö miten seuraava luku saadaan.
Code: Select all
x1=126+rand(1,6)*25
y1=89+rand(1,5)*25
Re: Botin nopeus
Itse asiassa näin:Grandi wrote:Tuon rand-jutun vois muuten toteuttaa purkattomammin tyyliin:Kun siinä kerran näyttäisi olevan sääntö miten seuraava luku saadaan.Code: Select all
x1=126+rand(1,6)*25 y1=89+rand(1,5)*25
Code: Select all
x1=101+rand(1,6)*25
y1=64+rand(1,5)*25
Re: Botin nopeus
Kiitos kaikesta avusta! Muutin koodia sen verran, että ohjelma arpoo kahden sekunnin välein vihreälle laatikolle uuden sijainnin ja botti ottaa sitä koko aika kiinni...
Code: Select all
Dim x,y,x1,y1,randX,randY,x_distance,y_distance,_time As Integer
x = 126
y = 89
x1=101+rand(1,6)*25
y1=64+Rand(1,5)*25
Repeat
x_distance = (x1-x)/25
y_distance = (y1-y)/25
If Timer()>_time+2000 Then
x1=101+rand(1,6)*25
y1=64+Rand(1,5)*25
_time = Timer()
EndIf
Color cbWhite
Text 0,0,"X-etäisyys: " + x_distance + " ruutua"
Text 0,11,"Y-etäisyys: " + y_distance + " ruutua"
If x_distance * y_distance = 0 Then
Text 0,22,"Botti sai laatikon kiinni!"
Else
Text 0,22,"Botti ei ole saanut laatikkoa kiinni!"
EndIf
Color cbWhite
Box 125,88,151,126,0
Line 150,88,150,212
Line 175,88,175,212
Line 200,88,200,212
Line 225,88,225,212
Line 250,88,250,212
Line 125,113,274,113
Line 125,138,274,138
Line 125,163,274,163
Line 125,188,274,188
If x_distance > 0 Then
x = x + 25
Wait 200
ElseIf x_distance < 0 Then
x = x - 25
Wait 200
ElseIf y_distance > 0 Then
y = y + 25
Wait 200
ElseIf y_distance < 0 Then
y = y - 25
Wait 200
EndIf
Color cbGreen
Box x1,y1,24,24
If x < 125 Or x1 < 125 Then
x = x + 25
x1 = x1 + 25
ElseIf x > 251 Or x1 > 251 Then
x = x - 25
x1 = x1 - 25
ElseIf y < 88 Or y1 < 88 Then
y = y + 25
y1 = y1 + 25
ElseIf y > 189 Or y1 > 189 Then
y = y - 25
y1 = y1 - 25
EndIf
Color cbRed
Box x,y,24,24
DrawScreen
Until EscapeKey()
WaitKey
End
Re: Botin nopeus
Tässä vielä sama toteutettuna typeillä, jos siitä on apua
Code: Select all
SCREEN 800,600
Const Size=30 ' pikseliä neliön koko
Const AlkuX=100 ' ruudukon alku paikka
Const AlkuY=100
Const MaaraX=15 ' määrä x suunnassa
Const MaaraY=15
Type PALIKKA
Field x
Field y
EndType
Laatikot=MakeImage(MaaraX*Size,MaaraY*Size)
DrawToImage Laatikot
MakePalikka()
DrawToScreen
Viive=00 ' ms jos haluat viiveen lisää tätä aikaa..
aa.PALIKKA=First(PALIKKA)
bb.PALIKKA=Last(PALIKKA)
Repeat
viive=(UpKey()-DownKey())*5+viive
If viive>300
viive=300
ElseIf viive<0
viive=0
EndIf
If Timer()>=aika+viive Then
DrawToImage laatikot
ImgClear(aa\x,aa\y) ' kokeile ilman xD
If Dist(aa\x,bb\x) < 0 Then
aa\x = aa\x + 1
ElseIf Dist(aa\x,bb\x) > 0 Then
aa\x = aa\x - 1
ElseIf Dist(aa\y,bb\y) < 0 Then
aa\y = aa\y + 1
ElseIf Dist(aa\y,bb\y) > 0 Then
aa\y = aa\y - 1
ElseIf Dist(aa\y,bb\y)=0
bb\x=Rand(0,MaaraX-1)
bb\y=Rand(0,MaaraY-1)
Color cbgreen : Box bb\x*Size,bb\y*Size,Size,Size,1
EndIf
Color cbred : Box aa\x*Size,aa\y*Size,Size,Size,1
DrawToScreen
aika=Timer()
EndIf
DrawImage Laatikot,alkuX,AlkuY
DrawGame
Color cbwhite
Text 10,10,"FPS: "+FPS()
Text 10,38,"Viive: "+viive+"ms"
Text 200,10,"Nuoli ylös lisää viivettä"
Text 200,30,"Nuoli alas vähentää viivettä"
DrawScreen
Forever
Function ImgClear(xx,yy)
Color cbblack : Box xx*Size,yy*Size,Size,Size,1
Color cbwhite : Box xx*Size,yy*Size,Size,Size,0
EndFunction
Function Dist(x1#,x2#)
If x1#>x2#
Return 1
ElseIf x1#<x2#
Return -1
EndIf
EndFunction
Function MakePalikka()
For xx=0 To MaaraX-1
For yy=0 To MaaraY-1
aa.PALIKKA=New(PALIKKA)
aa\x=xx
aa\y=yy
If xx=0 And yy=0
Color cbred : Box xx*Size,yy*Size,Size,Size,1
ElseIf xx=MaaraX-1 And yy=MaaraY-1
Color cbgreen: Box xx*Size,yY*Size,Size,Size,1
Else
Color cbwhite: Box xx*Size,yY*Size,Size,Size,0
EndIf
Next yy
Next xx
EndFunction