试图改变标签中的图像,当一个人按下某个按钮时 [英] Trying to change an image in a label, when a person pushes a certain button
问题描述
我声明的变量和最初的起始标签
pre $公共字母作为新列表字符串
公共共享x = 15
公共共享locationLetter作为字符串
公共共享locationNumber As Integer = 11
公共共享locationPlayer作为字符串
将玩家的船放入起始网格P11
P11.Image = My.Resources.Arrow
这段代码遍历每个标签然后找到具有图像的人,然后将其图像属性设置为无。它也改变了玩家的位置,在这种情况下,我希望图像从P11到O11。
Dim nextMove As String
Controls.Find(locationPlayer)
For Each lbl As标签在Controls.OfType(标签)
如果lbl.Image IsNot Nothing和x> = 0那么
x - = 1
lbl.Image = Nothing
locationLetter =字母。 Item(x)
locationPlayer = CStr(locationLetter& locationNumber)
如果lbl.Name = locationPlayer那么
lbl.Image = My.Resources.Arrow
End如果
结束如果
下一个
这行代码将相应的字母添加到这个字母列表,以便我可以调用它来连接找到玩家应该在的位置。
$ p code $ c Letters.Add( A)'0'
Letters.Add(B)'1
Letters.Add(C)'2
Letters.Add(D)' 3位
Letters.Add(E)'4位$ b $ (F)'5位置
Letters.Add(G)'6位置
Letters.Add(H)'7位置
Letters.Add I)'8位置
Letters.Add(J)'9位置
Letters.Add(K)'10位置
Letters.Add(L)' 11位置
Letters.Add(M)'12位置
Letters.Add(N)'13位置
Letters.Add(O)'14位置
Letters.Add(P)'15 position
locationLetter = Letters.Item(15)
我现在正在工作的代码,但是过度的代码是:
pre $ 如果P1。 Tag =playerThen
O1.Tag =player
O1.Image = My.Resources.Arrow
P1.Tag =
P1.Image = Nothing
btnDOWN.Enabled = True
btnLEFT.Enabled = False
ElseIf P2.Tag =playerThen
O2.Tag =player
O2.Image = My .Resourc es.Arrow
P2.Tag =
P2.Image = Nothing
btnDOWN.Enabled = True
ElseIf P3.Tag =playerThen
O3。 Tag =player
O3.Image = My.Resources.Arrow
P3.Tag =
P3.Image = Nothing
btnDOWN.Enabled = True
等我必须为每一个按钮做这个,所以这是336块x 4个按钮,或者大约6,720行代码将图像移动到另一个框。我的伪代码是这样的: 感谢您阅读我的帮助问题。我非常感谢所有的帮助,我可以得到
$ b $ pre $ 如果playerlocation =(一些网格号,例如P11)
找到标签的名称= playerlocation并添加图像来标签
ie
所以如果playerlocation = D4
找到名称为D4的标签并将图像添加到标签
-Galacticcore25
程序编程是好的!
一些半OOP:
描述你说你需要什么:
- 有尺寸;
- 包含一个数组,他们自己的属性);
- 必须允许虚拟玩家移动其单元格;
玩家:
- 它有一个位置 -
- 一个图片,它的位置的视觉表达;
- 一个动作范围:可以移动和只有在
董事会定义的单元格的范围内
建立一个Board对象(当然):
私人_BoardSize作为新的大小'董事会大小
私人_CellsArray作为BoardCell(,)'b
$ b
单元格数组
私有_Playe rDummy作为PlayerDummy
私人_Cells作为BoardCell
私人_cell作为BoardCell
私人_Location作为点
私人_Container作为控制
私人_PlayerPosition作为点'当前或默认位置player
Private _PlayerImage As Image'Player dummy Image
Private _Initialized As Boolean = False
BoardSize默认为21x15
Public Sub New()
Me.New(New Size(0,0))
End Sub
Public Sub New(_size As Size)
Me._BoardSize = _size
Me._cell =新的BoardCell
Me._cell.Size =新的大小(50,50)
Me._PlayerDummy =新的玩家
结束Sub
Friend Property BoardSize()As大小
获得
返回Me._BoardSize
结束获取
Set(ByVal value As Size)
Me._BoardSize = value
End Set
最终物业
Frien d Property Cell()作为BoardCell
获取
返回Me._cell
结束获取
Set(ByVal value As BoardCell)
Me._cell = value
End Set
End Property
ReadOnly Property Cell(_id As Point)As BoardCell
Get
返回Me._CellsArray(_id.X,_id.Y)
结束获取
结束属性
公共属性容器()作为控制
获取
返回_Container
结束获取
Set(ByVal value As Control)
_Container = value
Me._PlayerDummy.Parent = value
End Set
End Property
Public Property Location()As
Get
返回_Location
结束获取
Set(ByVal value As Point)
_Location = value
结束集
结束属性
公共属性PlayerPosition()作为点
Get
返回Me._PlayerPosition
结束获取
Set(value As As)
If Me._Initialized = True Then
'如果玩家位置发生变化,移动新的Cell
中的虚拟图像如果Me._PlayerPosition<>值然后
Me._PlayerPosition =值
Me._PlayerDummy.Location = Me._CellsArray(value.X,value.Y).Location
End If
End If
结束Set
End Property
Public Property PlayerImage()As Image
Get
返回Me._PlayerImage
结束获取
Set(value作为图像)
Me._PlayerImage =新的位图(值)
Me._PlayerDummy.Image = Me.PlayerImage
结束设置
结束属性
' Dimension(0,0)用于显示行/列标题
Public Sub Initialize(_size As Size)
Me._BoardSize = _size
'定义单元格数量
Me._CellsArray =新的BoardCell(_size.Width,_size.Height){}
每个维度(x,y)添加单元类
Dim x As Integer = 0
而x <= _BoardSize.Width
Dim y As Integer = 0
While y< = _BoardSize.Height
Me._CellsArray(x,y)= CreateBoardCell()
y + = 1
End While
x + = 1
End While
'绘制板子
对于x = 0 To Me._BoardSize.Width
对于y = 0 To Me._BoardSize.Height
Dim _position As Point = New Point(x,y)
如果x> 0 and y = 0 Then
Me.Cells(_position).Text = x.ToString
Me.Cells(_position).BackColor = Color.FromArgb(32,32,32)
Me .Cells(_position).ForeColor = Color.White
End If
如果y> 0 and x = 0 Then
Me.Cells(_position).Text = Chr(y + 64).ToString
Me.Cells(_position).BackColor = Color.FromArgb(32,32,32)
Me.Cells(_position).ForeColor = Color.White
End If
Me.Cells(_position).Location = New Point(Me._Location.X + x * Me.Cell.Size.Width,_
Me._Location.Y + y * Me.Cell.Size.Height)
Me.Cells(_position).Parent = Me.Container
Next
Next
Me.Cells(New Point(0,0))。BorderStyle = BorderStyle.None
Me.Cells(New Point(0,0))。BackColor = Me.Container。 BackColor
Me._Initialized = True
End Sub
私有函数CreateBoardCell()As BoardCell
Dim _boardcell As BoardCell = New BoardCell
_boardcell.Size = Me._cell.Size
_boardcell。 BackColor = Me._cell.BackColor
_boardcell.BorderStyle = Me._cell.BorderStyle
Me._PlayerDummy.Size =新大小(Me._cell.Size.Width - 1,Me._cell.Size.Height - 1)
返回_boardcell
结束函数
'定义Cell对象的类。从Label继承。
'可能是Panel提供了更多选项。不要使用PictureBoxes。
公共类BoardCell
继承标签
Public Sub New()
'安装程序默认属性
Me.AutoSize = False
Me.TextAlign = ContentAlignment.MiddleCenter
Me.Visible = True
End Sub
End Class
Friend类PlayerDummy
继承PictureBox
Private _Image As Image
Private _Parent As Control
$ b $ Public Sub New()
Me.SizeMode = PictureBoxSizeMode.Zoom
Me.BorderStyle = Windows.Forms.BorderStyle。 Fixed3D
Me.Visible = True
End Sub
Public Shadows属性Image()As Image
获取
返回Me._Image
结束获取
Set(ByVal value As Image)
MyBase.Image = value
Me._Image = value
End Set
End Prop erty
公共阴影属性Parent()作为控制
获得
返回_Parent
结束获取
Set(ByVal value As Control)
_Parent = value
MyBase.Parent = value
End Set
End Property
End Class
End Class
创建一个新的Board,实例化并定义它的属性
MyGameBoard =新的GameBoard
'开始位置绘制这个GameBoard
MyGameBoard.Location =新的点(50, 50)
MyGameBoard.Cell.Size =新大小(50,50)
MyGameBoard.Cell.BackColor = Color.Wheat
MyGameBoard.Cell.BorderStyle = BorderStyle.FixedSingle
'定义将包含该板的容器类(Form,Panel,PictureBox ...)
MyGameBoard.Container = Me
'将一个图像分配给新玩家对象并将其放置在其内部Board Cell
MyGameBoard.PlayerImage =新的位图(My.Resources.horse2)
'画板给它所需的大小
MyGameBoard.Initialize(新尺寸(10,10) )
现在,玩家
公共类玩家
公共类型方向'枚举此玩家允许的方向
Up = 0'也许它也可以对角移动
向下
Left
Right
End Enum
Private _Position As Point'Player Position
Private _Boundaries As New Rectangle'动作边界
Public Sub New()
Me.New(Nothing)
End Sub
$ b $ Public Sub New(_boundaries As Rectangle)
Me._Boundsaries = New Rectangle(1, 1,_boundaries.Width - 1,_boundaries.Height - 1)
End Sub
Public Property Position()As
Get
n Me._Position
结束获取
Set(value As Point)
'评估被设置的位置是否违反边界施加的
'约束条件
Me._Position .X = If(value.X> Me._Boundaries.Right,Me._Boundaries.Right,value.X)
Me._Position.X = If(value.X< Me._Boundaries.Left,Me._Boundaries.Left,value.X)
Me._Position.Y = If(value.Y> Me._Boundaries.Bottom,Me._Boundaries.Bottom,value.Y)
Me._Position.Y = If(value.Y< Me ._Boundaries.Top,Me._Boundaries.Top,value.Y)
结束设置
结束属性
公共属性边界()作为矩形
Get
返回Me._Boundaries
结束获取
Set(ByVal value As Rectangle)
Me._Boundsaries = value
End Set
End Property
玩家的移动。评估请求的操作是否违反边界
公共函数移动(_Direction As Direction)作为点
选择案例_direction
案例Direction.Up
Me.Position =新点(Me.Position .X,If(Me.Position.Y> Me._Boundaries.Top,Me.Position.Y-1,Me.Position.Y))
Exit Select
Case Direction.Down
Me.Position = New Point(Me.Position.X,If(Me.Position.Y< Me._Boundaries.Bottom,Me.Position.Y + 1,Me.Position.Y))
Exit Select
Case Direction.Left
Me.Position = New Point(If(Me.Position.X&Me._Boundaries.Left,Me.Position.X_1,Me.Position.X),Me .Position.Y)
退出选择
Case Direction.Right
Me.Position = New Point(If(Me.Position.X< Me._Boundaries.Right,Me.Position.X + 1,Me.Position.X),Me.Position.Y)
退出选择
End Select
返回Me._Position
End Function
End Class
$ b 创建一个新的运动边界=玩家的边界大小
MyPlayer =新玩家New Rectangle(New Point(1,1),MyGameBoard.BoardSize))
开始位置: p>
MyPlayer.Position =新点(10,10)
放置玩家假人
MyGameBoard.PlayerPosition = MyPlayer.Position
要移动它,只需使用Move方法并让董事会知道:
MyPlayer.Position = MyPlayer.Move(Player.Direction.Up)
MyGameBoard.PlayerPosition = MyPlayer.Position
放置一些控件让实际的玩家移动假人。
I am creating a game for my visual basic class. So far I have been successful, except for movement on my label grid. I have a 16, 21 label grid that I am using for the main map. The X axis is numeric 1-21 and the Y axis is letters A-P. So the upper left label is named A1 and the bottom right label is named P21. The player starts on label P11 and has an image of an arrow indicating their location. I also have an up, down, left, right buttons as well. When I press the up button I want the image to move itself to O11, or the above label. I have a solution, but it is very code extensive, and the up button alone is 1600+ line of code, which I think is a little excessive.
My variables that I declared and the initial starting label
Public Letters As New List(Of String)
Public Shared x = 15
Public Shared locationLetter As String
Public Shared locationNumber As Integer = 11
Public Shared locationPlayer As String
'Put player's ship in starting grid P11
P11.Image = My.Resources.Arrow
this code loops through each label and then finds the one that has the image and then sets its image property to nothing. It also changes the players location to what it should be, in this case I want the image to go from P11 to O11.
Dim nextMove As String
Controls.Find(locationPlayer)
For Each lbl As Label In Controls.OfType(Of Label)
If lbl.Image IsNot Nothing And x >= 0 Then
x -= 1
lbl.Image = Nothing
locationLetter = Letters.Item(x)
locationPlayer = CStr(locationLetter & locationNumber)
If lbl.Name = locationPlayer Then
lbl.Image = My.Resources.Arrow
End If
End If
Next
this line of code adds the appropriate letters to the Letters list so that I can call up it to concatenate to find the current position the player should be in
Letters.Add("A") ' 0 position
Letters.Add("B") ' 1 position
Letters.Add("C") ' 2 position
Letters.Add("D") ' 3 position
Letters.Add("E") ' 4 position
Letters.Add("F") ' 5 position
Letters.Add("G") ' 6 position
Letters.Add("H") ' 7 position
Letters.Add("I") ' 8 position
Letters.Add("J") ' 9 position
Letters.Add("K") ' 10 position
Letters.Add("L") ' 11 position
Letters.Add("M") ' 12 position
Letters.Add("N") ' 13 position
Letters.Add("O") ' 14 position
Letters.Add("P") ' 15 position
locationLetter = Letters.Item(15)
The code that I have now that is working, but is way excessive is:
If P1.Tag = "player" Then
O1.Tag = "player"
O1.Image = My.Resources.Arrow
P1.Tag = ""
P1.Image = Nothing
btnDOWN.Enabled = True
btnLEFT.Enabled = False
ElseIf P2.Tag = "player" Then
O2.Tag = "player"
O2.Image = My.Resources.Arrow
P2.Tag = ""
P2.Image = Nothing
btnDOWN.Enabled = True
ElseIf P3.Tag = "player" Then
O3.Tag = "player"
O3.Image = My.Resources.Arrow
P3.Tag = ""
P3.Image = Nothing
btnDOWN.Enabled = True
etc. I would have to do this for every single button, so that's 336 blocks x 4 buttons, or roughly 6,720 lines of code to move an image to another box. my pseudo code for this is:
If playerlocation = (some grid number, like P11 for example)
Find the label with the name = to playerlocation and add image to label
i.e.
so if playerlocation = D4
find the label with the name D4 and add the image to the label
thank you for reading my help question. I really appreciate all the help I can get
-Galacticcore25
解决方案 Procedural programming is good!
Some semi-OOP:
A description of what you said you need:
A Board
- it has Dimensions;
- contains a array of, lets say, Cells (which have their own properties);
- has to allow the movement of a dummy player over its Cells;
A Player:
- It has a position
- a picture which is the visual expression of its position;
- an action range: can move and only inside the range of the Cells that
the Board defines
Building a Board object (of course):
Public Class GameBoard
Private _BoardSize As New Size 'Board size
Private _CellsArray As BoardCell(,) 'The Cells array
Private _PlayerDummy As PlayerDummy
Private _Cells As BoardCell
Private _cell As BoardCell
Private _Location As Point
Private _Container As Control
Private _PlayerPosition As Point 'Current or default position of the player
Private _PlayerImage As Image 'Player dummy Image
Private _Initialized As Boolean = False
'The BoardSize defaults to 21x15
Public Sub New()
Me.New(New Size(0, 0))
End Sub
Public Sub New(_size As Size)
Me._BoardSize = _size
Me._cell = New BoardCell
Me._cell.Size = New Size(50, 50)
Me._PlayerDummy = New PlayerDummy
End Sub
Friend Property BoardSize() As Size
Get
Return Me._BoardSize
End Get
Set(ByVal value As Size)
Me._BoardSize = value
End Set
End Property
Friend Property Cell() As BoardCell
Get
Return Me._cell
End Get
Set(ByVal value As BoardCell)
Me._cell = value
End Set
End Property
Friend ReadOnly Property Cells(_id As Point) As BoardCell
Get
Return Me._CellsArray(_id.X, _id.Y)
End Get
End Property
Public Property Container() As Control
Get
Return _Container
End Get
Set(ByVal value As Control)
_Container = value
Me._PlayerDummy.Parent = value
End Set
End Property
Public Property Location() As Point
Get
Return _Location
End Get
Set(ByVal value As Point)
_Location = value
End Set
End Property
Public Property PlayerPosition() As Point
Get
Return Me._PlayerPosition
End Get
Set(value As Point)
If Me._Initialized = True Then
'If a player position changes, move the dummy image in the new Cell
If Me._PlayerPosition <> value Then
Me._PlayerPosition = value
Me._PlayerDummy.Location = Me._CellsArray(value.X, value.Y).Location
End If
End If
End Set
End Property
Public Property PlayerImage() As Image
Get
Return Me._PlayerImage
End Get
Set(value As Image)
Me._PlayerImage = New Bitmap(value)
Me._PlayerDummy.Image = Me.PlayerImage
End Set
End Property
'Dimension (0, 0) is used to show Rows/Columns headers
Public Sub Initialize(_size As Size)
Me._BoardSize = _size
'Defines the number of Cells
Me._CellsArray = New BoardCell(_size.Width, _size.Height) {}
'Add Cells classes per dimensions(x, y)
Dim x As Integer = 0
While x <= _BoardSize.Width
Dim y As Integer = 0
While y <= _BoardSize.Height
Me._CellsArray(x, y) = CreateBoardCell()
y += 1
End While
x += 1
End While
'Paint the Board
For x = 0 To Me._BoardSize.Width
For y = 0 To Me._BoardSize.Height
Dim _position As Point = New Point(x, y)
If x > 0 And y = 0 Then
Me.Cells(_position).Text = x.ToString
Me.Cells(_position).BackColor = Color.FromArgb(32, 32, 32)
Me.Cells(_position).ForeColor = Color.White
End If
If y > 0 And x = 0 Then
Me.Cells(_position).Text = Chr(y + 64).ToString
Me.Cells(_position).BackColor = Color.FromArgb(32, 32, 32)
Me.Cells(_position).ForeColor = Color.White
End If
Me.Cells(_position).Location = New Point(Me._Location.X + x * Me.Cell.Size.Width, _
Me._Location.Y + y * Me.Cell.Size.Height)
Me.Cells(_position).Parent = Me.Container
Next
Next
Me.Cells(New Point(0, 0)).BorderStyle = BorderStyle.None
Me.Cells(New Point(0, 0)).BackColor = Me.Container.BackColor
Me._Initialized = True
End Sub
Private Function CreateBoardCell() As BoardCell
Dim _boardcell As BoardCell = New BoardCell
_boardcell.Size = Me._cell.Size
_boardcell.BackColor = Me._cell.BackColor
_boardcell.BorderStyle = Me._cell.BorderStyle
Me._PlayerDummy.Size = New Size(Me._cell.Size.Width - 1, Me._cell.Size.Height - 1)
Return _boardcell
End Function
'A class defining a Cell object. Inherits from Label.
'May be a Panel gives more options. Do not use PictureBoxes.
Public Class BoardCell
Inherits Label
Public Sub New()
'Setup default properties
Me.AutoSize = False
Me.TextAlign = ContentAlignment.MiddleCenter
Me.Visible = True
End Sub
End Class
Friend Class PlayerDummy
Inherits PictureBox
Private _Image As Image
Private _Parent As Control
Public Sub New()
Me.SizeMode = PictureBoxSizeMode.Zoom
Me.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
Me.Visible = True
End Sub
Public Shadows Property Image() As Image
Get
Return Me._Image
End Get
Set(ByVal value As Image)
MyBase.Image = value
Me._Image = value
End Set
End Property
Public Shadows Property Parent() As Control
Get
Return _Parent
End Get
Set(ByVal value As Control)
_Parent = value
MyBase.Parent = value
End Set
End Property
End Class
End Class
To create a new Board, instantiate it and definine its properties
MyGameBoard = New GameBoard
'Starting position to draw this GameBoard
MyGameBoard.Location = New Point(50, 50)
MyGameBoard.Cell.Size = New Size(50, 50)
MyGameBoard.Cell.BackColor = Color.Wheat
MyGameBoard.Cell.BorderStyle = BorderStyle.FixedSingle
'Define the container class (Form, Panel, PictureBox...) that will contain this Board
MyGameBoard.Container = Me
'Assign an Image to the new player object and Position it inside its Board Cell
MyGameBoard.PlayerImage = New Bitmap(My.Resources.horse2)
'Paint the Board giving it desired size
MyGameBoard.Initialize(New Size(10, 10))
Now, the Player
Public Class Player
Public Enum Direction 'Enumerates this player allowed directions
Up = 0 'Maybe it could also move diagonally
Down
Left
Right
End Enum
Private _Position As Point 'Player Position
Private _Boundaries As New Rectangle 'The Boundaries of its movements
Public Sub New()
Me.New(Nothing)
End Sub
Public Sub New(_boundaries As Rectangle)
Me._Boundaries = New Rectangle(1, 1, _boundaries.Width - 1, _boundaries.Height - 1)
End Sub
Public Property Position() As Point
Get
Return Me._Position
End Get
Set(value As Point)
'Evaluates whether the position being set violates the
'constraints imposed by the Boundaries
Me._Position.X = If(value.X > Me._Boundaries.Right, Me._Boundaries.Right, value.X)
Me._Position.X = If(value.X < Me._Boundaries.Left, Me._Boundaries.Left, value.X)
Me._Position.Y = If(value.Y > Me._Boundaries.Bottom, Me._Boundaries.Bottom, value.Y)
Me._Position.Y = If(value.Y < Me._Boundaries.Top, Me._Boundaries.Top, value.Y)
End Set
End Property
Public Property Boundaries() As Rectangle
Get
Return Me._Boundaries
End Get
Set(ByVal value As Rectangle)
Me._Boundaries = value
End Set
End Property
'Move of the Player. Evaluates if the requested action violates Boundaries
Public Function Move(_direction As Direction) As Point
Select Case _direction
Case Direction.Up
Me.Position = New Point(Me.Position.X, If(Me.Position.Y > Me._Boundaries.Top, Me.Position.Y - 1, Me.Position.Y))
Exit Select
Case Direction.Down
Me.Position = New Point(Me.Position.X, If(Me.Position.Y < Me._Boundaries.Bottom, Me.Position.Y + 1, Me.Position.Y))
Exit Select
Case Direction.Left
Me.Position = New Point(If(Me.Position.X > Me._Boundaries.Left, Me.Position.X - 1, Me.Position.X), Me.Position.Y)
Exit Select
Case Direction.Right
Me.Position = New Point(If(Me.Position.X < Me._Boundaries.Right, Me.Position.X + 1, Me.Position.X), Me.Position.Y)
Exit Select
End Select
Return Me._Position
End Function
End Class
Create a new player with movement Boundaries = to the board Size
MyPlayer = New Player(New Rectangle(New Point(1, 1), MyGameBoard.BoardSize))
Starting position:
MyPlayer.Position = New Point(10, 10)
Place the Player Dummy
MyGameBoard.PlayerPosition = MyPlayer.Position
To move it just use the Move method and let the Board know about:
MyPlayer.Position = MyPlayer.Move(Player.Direction.Up)
MyGameBoard.PlayerPosition = MyPlayer.Position
Place some controls to let the actual human player move the dummy.
这篇关于试图改变标签中的图像,当一个人按下某个按钮时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!