Oppure

Loading
30/05/10 19:41
Berto
Questo è il primo algoritmo che metto su questo sito quindi vi prego di perdonare eventuali errori e di non arrabbiarvi troppo, sappiate che accetterò le vostre critiche purchè siano costruttive...
Io ho creato un programma in vb6 molto simile a Frogger(un vecchio gioco per pc) che funziona in questo modo:
Option Explicit
Dim a As Byte
Dim v As Variant
Private Sub haiperso()
MsgBox "HAHA"
MsgBox "Hai perso..."
Unload Form1

End Sub


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight Then
    shape1.X1 = shape1.X1 + 50
    shape1.X2 = shape1.X2 + 50
    If shape1.X1 >= 7920 Then
        shape1.X1 = shape1.X1 - 100
        shape1.X2 = shape1.X2 - 100
    End If
End If
If KeyCode = vbKeyDown Then
    shape1.Y1 = shape1.Y1 + 50
    shape1.Y2 = shape1.Y2 + 50
    If shape1.Y1 >= 3360 Then
        shape1.Y1 = shape1.Y1 - 100
        shape1.Y2 = shape1.Y2 - 100
    End If
End If
If KeyCode = vbKeyLeft Then
    shape1.X1 = shape1.X1 - 50
    shape1.X2 = shape1.X2 - 50
    If shape1.X1 <= 600 Then
        MsgBox "Hai vinto!"
        Unload Form1
    End If
End If
If KeyCode = vbKeyUp Then
    shape1.Y1 = shape1.Y1 - 50
    shape1.Y2 = shape1.Y2 - 50
    If shape1.Y1 <= 300 Then
        shape1.Y1 = shape1.Y1 + 100
        shape1.Y2 = shape1.Y2 + 100
    End If
End If
End Sub

Private Sub Form_Load()
MsgBox "Buon divertimento!"
End Sub

Private Sub Timer3_Timer()
Dim s As Byte
Randomize
s = Val(Int(Rnd * 4))
Select Case s
Case Is = 0
    Line10.Y1 = Line10.Y1 + 170
    Line10.Y2 = Line10.Y2 + 170
Case Is = 1
    Line10.Y1 = Line10.Y1 - 170
    Line10.Y2 = Line10.Y2 - 170
Case Is = 2
    Line10.X1 = Line10.X1 + 170
    Line10.X2 = Line10.X2 + 170
Case Is = 3
    Line10.X1 = Line10.X1 - 170
    Line10.X2 = Line10.X2 - 170
End Select

If Line10.Y1 <= 500 Then
    Line10.Y1 = Line10.Y1 + 200
    Line10.Y2 = Line10.Y2 + 200
End If
If Line10.Y1 >= 3360 Then
    Line10.Y1 = Line10.Y1 - 200
    Line10.Y2 = Line10.Y2 - 200
End If
If Line10.X1 <= 2040 Then
    Line10.X1 = Line10.X1 + 200
    Line10.X2 = Line10.X2 + 200
End If
If Line10.X1 >= 3480 Then
    Line10.X1 = Line10.X1 - 200
    Line10.X2 = Line10.X2 - 200
End If


s = Val(Int(Rnd * 4))
Select Case s
Case Is = 0
    Line9.Y1 = Line9.Y1 + 150
    Line9.Y2 = Line9.Y2 + 150
Case Is = 1
    Line9.Y1 = Line9.Y1 - 150
    Line9.Y2 = Line9.Y2 - 150
Case Is = 2
    Line9.X1 = Line9.X1 + 150
    Line9.X2 = Line9.X2 + 150
Case Is = 3
    Line9.X1 = Line9.X1 - 150
    Line9.X2 = Line9.X2 - 150
End Select

If Line9.Y1 <= 500 Then
    Line9.Y1 = Line9.Y1 + 300
    Line9.Y2 = Line9.Y2 + 300
End If
If Line9.Y1 >= 3360 Then
    Line9.Y1 = Line9.Y1 - 300
    Line9.Y2 = Line9.Y2 - 300
End If
If Line9.X1 <= 2040 Then
    Line9.X1 = Line9.X1 + 300
    Line9.X2 = Line9.X2 + 300
End If
If Line9.X1 >= 3480 Then
    Line9.X1 = Line9.X1 - 300
    Line9.X2 = Line9.X2 - 300
End If

End Sub



Private Sub Timer1_Timer()
If a = 1 Then
Line6.Y1 = Line6.Y1 + 10
Line6.Y2 = Line6.Y2 + 10

Line7.Y1 = Line7.Y1 + 10
Line7.Y2 = Line7.Y2 + 10

Line8.Y1 = Line8.Y1 + 10
Line8.Y2 = Line8.Y2 + 10

Line16.Y1 = Line16.Y1 + 10
Line16.Y2 = Line16.Y2 + 10

Line17.Y1 = Line17.Y1 + 10
Line17.Y2 = Line17.Y2 + 10
If Line6.Y2 >= 2880 Then
a = 2

End If
Else
If a = 2 Then
Line6.Y1 = Line6.Y1 - 10
Line6.Y2 = Line6.Y2 - 10

Line7.Y2 = Line7.Y2 - 10
Line7.Y1 = Line7.Y1 - 10

Line8.Y1 = Line8.Y1 - 10
Line8.Y2 = Line8.Y2 - 10

Line16.Y1 = Line16.Y1 - 10
Line16.Y2 = Line16.Y2 - 10

Line17.Y1 = Line17.Y1 - 10
Line17.Y2 = Line17.Y2 - 10
If Line6.Y2 <= 960 Then
a = 1
End If
Else
Line6.Y1 = Line6.Y1 + 10
Line6.Y2 = Line6.Y2 + 10

Line7.Y1 = Line7.Y1 + 10
Line7.Y2 = Line7.Y2 + 10

Line8.Y1 = Line8.Y1 + 10
Line8.Y2 = Line8.Y2 + 10

Line16.Y1 = Line16.Y1 + 10
Line16.Y2 = Line16.Y2 + 10

Line17.Y1 = Line17.Y1 + 10
Line17.Y2 = Line17.Y2 + 10
a = 2

End If
End If

End Sub

Private Sub Timer2_Timer()
If Line6.X1 > shape1.X2 And Line6.X1 < shape1.X1 Then
    If shape1.Y1 > Line6.Y2 And shape1.Y1 < Line6.Y1 Then
haiperso
    End If
End If
If Line8.X1 > shape1.X2 And Line8.X1 < shape1.X1 Then
    If shape1.Y1 > Line8.Y2 And shape1.Y1 < Line8.Y1 Then
haiperso
    End If
End If
If Line7.X1 > shape1.X2 And Line7.X1 < shape1.X1 Then
    If shape1.Y1 > Line7.Y2 And shape1.Y1 < Line7.Y1 Then
haiperso
    End If
End If

If Line9.X1 > shape1.X2 And Line9.X1 < shape1.X1 Then
    If shape1.Y1 > Line9.Y2 And shape1.Y1 < Line9.Y1 Then
haiperso
    End If
End If
If Line10.X1 > shape1.X2 And Line10.X1 < shape1.X1 Then
    If shape1.Y1 > Line10.Y2 And shape1.Y1 < Line10.Y1 Then
haiperso
    End If
End If
If Line11.X1 > shape1.X2 And Line11.X1 < shape1.X1 Then
    If shape1.Y1 > Line11.Y2 And shape1.Y1 < Line11.Y1 Then
haiperso
    End If
End If
If Line12.X1 > shape1.X2 And Line12.X1 < shape1.X1 Then
    If shape1.Y1 > Line12.Y2 And shape1.Y1 < Line12.Y1 Then
haiperso
    End If
End If
If Line13.X1 > shape1.X2 And Line13.X1 < shape1.X1 Then
    If shape1.Y1 > Line13.Y2 And shape1.Y1 < Line13.Y1 Then
haiperso
    End If
End If
If Line14.X1 > shape1.X2 And Line14.X1 < shape1.X1 Then
    If shape1.Y1 > Line14.Y2 And shape1.Y1 < Line14.Y1 Then
haiperso
    End If
End If
If Line15.X1 > shape1.X2 And Line15.X1 < shape1.X1 Then
    If shape1.Y1 > Line15.Y2 And shape1.Y1 < Line15.Y1 Then
haiperso
    End If
End If
If Line16.X1 > shape1.X2 And Line16.X1 < shape1.X1 Then
    If shape1.Y1 > Line16.Y2 And shape1.Y1 < Line16.Y1 Then
haiperso
    End If
End If
If Line17.X1 > shape1.X2 And Line17.X1 < shape1.X1 Then
    If shape1.Y1 > Line17.Y2 And shape1.Y1 < Line17.Y1 Then
haiperso
    End If
End If
End Sub

Private Sub Timer4_Timer()
Dim s As Byte
Randomize
s = Val(Int(Rnd * 4))
Select Case s
Case Is = 0
    Line11.Y1 = Line11.Y1 + 50
    Line11.Y2 = Line11.Y2 + 50
Case Is = 1
    Line11.Y1 = Line11.Y1 - 50
    Line11.Y2 = Line11.Y2 - 50
Case Is = 2
    Line11.X1 = Line11.X1 + 50
    Line11.X2 = Line11.X2 + 50
Case Is = 3
    Line11.X1 = Line11.X1 - 50
    Line11.X2 = Line11.X2 - 50
End Select

If Line11.Y1 <= 240 Then
    Line11.Y1 = Line11.Y1 + 100
    Line11.Y2 = Line11.Y2 + 100
End If
If Line11.Y2 >= 3200 Then
    Line11.Y1 = Line11.Y1 - 100
    Line11.Y2 = Line11.Y2 - 100
End If
If Line11.X1 <= 600 Then
    Line11.X1 = Line11.X1 + 100
    Line11.X2 = Line11.X2 + 100
End If
If Line11.X2 >= 1920 Then
    Line11.X1 = Line11.X1 - 100
    Line11.X2 = Line11.X2 - 100
End If


s = Val(Int(Rnd * 4))
Select Case s
Case Is = 0
    Line12.Y1 = Line12.Y1 + 50
    Line12.Y2 = Line12.Y2 + 50
Case Is = 1
    Line12.Y1 = Line12.Y1 - 50
    Line12.Y2 = Line12.Y2 - 50
Case Is = 2
    Line12.X1 = Line12.X1 + 50
    Line12.X2 = Line12.X2 + 50
Case Is = 3
    Line12.X1 = Line12.X1 - 50
    Line12.X2 = Line12.X2 - 50
End Select

If Line12.Y1 <= 240 Then
    Line12.Y1 = Line12.Y1 + 100
    Line12.Y2 = Line12.Y2 + 100
End If
If Line12.Y2 >= 3200 Then
    Line12.Y1 = Line12.Y1 - 100
    Line12.Y2 = Line12.Y2 - 100
End If
If Line12.X1 <= 600 Then
    Line12.X1 = Line12.X1 + 100
    Line12.X2 = Line12.X2 + 100
End If
If Line12.X2 >= 1920 Then
    Line12.X1 = Line12.X1 - 100
    Line12.X2 = Line12.X2 - 100
End If
s = Val(Int(Rnd * 4))
Select Case s
Case Is = 0
    Line13.Y1 = Line13.Y1 + 50
    Line13.Y2 = Line13.Y2 + 50
Case Is = 1
    Line13.Y1 = Line13.Y1 - 50
    Line13.Y2 = Line13.Y2 - 50
Case Is = 2
    Line13.X1 = Line13.X1 + 50
    Line13.X2 = Line13.X2 + 50
Case Is = 3
    Line13.X1 = Line13.X1 - 50
    Line13.X2 = Line13.X2 - 50
End Select

If Line13.Y1 <= 240 Then
    Line13.Y1 = Line13.Y1 + 100
    Line13.Y2 = Line13.Y2 + 100
End If
If Line13.Y2 >= 3200 Then
    Line13.Y1 = Line13.Y1 - 100
    Line13.Y2 = Line13.Y2 - 100
End If
If Line13.X1 <= 600 Then
    Line13.X1 = Line13.X1 + 100
    Line13.X2 = Line13.X2 + 100
End If
If Line13.X2 >= 1920 Then
    Line13.X1 = Line13.X1 - 100
    Line13.X2 = Line13.X2 - 100
End If
s = Val(Int(Rnd * 4))
Select Case s
Case Is = 0
    Line14.Y1 = Line14.Y1 + 50
    Line14.Y2 = Line14.Y2 + 50
Case Is = 1
    Line14.Y1 = Line14.Y1 - 50
    Line14.Y2 = Line14.Y2 - 50
Case Is = 2
    Line14.X1 = Line14.X1 + 50
    Line14.X2 = Line14.X2 + 50
Case Is = 3
    Line14.X1 = Line14.X1 - 50
    Line14.X2 = Line14.X2 - 50
End Select

If Line14.Y1 <= 240 Then
    Line14.Y1 = Line14.Y1 + 100
    Line14.Y2 = Line14.Y2 + 100
End If
If Line14.Y2 >= 3200 Then
    Line14.Y1 = Line14.Y1 - 100
    Line14.Y2 = Line14.Y2 - 100
End If
If Line14.X1 <= 600 Then
    Line14.X1 = Line14.X1 + 100
    Line14.X2 = Line14.X2 + 100
End If
If Line14.X2 >= 1920 Then
    Line14.X1 = Line14.X1 - 100
    Line14.X2 = Line14.X2 - 100
End If
s = Val(Int(Rnd * 4))
Select Case s
Case Is = 0
    Line15.Y1 = Line15.Y1 + 50
    Line15.Y2 = Line15.Y2 + 50
Case Is = 1
    Line15.Y1 = Line15.Y1 - 50
    Line15.Y2 = Line15.Y2 - 50
Case Is = 2
    Line15.X1 = Line15.X1 + 50
    Line15.X2 = Line15.X2 + 50
Case Is = 3
    Line15.X1 = Line15.X1 - 50
    Line15.X2 = Line15.X2 - 50
End Select

If Line15.Y1 <= 240 Then
    Line15.Y1 = Line15.Y1 + 100
    Line15.Y2 = Line15.Y2 + 100
End If
If Line15.Y2 >= 3200 Then
    Line15.Y1 = Line15.Y1 - 100
    Line15.Y2 = Line15.Y2 - 100
End If
If Line15.X1 <= 600 Then
    Line15.X1 = Line15.X1 + 100
    Line15.X2 = Line15.X2 + 100
End If
If Line15.X2 >= 1920 Then
    Line15.X1 = Line15.X1 - 100
    Line15.X2 = Line15.X2 - 100
End If
End Sub

Il programma funziona ma non è molto elegante...
Ho provato a migliorarlo con l'utilizzo di variabili e sottoprogrammi e questo è il risultato:
Option Explicit
Dim sn As String
Dim lima As Integer
Dim limb As Integer
Dim lims As Integer
Dim limd As Integer
Dim a As Byte
Dim v As Variant
Dim tn As Variant
Private Sub haiperso()
MsgBox "HAHA"
MsgBox "Hai perso..."
Unload Form1

End Sub

Private Sub Command1_Click()
shape1.X1 = shape1.X1 + 50
shape1.X2 = shape1.X2 + 50
End Sub

Private Sub Command2_Click()
shape1.Y1 = shape1.Y1 + 50
shape1.Y2 = shape1.Y2 + 50
End Sub

Private Sub Command3_Click()
shape1.X1 = shape1.X1 - 50
shape1.X2 = shape1.X2 - 50
End Sub

Private Sub Command4_Click()
shape1.Y1 = shape1.Y1 - 50
shape1.Y2 = shape1.Y2 - 50
End Sub

Private Sub Timer1_Timer()
If a = 1 Then
Line6.Y1 = Line6.Y1 + 10
Line6.Y2 = Line6.Y2 + 10

Line7.Y1 = Line7.Y1 + 10
Line7.Y2 = Line7.Y2 + 10

Line8.Y1 = Line8.Y1 + 10
Line8.Y2 = Line8.Y2 + 10
If Line6.Y2 >= 2880 Then
a = 2

End If
Else
If a = 2 Then
Line6.Y1 = Line6.Y1 - 10
Line6.Y2 = Line6.Y2 - 10

Line7.Y2 = Line7.Y2 - 10
Line7.Y1 = Line7.Y1 - 10

Line8.Y1 = Line8.Y1 - 10
Line8.Y2 = Line8.Y2 - 10

If Line6.Y2 <= 960 Then
a = 1
End If
Else
Line6.Y1 = Line6.Y1 + 10
Line6.Y2 = Line6.Y2 + 10

Line7.Y1 = Line7.Y1 + 10
Line7.Y2 = Line7.Y2 + 10

Line8.Y1 = Line8.Y1 + 10
Line8.Y2 = Line8.Y2 + 10

a = 2

End If
End If

End Sub
Private Sub controllo()
If tn.X1 > shape1.X2 And tn.X1 < shape1.X1 Then
    If shape1.Y1 > tn & ".Y2" And shape1.Y1 < tn & ".Y1" Then
        haiperso
    End If
End If
End Sub
Private Sub Timer2_Timer()
Dim f As Byte
For f = 6 To 15
    tn = "Line" & f
    controllo
Next

End Sub

Private Sub Timer3_Timer()
lima = 240
limb = 3200
lims = 2100
limd = 3400
sn = Line9
shapemov
sn = Line10
shapemov
End Sub


Private Sub Timer4_Timer()
Dim f As Byte
lima = 240
limb = 3200
lims = 240
limd = 1920
For f = 11 To 15
    sn = "Line" & f
    shapemov
Next
End Sub
Private Sub shapemov()
Dim s As Byte
Randomize
s = Val(Int(Rnd * 4))
Select Case s
Case Is = 0
    sn.Y1 = sn.Y1 + 50
    sn.Y2 = sn.Y2 + 50
Case Is = 1
    sn.Y1 = sn.Y1 - 50
    sn.Y2 = sn.Y2 - 50
Case Is = 2
    sn.X1 = sn.X1 + 50
    sn.X2 = sn.X2 + 50
Case Is = 3
    sn.X1 = sn.X1 - 50
    sn.X2 = sn.X2 - 50
End Select

If sn.Y1 <= lima Then
    sn.Y1 = sn.Y1 + 100
    sn.Y2 = sn.Y2 + 100
End If
If sn.Y1 >= limb Then
    sn.Y1 = sn.Y1 - 100
    sn.Y2 = sn.Y2 - 100
End If
If sn.X1 <= lims Then
    sn.X1 = sn.X1 + 100
    sn.X2 = sn.X2 + 100
End If
If sn.X1 >= limd Then
    sn.X1 = sn.X1 - 100
    sn.X2 = sn.X2 - 100
End If

End Sub


Le differenze tra i due codici sono il movimento di shape1(che in realtà è una line) tramite command button e l'assenza del controllo sulla posizione di shape1...
Il problema è che le variabili così dichiarate non funzionano! :pat:
In più quando uso l'eseguibile e faccio riferimento a unload form1 non viene bloccata l'esecuzione del programma ma viene solo chiuso il form...
Qualcuno mi può aiutare?
Vi ringrazio in anticipo e vi chiedo ancora scusa per eventuali errori... :-|
aaa
30/05/10 20:51
GrG
non capisco...

L'istruzione Unload serve per chiudere il form indicato, quindi funziona correttamente... tu che vorresti fare?
aaa
30/05/10 22:53
Alfonso
Prima di tutto è impossibile fare una verifica sul campo del tuo listato. Con così tante linee, shape, command, timer è troppo faticoso approntare il progetto come l'hai fatto tu.
La prossima volta allega uno zip della form completa.

Comunque che non si chiuda con unload è l'ultimo dei tuoi problemi.
Guardando il listato si nota:

Dim tn As Variant
a tn viene assegnato un valore in Timer2_Timer
tn="Line" & F
il risultato è una stringa non certo un controllo line come penso tu supponga. Infatti chiamando la sub controllo si ha un errore "Necessario oggetto" quando scrivi tn.x1
Come l'hai impostato tu, tn potrebbe indicare il nome del controllo (che è una stringa) ma allora nella sub controllo dovresti scrivere una routine di scansione dei controlli lines in cerca di quello con il nome tn e procedere su quello per i confronti.

Più semplicemente ti consiglio di creare un array di lines1 da 6 a 17 e poi nel ciclo for passare f (l'indice dell'array di linee) alla sub controllo
Private Sub Timer2_Timer()
  Dim f As Byte
  For f = 6 To 15
    controllo f
  Next
End Sub

Private Sub controllo(f as Byte)
  If line1(f).X1 > shape1.X2 And line1(f).X1 < shape1.X1 Then
    If shape1.Y1 > line1(f).Y2 And shape1.Y1 < line1(f).Y1 Then
        haiperso
    End If
  End If
End Sub


Così devi fare per sn.
aaa
31/05/10 12:45
Berto
Vi ringrazio per la pazienza, se sapessi come allegare direttamente il form e il progetto sul vostro sito lo avrei fatto...
L'idea di Alfonso è ottima, usare un vettore risolverebbe il problema ma io gli array e le matrici le so fare solo di numeri o stringhe... :_doubt:
Mi chiedevo inoltre se ci fosse un modo di dichiarare le variabili tale da permettere al mio programma di funzionare.
Per quanto riguarda l'unload mi scuso GrG per la poca chiarezza nella domanda posta, provo a riformularla:
Io vorrei che tutto il processo riguardante il programma fosse chiuso non solo il form1, infatti, dopo aver usato l'unload, il processo è ancora visibile da task manager.
In pratica: COME CHIUDO IL PROGRAMMA? :hail:
aaa
31/05/10 14:31
Alfonso
Creare un array di linee è molto semplice:
Dalla Casella degli strumenti selezioni il controllo linea e tracci la tua linea sulla form. Se è la prima linea che tracci sarà la line1. Ti posizioni con il mouse su uno degli estremi della linea selezionata, premi il tasto destro del mouse, scegli nel menu contestuale Copia. Ti posizioni con il mouse su un punto vuoto del form e cliccando con il tasto destro, dal menu che appare scegli Incolla.
Ti sarà chiesto se vuoi creare una matrice di controlli. Se rispondi Sì, ti ritroverai con una linea Line1(0) e una Line1(1).
Continuando ad incollare arrivi fino a Line1(16).
Cancelli le linee con indice da 0 a 5 ed hai pronto un array con indice da 6 a 16.

Se preferisci continuare così come sei adesso:

Dim tn As String

Questo rimane come è adesso
Private Sub Timer2_Timer()
  Dim f As Byte
  For f = 6 To 15
     tn = "Line" & f
     controllo
  Next
End Sub

Scansioni l'array dei controlli presenti nella form e quando trovi quello con il nome che cerchi lo utilizzi per i confronti
Private Sub controllo()
  Dim t As Integer
  For t = 0 To Controls.Count - 1
    If Controls(t).Name = tn Then
        If Controls(t).X1 > shape1.X2 And Controls(t).X1 < shape1.X1 Then
            If shape1.Y1 > Controls(t).Y2 And shape1.Y1 < Controls(t).Y1 Then
                haiperso
            End If
        End If
    End If
  Next
End Sub


Stessa cosa per sn e la Sub shapemov()
aaa
31/05/10 14:54
Berto
Grazie e mille!!! Io volevo fare funzionare un programma e ora so anche come creare una matrice di comandi! :rotfl::rotfl::rotfl:
Però resta il fatto che non riesco a chiudere il programma quando uno perde e che non ho la minima idea di come mettere il file .rar del programma completato sul sito...
Potete aiutarmi anche per questi problemi?
Ultima modifica effettuata da Berto 31/05/10 16:32
aaa
31/05/10 16:46
Alfonso
Il programma deve essere in formato .zip e lo puoi allegare quando stai rispondendo ad un messaggio. Alla sinistra del pulsante Send c'è il pulsante Sfoglia che ti serve per selezionare il file da allegare.

Non so cosa fa il tuo programma. Se è composto solo dal form1 e con unload form1 non si chiude completamente, vuol dire che potrebbe esserci in corso una qualche procedura o un loop e comunque, prima di chiudere, disabilita i timer.

Timer1.enabled = false

E in form_unload metti Set Form1 = Nothing
aaa
31/05/10 16:50
Berto
Grazie, quando avrò apportato le modifiche pubblicherò il programma completo e potrai provarlo di persona... (Io sono per l'open source) :heehee:
aaa