Nel programma che ho realizzato (pallina ke rimbalza e ke bisogna colpire) ho creato tre form : ognuno corrisponde a una difficoltà diversa (facile, medio, difficile). Il form della difficoltà media mi funziona regolarmente (è il form originario, quello che ho creato x primo e dal quale ho sviluppato gli altri), mentre gli altri due nn funzionano. Inserisco il codice del form facile:
codice:
Public Class formfacile
Dim vel, vdir, hdir, i As Double
Dim risp As Integer
'I commenti del codice principale (quello del Timerfac) son da riferirsi alla palla
Private Sub Timerfac_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timerfac.Tick
ball.Left = ball.Left + (hdir * vel)
ball.Top = ball.Top + (vdir * vel)
'Rimbalza e accelera se colpisci il campo
If ball.Left + ball.Width >= field.Left + field.Width Then
hdir = -hdir
vel = vel + 0.15
End If
If ball.Top + ball.Height >= field.Top + field.Height Then
vdir = -vdir
vel = vel + 0.15
End If
If ball.Top <= field.Top Then
vdir = -vdir
vel = vel + 0.15
End If
'Se passi sopra la racchetta: continua
If ball.Left <= racchetta.Left + racchetta.Width Then
If ball.Top + ball.Height <= racchetta.Top Then
hdir = hdir
vel = vel
End If
End If
'Se centri la racchetta: rimbalza, aggiungi 1pt e rimpicciolisciti
'Se centri la racchetta
If ball.Left <= racchetta.Left + racchetta.Width Then
If ball.Top + ball.Height >= racchetta.Top Then
If ball.Top + ball.Height <= racchetta.Top + racchetta.Height Then
'Rimbalza
hdir = -hdir
'Accelera
If i < 25 Then
vel = vel + 0.5
End If
If i >= 25 Then
vel = vel + 0.3
End If
'Aggiungi 1pt
i = i + 1
End If
End If
End If
'Se passi sotto la racchetta: continua
If ball.Left <= racchetta.Left + racchetta.Width Then
If ball.Top >= racchetta.Top + racchetta.Height Then
hdir = hdir
vel = vel
End If
End If
'Se tocchi il bordo sinistro: fermati
If ball.Left <= field.Left Then
Timerfac.Enabled = False
End If
'Quando ti fermi: appare MsgBox
If Timerfac.Enabled = False Then
risp = MsgBox("Il tuo punteggio è " & i & vbCrLf & "Vuoi rigiocare?", 4, "Fine partita")
End If
'La racchetta non può uscire dal campo
If racchetta.Top <= field.Top Then
racchetta.Top = field.Top
End If
If racchetta.Top + racchetta.Height >= field.Top + field.Height Then
racchetta.Top = field.Top + field.Height - racchetta.Height
End If
'Fine codice istruzioni palla
'Se si isponde "Sì": rigioca [NON FUNZIONA]
If risp = 6 Then
End If
'Se si risponde "No": esci
If risp = 7 Then
End
End If
Label7.Text = vel
Label2.Text = i
End Sub
Private Sub racchetta_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Q Or Keys.W Or Keys.E Or Keys.R Or Keys.T Or Keys.Y Or Keys.U Or Keys.I Or Keys.O Or Keys.P Or Keys.A Or Keys.S Or Keys.D Or Keys.F Or Keys.G Or Keys.H Or Keys.J Or Keys.K Or Keys.L Or Keys.Z Or Keys.X Or Keys.C Or Keys.V Or Keys.B Or Keys.N Or Keys.M Then
Timerfac.Enabled = True
premilettera.Visible = False
End If
'Muovi palla con la tastiera
If e.KeyCode = Keys.A Then
racchetta.Top = racchetta.Top - 30
End If
If e.KeyCode = Keys.Z Then
racchetta.Top = racchetta.Top + 30
End If
End Sub
Private Sub formfacile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
vel = 9
vdir = 1
hdir = 1
i = 0
Timerfac.Enabled = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Se si preme "Esci", il gioco finisce
End
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Se si preme "Ricomincia", la palla viene riposizionata e si riparte
Timerfac.Enabled = True
ball.Location = punto.Location
hdir = -hdir
vdir = -vdir
vel = 9
i = 0
End Sub
End Class
E' uguale a quello del form medio (ho fatto copia e incolla), ma non funziona. Ho impostato che quando carico il form, il timer è disattivato e c'è la scritta "Quando sei pronto premi una lettera" (label "premilettera"); quando si preme un tasto il timer si attiva e la scritta sparisce: questio ultimi due passaggi non li fa. Credo ci sia un problema con l'evento KeyDown del form. Mi aiutate? GRAZIE IN ANTICIPO