五子棋的代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
using namespace std
const int N=15 //15*15的棋盘
const char ChessBoardflag = ' ' //棋盘标志
const char flag1='o' //玩家1或电脑的棋子标志
const char flag2='X' //玩家2的棋子标志
typedef struct Coordinate //坐标类
{
int x //代表行
int y //代表列
}Coordinate
class GoBang //五子棋类
{
public:
GoBang() //初始化
{
InitChessBoard()
}
void Play() //下棋
{
Coordinate Pos1 // 玩家1或电脑
Coordinate Pos2 //玩家2
int n = 0
while (1)
{
int mode = ChoiceMode()
while (1)
{
if (mode == 1) //电脑vs玩家
{
ComputerChess(Pos1,flag1) // 电脑下棋
if (GetVictory(Pos1, 0, flag1) == 1) //0表示电脑,真表示获胜
break
PlayChess(Pos2, 2, flag2) //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break
}
else //玩家1vs玩家2
{
PlayChess(Pos1, 1, flag1) // 玩家1下棋
if (GetVictory(Pos1, 1, flag1)) //1表示玩家1
break
PlayChess(Pos2, 2, flag2) //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break
}
}
cout <<"***再来一局***" <<endl
cout <<"y or n :"
char c = 'y'
cin >>c
if (c == 'n')
break
}
}
protected:
int ChoiceMode() //选择模式
{
int i = 0
system("cls") //系统调用,清屏
InitChessBoard() //重新初始化棋盘
cout <<"***0、退出 1、电脑vs玩家 2、玩家vs玩家***" <<endl
while (1)
{
cout <<"请选择:"
cin >>i
if (i == 0) //选择0退出
exit(1)
if (i == 1 || i == 2)
return i
cout <<"输入不合法" <<endl
}
}
void InitChessBoard() //初始化棋盘
{
for (int i = 0i <N + 1++i)
{
for (int j = 0j <N + 1++j)
{
_ChessBoard[i][j] = ChessBoardflag
}
}
}
void PrintChessBoard() //打印棋盘,这个函数可以自己调整
{
system("cls") //系统调用,清空屏幕
for (int i = 0i <N+1++i)
{
for (int j = 0j <N+1++j)
{
if (i == 0) //打印列数字
{
if (j!=0)
printf("%d ", j)
else
printf(" ")
}
else if (j == 0) //打印行数字
printf("%2d ", i)
else
{
if (i <N+1)
{
printf("%c |",_ChessBoard[i][j])
}
}
}
cout <<endl
cout <<" "
for (int m = 0m <Nm++)
{
printf("--|")
}
cout <<endl
}
}
void PlayChess(Coordinate&pos, int player, int flag) //玩家下棋
{
PrintChessBoard() //打印棋盘
while (1)
{
printf("玩家%d输入坐标:", player)
cin >>pos.x >>pos.y
if (JudgeValue(pos) == 1) //坐标合法
break
cout <<"坐标不合法,重新输入" <<endl
}
_ChessBoard[pos.x][pos.y] = flag
}
void ComputerChess(Coordinate&pos, char flag) //电脑下棋
{
PrintChessBoard() //打印棋盘
int x = 0
int y = 0
while (1)
{
x = (rand() % N) + 1 //产生1~N的随机数
srand((unsigned int) time(NULL))
y = (rand() % N) + 1 //产生1~N的随机数
srand((unsigned int) time(NULL))
if (_ChessBoard[x][y] == ChessBoardflag) //如果这个位置是空的,也就是没有棋子
break
}
pos.x = x
pos.y = y
_ChessBoard[pos.x][pos.y] = flag
}
int JudgeValue(const Coordinate&pos) //判断输入坐标是不是合法
{
if (pos.x >0 &&pos.x <= N&&pos.y >0 &&pos.y <= N)
{
if (_ChessBoard[pos.x][pos.y] == ChessBoardflag)
{
return 1 //合法
}
}
return 0 //非法
}
int JudgeVictory(Coordinate pos, char flag) //判断有没有人胜负(底层判断)
{
int begin = 0
int end = 0
int begin1 = 0
int end1 = 0
//判断行是否满足条件
(pos.y - 4) >0 ? begin = (pos.y - 4) : begin = 1
(pos.y + 4) >N ? end = N : end = (pos.y + 4)
for (int i = pos.x, j = beginj + 4 <= endj++)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i][j + 1] == flag&&
_ChessBoard[i][j + 2] == flag&&_ChessBoard[i][j + 3] == flag&&
_ChessBoard[i][j + 4] == flag)
return 1
}
//判断列是否满足条件
(pos.x - 4) >0 ? begin = (pos.x - 4) : begin = 1
(pos.x + 4) >N ? end = N : end = (pos.x + 4)
for (int j = pos.y, i = begini + 4 <= endi++)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j] == flag&&
_ChessBoard[i + 2][j] == flag&&_ChessBoard[i + 3][j] == flag&&
_ChessBoard[i + 4][j] == flag)
return 1
}
int len = 0
//判断主对角线是否满足条件
pos.x >pos.y ? len = pos.y - 1 : len = pos.x - 1
if (len >4)
len = 4
begin = pos.x - len //横坐标的起始位置
begin1 = pos.y - len //纵坐标的起始位置
pos.x >pos.y ? len = (N - pos.x) : len = (N - pos.y)
if (len>4)
len = 4
end = pos.x + len //横坐标的结束位置
end1 = pos.y + len //纵坐标的结束位置
for (int i = begin, j = begin1(i + 4 <= end) &&(j + 4 <= end1)++i, ++j)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j + 1] == flag&&
_ChessBoard[i + 2][j + 2] == flag&&_ChessBoard[i + 3][j + 3] == flag&&
_ChessBoard[i + 4][j + 4] == flag)
return 1
}
//判断副对角线是否满足条件
(pos.x - 1) >(N - pos.y) ? len = (N - pos.y) : len = pos.x - 1
if (len >4)
len = 4
begin = pos.x - len //横坐标的起始位置
begin1 = pos.y + len //纵坐标的起始位置
(N - pos.x) >(pos.y - 1) ? len = (pos.y - 1) : len = (N - pos.x)
if (len>4)
len = 4
end = pos.x + len //横坐标的结束位置
end1 = pos.y - len //纵坐标的结束位置
for (int i = begin, j = begin1(i + 4 <= end) &&(j - 4 >= end1)++i, --j)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j - 1] == flag&&
_ChessBoard[i + 2][j - 2] == flag&&_ChessBoard[i + 3][j - 3] == flag&&
_ChessBoard[i + 4][j - 4] == flag)
return 1
}
for (int i = 1i <N + 1++i) //棋盘有没有下满
{
for (int j =1j <N + 1++j)
{
if (_ChessBoard[i][j] == ChessBoardflag)
return 0 //0表示棋盘没满
}
}
return -1 //和棋
}
bool GetVictory(Coordinate&pos, int player, int flag) //对JudgeVictory的一层封装,得到具体那个玩家获胜
{
int n = JudgeVictory(pos, flag) //判断有没有人获胜
if (n != 0) //有人获胜,0表示没有人获胜
{
PrintChessBoard()
if (n == 1) //有玩家赢棋
{
if (player == 0) //0表示电脑获胜,1表示玩家1,2表示玩家2
printf("***电脑获胜***\n")
else
printf("***恭喜玩家%d获胜***\n", player)
}
else
printf("***双方和棋***\n")
return true //已经有人获胜
}
return false //没有人获胜
}
private:
char _ChessBoard[N+1][N+1]
}
扩展资料:设计思路
1、进行问题分析与设计,计划实现的功能为,开局选择人机或双人对战,确定之后比赛开始。
2、比赛结束后初始化棋盘,询问是否继续比赛或退出,后续可加入复盘、悔棋等功能。
3、整个过程中,涉及到了棋子和棋盘两种对象,同时要加上人机对弈时的AI对象,即涉及到三个对象。
像素射击:int anim abc_fade_in 0x7f010001int anim abc_fade_out 0x7f010002
int anim abc_grow_fade_in_from_bottom 0x7f010003
int anim abc_popup_enter 0x7f010004
int anim abc_popup_exit 0x7f010005
int anim abc_shrink_fade_out_from_bottom 0x7f010006
int anim abc_slide_in_bottom 0x7f010007
int anim abc_slide_in_top 0x7f010008
int anim abc_slide_out_bottom 0x7f010009
int anim abc_slide_out_top 0x7f01000a
int anim abc_tooltip_enter 0x7f01000b
int anim abc_tooltip_exit 0x7f01000c
int attr actionBarDivider 0x7f040001
int attr actionBarItemBackground 0x7f040002
int attr actionBarPopupTheme 0x7f040003
int attr actionBarSize 0x7f040004
int attr actionBarSplitStyle 0x7f040005
int attr actionBarStyle 0x7f040006
int attr actionBarTabBarStyle 0x7f040007
int attr actionBarTabStyle 0x7f040008
int attr actionBarTabTextStyle 0x7f040009
int attr actionBarTheme 0x7f04000a
int attr actionBarWidgetTheme 0x7f04000b
int attr actionButtonStyle 0x7f04000c
int attr actionDropDownStyle 0x7f04000d
int attr actionLayout 0x7f04000e
int attr actionMenuTextAppearance 0x7f04000f
int attr actionMenuTextColor 0x7f040010
int attr actionModeBackground 0x7f040011
int attr actionModeCloseButtonStyle 0x7f040012
int attr actionModeCloseDrawable 0x7f040013
int attr actionModeCopyDrawable 0x7f040014
int attr actionModeCutDrawable 0x7f040015
int attr actionModeFindDrawable 0x7f040016
int attr actionModePasteDrawable 0x7f040017
int attr actionModePopupWindowStyle 0x7f040018
int attr actionModeSelectAllDrawable 0x7f040019
int attr actionModeShareDrawable 0x7f04001a
int attr actionModeSplitBackground 0x7f04001b
int attr actionModeStyle 0x7f04001c
int attr actionModeWebSearchDrawable 0x7f04001d
int attr actionOverflowButtonStyle 0x7f04001e
int attr actionOverflowMenuStyle 0x7f04001f
int attr actionProviderClass 0x7f040020
int attr actionViewClass 0x7f040021
int attr activityChooserViewStyle 0x7f040022
int attr alertDialogButtonGroupStyle 0x7f040023
int attr alertDialogCenterButtons 0x7f040024
int attr alertDialogStyle 0x7f040025
int attr alertDialogTheme 0x7f040026
int attr allowStacking 0x7f040027
int attr alpha 0x7f040028
int attr alphabeticModifiers 0x7f040029
int attr arrowHeadLength 0x7f04002a
int attr arrowShaftLength 0x7f04002b
int attr autoCompleteTextViewStyle 0x7f04002c
int attr autoSizeMaxTextSize 0x7f04002d
int attr autoSizeMinTextSize 0x7f04002e
int attr autoSizePresetSizes 0x7f04002f
int attr autoSizeStepGranularity 0x7f040030
int attr autoSizeTextType 0x7f040031
int attr background 0x7f040032
int attr backgroundSplit 0x7f040033
int attr backgroundStacked 0x7f040034
int attr backgroundTint 0x7f040035
int attr backgroundTintMode 0x7f040036
int attr barLength 0x7f040037
int attr borderlessButtonStyle 0x7f040038
int attr buttonBarButtonStyle 0x7f040039
int attr buttonBarNegativeButtonStyle 0x7f04003a
int attr buttonBarNeutralButtonStyle 0x7f04003b
int attr buttonBarPositiveButtonStyle 0x7f04003c
int attr buttonBarStyle 0x7f04003d
int attr buttonGravity 0x7f04003e
int attr buttonIconDimen 0x7f04003f
int attr buttonPanelSideLayout 0x7f040040
int attr buttonStyle 0x7f040041
int attr buttonStyleSmall 0x7f040042
int attr buttonTint 0x7f040043
int attr buttonTintMode 0x7f040044
int attr checkboxStyle 0x7f040045
int attr checkedTextViewStyle 0x7f040046
int attr closeIcon 0x7f040047
int attr closeItemLayout 0x7f040048
int attr collapseContentDescription 0x7f040049
int attr collapseIcon 0x7f04004a
int attr color 0x7f04004b
int attr colorAccent 0x7f04004c
int attr colorBackgroundFloating 0x7f04004d
int attr colorButtonNormal 0x7f04004e
int attr colorControlActivated 0x7f04004f
int attr colorControlHighlight 0x7f040050
int attr colorControlNormal 0x7f040051
int attr colorError 0x7f040052
int attr colorPrimary 0x7f040053
int attr colorPrimaryDark 0x7f040054
int attr colorSwitchThumbNormal 0x7f040055
int attr commitIcon 0x7f040056
int attr contentDescription 0x7f040057
int attr contentInsetEnd 0x7f040058
int attr contentInsetEndWithActions 0x7f040059
int attr contentInsetLeft 0x7f04005a
int attr contentInsetRight 0x7f04005b
int attr contentInsetStart 0x7f04005c
int attr contentInsetStartWithNavigation 0x7f04005d
int attr controlBackground 0x7f04005e
int attr coordinatorLayoutStyle 0x7f04005f
int attr customNavigationLayout 0x7f040060
int attr defaultQueryHint 0x7f040061
int attr dialogCornerRadius 0x7f040062
int attr dialogPreferredPadding 0x7f040063
int attr dialogTheme 0x7f040064
int attr displayOptions 0x7f040065
int attr divider 0x7f040066
int attr dividerHorizontal 0x7f040067
int attr dividerPadding 0x7f040068
int attr dividerVertical 0x7f040069
int attr drawableSize 0x7f04006a
int attr drawerArrowStyle 0x7f04006b
int attr dropDownListViewStyle 0x7f04006c
int attr dropdownListPreferredItemHeight 0x7f04006d
int attr editTextBackground 0x7f04006e
int attr editTextColor 0x7f04006f
int attr editTextStyle 0x7f040070
int attr elevation 0x7f040071
int attr expandActivityOverflowButtonDrawable 0x7f040072
int attr firstBaselineToTopHeight 0x7f040073
int attr font 0x7f040074
int attr fontFamily 0x7f040075
int attr fontProviderAuthority 0x7f040076
int attr fontProviderCerts 0x7f040077
int attr fontProviderFetchStrategy 0x7f040078
int attr fontProviderFetchTimeout 0x7f040079
int attr fontProviderPackage 0x7f04007a
int attr fontProviderQuery 0x7f04007b
int attr fontStyle 0x7f04007c
int attr fontVariationSettings 0x7f04007d
int attr fontWeight 0x7f04007e
int attr gapBetweenBars 0x7f04007f
int attr goIcon 0x7f040080
int attr height 0x7f040081
int attr hideOnContentScroll 0x7f040082
int attr homeAsUpIndicator 0x7f040083
int attr homeLayout 0x7f040084
int attr icon 0x7f040085
int attr iconTint 0x7f040086
int attr iconTintMode 0x7f040087
int attr iconifiedByDefault 0x7f040088
int attr imageButtonStyle 0x7f040089
int attr indeterminateProgressStyle 0x7f04008a
int attr initialActivityCount 0x7f04008b
int attr isLightTheme 0x7f04008c
int attr itemPadding 0x7f04008d
int attr keylines 0x7f04008e
int attr lastBaselineToBottomHeight 0x7f04008f
int attr layout 0x7f040090
int attr layout_anchor 0x7f040091
int attr layout_anchorGravity 0x7f040092
int attr layout_behavior 0x7f040093
int attr layout_dodgeInsetEdges 0x7f040094
int attr layout_insetEdge 0x7f040095
int attr layout_keyline 0x7f040096
int attr lineHeight 0x7f040097
int attr listChoiceBackgroundIndicator 0x7f040098
int attr listDividerAlertDialog 0x7f040099
int attr listItemLayout 0x7f04009a
int attr listLayout 0x7f04009b
int attr listMenuViewStyle 0x7f04009c
int attr listPopupWindowStyle 0x7f04009d
int attr listPreferredItemHeight 0x7f04009e
int attr listPreferredItemHeightLarge 0x7f04009f
int attr listPreferredItemHeightSmall 0x7f0400a0
int attr listPreferredItemPaddingLeft 0x7f0400a1
int attr listPreferredItemPaddingRight 0x7f0400a2
int attr logo 0x7f0400a3
int attr logoDescription 0x7f0400a4
int attr maxButtonHeight 0x7f0400a5
int attr measureWithLargestChild 0x7f0400a6
int attr multiChoiceItemLayout 0x7f0400a7
int attr navigationContentDescription 0x7f0400a8
int attr navigationIcon 0x7f0400a9
int attr navigationMode 0x7f0400aa
int attr numericModifiers 0x7f0400ab
int attr overlapAnchor 0x7f0400ac
int attr paddingBottomNoButtons 0x7f0400ad
int attr paddingEnd 0x7f0400ae
int attr paddingStart 0x7f0400af
int attr paddingTopNoTitle 0x7f0400b0
int attr panelBackground 0x7f0400b1
int attr panelMenuListTheme 0x7f0400b2
int attr panelMenuListWidth 0x7f0400b3
int attr popupMenuStyle 0x7f0400b4
int attr popupTheme 0x7f0400b5
int attr popupWindowStyle 0x7f0400b6
int attr preserveIconSpacing 0x7f0400b7
int attr progressBarPadding 0x7f0400b8
int attr progressBarStyle 0x7f0400b9
int attr queryBackground 0x7f0400ba
int attr queryHint 0x7f0400bb
int attr radioButtonStyle 0x7f0400bc
int attr ratingBarStyle 0x7f0400bd
int attr ratingBarStyleIndicator 0x7f0400be
int attr ratingBarStyleSmall 0x7f0400bf
int attr searchHintIcon 0x7f0400c0
int attr searchIcon 0x7f0400c1
int attr searchViewStyle 0x7f0400c2
int attr seekBarStyle 0x7f0400c3
int attr selectableItemBackground 0x7f0400c4
int attr selectableItemBackgroundBorderless 0x7f0400c5
int attr showAsAction 0x7f0400c6
int attr showDividers 0x7f0400c7
int attr showText 0x7f0400c8
int attr showTitle 0x7f0400c9
int attr singleChoiceItemLayout 0x7f0400ca
int attr spinBars 0x7f0400cb
int attr spinnerDropDownItemStyle 0x7f0400cc
int attr spinnerStyle 0x7f0400cd
int attr splitTrack 0x7f0400ce
int attr srcCompat 0x7f0400cf
int attr state_above_anchor 0x7f0400d0
int attr statusBarBackground 0x7f0400d1
int attr subMenuArrow 0x7f0400d2
int attr submitBackground 0x7f0400d3
int attr subtitle 0x7f0400d4
int attr subtitleTextAppearance 0x7f0400d5
int attr subtitleTextColor 0x7f0400d6
int attr subtitleTextStyle 0x7f0400d7
int attr suggestionRowLayout 0x7f0400d8
int attr switchMinWidth 0x7f0400d9
int attr switchPadding 0x7f0400da
int attr switchStyle 0x7f0400db
int attr switchTextAppearance 0x7f0400dc
int attr textAllCaps 0x7f0400dd
int attr textAppearanceLargePopupMenu 0x7f0400de
int attr textAppearanceListItem 0x7f0400df
int attr textAppearanceListItemSecondary 0x7f0400e0
int attr textAppearanceListItemSmall 0x7f0400e1
int attr textAppearancePopupMenuHeader 0x7f0400e2
int attr textAppearanceSearchResultSubtitle 0x7f0400e3
int attr textAppearanceSearchResultTitle 0x7f0400e4
int attr textAppearanceSmallPopupMenu 0x7f0400e5
int attr textColorAlertDialogListItem 0x7f0400e6
int attr textColorSearchUrl 0x7f0400e7
int attr theme 0x7f0400e8
int attr thickness 0x7f0400e9
int attr thumbTextPadding 0x7f0400ea
int attr thumbTint 0x7f0400eb
int attr thumbTintMode 0x7f0400ec
int attr tickMark 0x7f0400ed
int attr tickMarkTint 0x7f0400ee
int attr tickMarkTintMode 0x7f0400ef
int attr tint 0x7f0400f0
int attr tintMode 0x7f0400f1
int attr title 0x7f0400f2
int attr titleMargin 0x7f0400f3
int attr titleMarginBottom 0x7f0400f4
int attr titleMarginEnd 0x7f0400f5
int attr titleMarginStart 0x7f0400f6
int attr titleMarginTop 0x7f0400f7
int attr titleMargins 0x7f0400f8
int attr titleTextAppearance 0x7f0400f9
int attr titleTextColor 0x7f
你是要源程序,还是要安装软件?????角色击中标志
为了使程序中的仙人掌、游戏角色和射击时发射的子d可以移动,需要向项目中添加定时器tmrMouseCnt和Timer1,在这两个定时响应函数中完成不同对象的移动功能。在游戏运行后,为了使用户可以通过键盘和鼠标来 *** 作游戏的角色,实现射击的功能,需要添加鼠标消息和键盘消息处理函数。例如,对于角色1来说,可以通过上下键来移动,空格键来射击,对于角色2来说,鼠标左右键控制移动,双击实现射击。在射击过程中,要处理两个细节,一个细节是子d与仙人掌及角色的区域重叠问题,当子d与仙人掌重叠时让子d隐藏起来,与角色重叠时表示击中目标,游戏结束。这里需要判断何时两个区域有重叠,解决这个问题的方法是使用API函数IntersectRect,用它来判断两个区域是否有重叠。另一个细节是子d射击过程中需要添加"呼啸"的声音和击中目标时添加人物惨叫的声音,来达到逼真的效果,为了实现这个功能,需要向程序中添加语音文件(程序中的语音文件分别为:BANG.WAV和OH!!.WAV),然后通过API函数sndPlaySound来实现。另外,在对象移动的过程中,需要注意移动到边缘位置的情况处理。
程序的具体实现代码如下:
SHOOTOUT.BAS
Option Explicit
' Data type required by the IntersectRect function
Type tRect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
' Windows API rectangle functions
Declare Function IntersectRect Lib "user32" (lpDestRect As tRect, lpSrc1Rect As tRect, lpSrc2Rect As tRect) As Long
' Functions and constants used to play sounds.
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
' Constant used with sndPlaySound function
Global Const SND_ASYNC = &H1
'----------------------------------------------------------
' SHOOTOUT.FRM
Option Explicit
' KeyCodes for keyboard action.
Const KEY_SPACE = &H20
Const KEY_UP = &H26
Const KEY_DOWN = &H28
' Number of Twips to move player on each key or mouse event.
Const PlayerIncrement = 45
' Constants for mouse action.
Const NO_BUTTON = 0
Const LBUTTON = 1
Const RBUTTON = 2
' Boolean that indicates if mouse button has been pressed down.
Dim MouseButtonDown As Integer
' Number of bullets either player can have in use at one time.
Const NUM_BULLETS = 6
' Booleans indicating if player 0 or player 1 have just fired.
Dim GunFired(0 To 1) As Integer
' Start the game by enabling the main timer and hiding the start button.
Private Sub btnStart_Click()
Timer1.Enabled = True
btnStart.Visible = False
End Sub
' Check if the two Images intersect, using the IntersectRect API call.
Private Function Collided(imgA As Image, imgB As Image) As Integer
Dim A As tRect
Dim B As tRect
Dim ResultRect As tRect
' Copy information into tRect structure
A.Left = imgA.Left
A.Top = imgA.Top
B.Left = imgB.Left
B.Top = imgB.Top
' Calculate the right and bottoms of rectangles needed by the API call.
A.Right = A.Left + imgA.Width - 1
A.Bottom = A.Top + imgA.Height - 1
B.Right = B.Left + imgB.Width - 1
B.Bottom = B.Top + imgB.Height - 1
' IntersectRect will only return 0 (false) if the
' two rectangles do NOT intersect.
Collided = IntersectRect(ResultRect, A, B)
End Function
' Double-clicking the mouse fires Player 1's gun.
Private Sub Form_DblClick()
Dim rc As Integer
If Not Timer1.Enabled Then Exit Sub
GunFired(1) = True
rc = sndPlaySound(App.Path &"\BANG.WAV", SND_ASYNC)
End Sub
' This event handles Player 0's game action via the keyboard.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim rc As Integer
Static InKeyDown As Integer
If Not Timer1.Enabled Then Exit Sub
If InKeyDown Then Exit Sub
InKeyDown = True
DoEvents
Select Case KeyCode
Case KEY_UP
imgPlayer(0).Top = imgPlayer(0).Top - PlayerIncrement
If imgPlayer(0).Top <0 Then imgPlayer(0).Top = 0
Case KEY_SPACE
GunFired(0) = True
rc = sndPlaySound(App.Path &"\BANG.WAV", SND_ASYNC)
Case KEY_DOWN
imgPlayer(0).Top = imgPlayer(0).Top + PlayerIncrement
If imgPlayer(0).Top >(picDesert.ScaleHeight -
imgPlayer(0).Height) Then
imgPlayer(0).Top = picDesert.ScaleHeight -
imgPlayer(0).Height
End If
End Select
InKeyDown = False
End Sub
Private Sub Form_Load()
Dim i As Integer
Timer1.Interval = 22
Timer1.Enabled = False
MouseButtonDown = NO_BUTTON
For i = 1 To NUM_BULLETS - 1
Load imgLBullet(i)
Load imgRBullet(i)
Next
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
MouseButtonDown = Button
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MouseButtonDown = NO_BUTTON
End Sub
' The main game timer.
Private Sub Timer1_Timer()
Const CactusIncrement = 30
Const BulletIncrement = 300
Const NumCacti = 2
Dim i As Integer
Dim rc As Integer
' Move the roving cacti.
For i = 0 To NumCacti - 1
imgCactus(i).Top = imgCactus(i).Top - CactusIncrement
If imgCactus(i).Top <-imgCactus(i).Height Then
imgCactus(i).Top = picDesert.Height
End If
Next
' Did player 0 fire a bullet?
If GunFired(0) Then
GunFired(0) = False
' Find a spare (invisible) bullet.
For i = 0 To NUM_BULLETS - 1
If Not imgLBullet(i).Visible Then
imgLBullet(i).Top = imgPlayer(0).Top
imgLBullet(i).Left = imgPlayer(0).Left +
(imgPlayer(0).Width / 2)
imgLBullet(i).Visible = True
Exit For
End If
Next
End If
' Did player 1 fire a bullet?
If GunFired(1) Then
GunFired(1) = False
' Find a spare (invisible) bullet.
For i = 0 To NUM_BULLETS - 1
If Not imgRBullet(i).Visible Then
imgRBullet(i).Top = imgPlayer(1).Top
imgRBullet(i).Left = imgPlayer(1).Left -
(imgPlayer(1).Width / 2)
imgRBullet(i).Visible = True
Exit For
End If
Next
End If
' Move Visible Bullets
For i = 0 To NUM_BULLETS - 1
' Move player 0's bullets.
If imgLBullet(i).Visible Then
imgLBullet(i).Left = imgLBullet(i).Left + BulletIncrement
If Collided(imgLBullet(i), imgCactus(0)) Then
imgLBullet(i).Visible = False
ElseIf Collided(imgLBullet(i), imgCactus(1)) Then
imgLBullet(i).Visible = False
ElseIf imgLBullet(i).Left >picDesert.ScaleWidth Then
imgLBullet(i).Visible = False
ElseIf Collided(imgLBullet(i), imgPlayer(1)) Then
imgLBullet(i).Visible = False
imgPlayer(1).Picture = imgRIP.Picture
Timer1.Enabled = False
rc = sndPlaySound(App.Path &"\OH!!.WAV", SND_ASYNC)
End If
End If
' Move player 1's bullets.
If imgRBullet(i).Visible Then
imgRBullet(i).Left = imgRBullet(i).Left - BulletIncrement
If Collided(imgRBullet(i), imgCactus(0)) Then
imgRBullet(i).Visible = False
ElseIf Collided(imgRBullet(i), imgCactus(1)) Then
imgRBullet(i).Visible = False
ElseIf imgRBullet(i).Left <-imgRBullet(i).Width Then
imgRBullet(i).Visible = False
ElseIf Collided(imgRBullet(i), imgPlayer(0)) Then
imgRBullet(i).Visible = False
imgPlayer(0).Picture = imgRIP.Picture
Timer1.Enabled = False
rc = sndPlaySound(App.Path &"\OH!!.WAV", SND_ASYNC)
End If
End If
Next
End Sub
' Handle Player 1's movement (up and down).
Private Sub tmrMouseCntl_Timer()
If Not Timer1.Enabled Then Exit Sub
Select Case MouseButtonDown
Case RBUTTON
imgPlayer(1).Top = imgPlayer(1).Top - PlayerIncrement
If imgPlayer(1).Top <0 Then imgPlayer(1).Top = 0
Case LBUTTON
imgPlayer(1).Top = imgPlayer(1).Top + PlayerIncrement
If imgPlayer(1).Top >(picDesert.ScaleHeight -
imgPlayer(1).Height) Then
imgPlayer(1).Top = picDesert.ScaleHeight -
imgPlayer(1).Height
End If
End Select
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)