Visual Basic 中学校 掲示板 投稿の管理
タグのない投稿を抽出
統計
RSS
Visual Basic 中学校
投稿一覧
ブロック崩しについての質問
この投稿へのリンク
https://keijiban.umayadia.com/ThreadDetail.aspx?ThreadId=9762#CommentId15650
この投稿の削除
削除パスワード
削除する
コメント本文
投稿者
YAS
 (社会人)
投稿日時
2010/8/25 08:00:18
今まで話の流れだと下のような感じでしょうか。(コピー&ペーストで動きます。)
もしかすると「Me.KeyPreview = True」が必要なのかもしれません。
Public Class Form1
Dim Paddle As New PictureBox
Dim Ball As New PictureBox
Dim Balldx As Integer = 5
Dim Balldy As Integer = 5
Dim Paddledx As Integer
Dim Key As Keys
Dim WithEvents Timer As New Timer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DoubleBuffered = True
Me.BackColor = Color.Black
Me.Ball.Size = New Size(14, 14)
Me.Ball.Image = New Bitmap(Me.Ball.Size.Width, Me.Ball.Size.Height)
Using g As Graphics = Graphics.FromImage(Me.Ball.Image)
g.FillEllipse(Brushes.White, g.VisibleClipBounds)
End Using
Me.Paddle.Size = New Size(50, 14)
Me.Paddle.Image = New Bitmap(Me.Paddle.Size.Width, Me.Paddle.Size.Height)
Using g As Graphics = Graphics.FromImage(Me.Paddle.Image)
g.FillRectangle(Brushes.White, g.VisibleClipBounds)
End Using
Me.Paddle.Location = New Point((Me.ClientSize.Width - Me.Paddle.Width) / 2, Me.ClientSize.Height - Me.Paddle.Height)
Me.KeyPreview = True
Me.Timer.Interval = 10
Me.Timer.Start()
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Key = e.KeyCode
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Me.Paddle.Image IsNot Nothing AndAlso Me.Ball.Image IsNot Nothing Then
e.Graphics.DrawImage(Me.Paddle.Image, Me.Paddle.Location)
e.Graphics.DrawImage(Me.Ball.Image, Me.Ball.Location)
End If
End Sub
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer.Tick
If Me.Ball.Location.X + Me.Ball.Size.Width > Me.ClientSize.Width OrElse Me.Ball.Location.X < 0 Then
Me.Balldx *= -1
End If
If Me.Ball.Location.Y + Me.Ball.Size.Height > Me.ClientSize.Height OrElse Me.Ball.Location.Y < 0 Then
Me.Balldy *= -1
End If
Me.Ball.Location += New Point(Me.Balldx, Me.Balldy)
If Key = Keys.Right Then
Me.Paddledx = DirectCast(IIf(Me.Paddle.Location.X + Me.Paddle.Size.Width >= Me.ClientSize.Width, 0, 3), Integer)
ElseIf Key = Keys.Left Then
Me.Paddledx = DirectCast(IIf(Me.Paddle.Location.X <= 0, 0, -3), Integer)
End If
Me.Paddle.Location += New Point(Me.Paddledx, 0)
Me.Invalidate()
End Sub
End Class