前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >游戏开发:经典赛马游戏回味【VB编程经典】

游戏开发:经典赛马游戏回味【VB编程经典】

作者头像
刘金玉编程
发布2023-08-31 13:15:56
2190
发布2023-08-31 13:15:56
举报
文章被收录于专栏:编程创造城市编程创造城市
游戏开发是软件学习的一个很好的途径,我们可以使用已经学习过的知识,来制作一些简单又有趣的游戏!本期我们来开发一款赛马游戏!

赛马游戏界面

人机交互的游戏

马的素材在QQ群

规则

1、一个玩家,一个电脑,一个终点 ------界面设计

2、鼠标点击标签,控制玩家前进 ------点击一次,产生一个随机数前进

3、电脑随机前进 -------------通过随机数来获得前进距离,难度系数是指电脑随机数范围的大小

4、比较谁先到终点 -----------可以通过timer监控玩家与电脑谁先到达

界面属性配置与项目源代码:

代码语言:javascript
复制
VERSION 5.00
Begin VB.Form Form1 
   BackColor       =   &H00FFFFFF&
   Caption         =   "赛马游戏--刘金玉编程"
   ClientHeight    =   6720
   ClientLeft      =   60
   ClientTop       =   405
   ClientWidth     =   11775
   LinkTopic       =   "Form1"
   ScaleHeight     =   6720
   ScaleWidth      =   11775
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command1 
      Caption         =   "开始游戏"
      Height          =   735
      Left            =   3240
      TabIndex        =   6
      Top             =   5280
      Width           =   1455
   End
   Begin VB.Timer Timer3 
      Enabled         =   0   'False
      Interval        =   1
      Left            =   10920
      Top             =   5040
   End
   Begin VB.Timer Timer2 
      Enabled         =   0   'False
      Interval        =   100
      Left            =   240
      Top             =   4320
   End
   Begin VB.Frame frameplayer 
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      BorderStyle     =   0  'None
      Caption         =   "Frame1"
      ForeColor       =   &H80000008&
      Height          =   1575
      Left            =   0
      TabIndex        =   3
      Top             =   2760
      Width           =   1215
      Begin VB.Label Label3 
         Alignment       =   2  'Center
         BackStyle       =   0  'Transparent
         Caption         =   "玩家"
         Height          =   255
         Left            =   120
         TabIndex        =   4
         Top             =   0
         Width           =   855
      End
      Begin VB.Image imgplayer 
         Height          =   900
         Left            =   0
         Picture         =   "Form1.frx":0000
         Stretch         =   -1  'True
         Top             =   480
         Width           =   1320
      End
   End
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   100
      Left            =   120
      Top             =   1680
   End
   Begin VB.Frame framepc 
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      BorderStyle     =   0  'None
      Caption         =   "Frame1"
      ForeColor       =   &H80000008&
      Height          =   1575
      Left            =   0
      TabIndex        =   1
      Top             =   240
      Width           =   1215
      Begin VB.Image imgpc 
         Height          =   900
         Left            =   0
         Picture         =   "Form1.frx":3557
         Stretch         =   -1  'True
         Top             =   360
         Width           =   1320
      End
      Begin VB.Label Label2 
         Alignment       =   2  'Center
         BackStyle       =   0  'Transparent
         Caption         =   "电脑"
         Height          =   255
         Left            =   120
         TabIndex        =   2
         Top             =   0
         Width           =   855
      End
   End
   Begin VB.Line Line3 
      X1              =   10680
      X2              =   10680
      Y1              =   0
      Y2              =   6720
   End
   Begin VB.Label Label4 
      Alignment       =   2  'Center
      Caption         =   "点我奔跑"
      BeginProperty Font 
         Name            =   "微软雅黑"
         Size            =   24
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   855
      Left            =   480
      TabIndex        =   5
      Top             =   5280
      Width           =   2295
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      BackColor       =   &H80000005&
      Caption         =   "终点区域"
      BeginProperty Font 
         Name            =   "微软雅黑"
         Size            =   42
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H80000008&
      Height          =   4815
      Left            =   10680
      TabIndex        =   0
      Top             =   0
      Width           =   1095
   End
   Begin VB.Line Line2 
      X1              =   0
      X2              =   11760
      Y1              =   4800
      Y2              =   4800
   End
   Begin VB.Line Line1 
      X1              =   0
      X2              =   11760
      Y1              =   2160
      Y2              =   2160
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim pcIndex%
Dim playerIndex%

Private Sub Command1_Click()
Timer1.Enabled = True
Timer2.Enabled = True
Timer3.Enabled = True
Command1.Enabled = False
framepc.Left = 0
frameplayer.Left = 0
End Sub

Private Sub Label4_Click()
Randomize
frameplayer.Left = frameplayer.Left + Rnd * 100
End Sub

Private Sub Timer1_Timer() '制作PC马的动画
pcIndex = pcIndex + 1
If pcIndex > 6 Then pcIndex = 1
imgpc.Picture = LoadPicture(App.Path & "/imgs/h (" & pcIndex & ").jpg")

Randomize
framepc.Left = framepc.Left + Rnd * 100

End Sub

Private Sub Timer2_Timer() '玩家的动画
playerIndex = playerIndex + 1
If playerIndex > 6 Then playerIndex = 1
imgplayer.Picture = LoadPicture(App.Path & "/imgs/h (" & playerIndex & ").jpg")

End Sub

Private Sub Timer3_Timer()
If framepc.Left + framepc.Width >= Line3.X1 Then
    
    MsgBox "电脑获胜", vbInformation, "通知"
    Timer3.Enabled = False
    Command1.Enabled = True
    Exit Sub
End If

If frameplayer.Left + frameplayer.Width >= Line3.X1 Then
    
    MsgBox "玩家获胜", vbInformation, "通知"
    Timer3.Enabled = False
    Command1.Enabled = True
    Exit Sub
End If

End Sub

课堂总结

1、学会把复杂问题简单化,通过步骤分解的方式,分解成一个个简单的小问题

2、更加关注用户体验

3、游戏开发,必须注重游戏的可玩性,提高可玩性

图片素材:

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-05-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程创造城市 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云小微
腾讯云小微,是一套腾讯云的智能服务系统,也是一个智能服务开放平台,接入小微的硬件可以快速具备听觉和视觉感知能力,帮助智能硬件厂商实现语音人机互动和音视频服务能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档