Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 40 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
40
Dung lượng
1,14 MB
Nội dung
Figure 6.1 illustrates the concept of scrolling, which, in essence, involves the use of a large game world of which only a small portion is visible through the screen at a time. The key to scrolling is having something in the virtual game world to display in the scroll window (or the screen). Also, I should point out that the entire screen need not be used as the scroll window. It is common to use the entire screen in scrolling-shooter games, but role-playing games (RPGs) often use a smaller window on the screen for scrolling, using the rest of the screen for gameplay (combat, inventory, and so on) and player/party infor- mation, as shown in Figure 6.2. You could display one huge bitmap image in the virtual game world representing the cur- rent level of the game (or the map), and then copy a portion of that virtual world onto the screen. This is the simplest form of scrolling. Another method uses tiles to create the game world at runtime, which I cover in the next two chapters. First, you write a short program that demonstrates how to use bitmap scrolling, as this is an important first step to under- standing the process—and, one might argue, a good enough method in and of itself for creating a scrolling game. Chapter 6 ■ The Basics of Tile-Based Scrolling100 Figure 6.1 The scroll window shows a small part of a larger game world. A Limited View of the World I have written a program called ScrollScreen to show you. The \sources\chapter06 \ScrollScreen folder on this book’s CD-ROM contains the gameworld.bmp file used in this program. Although I encourage you to write the program yourself, you may load the project file off the CD-ROM if you wish (or I should say, if you are feeling lazy). Figure 6.3 shows the testworld.bmp bitmap file. When you run the ScrollScreen program, it loads the gameworld.bmp image into the vir- tual buffer (which is a Direct3D surface) and displays the upper-left corner in the 800 × 600 screen. You can change the resolution if you want, and I encourage you to try running the program in full-screen mode (by setting Fullscreen = True in the source code) for the best effect. The ScrollScreen program detects when the arrow keys have been pressed and then adjusts the game world’s x and y position accordingly, which causes it to look like the image is scrolling. Figure 6.4 shows the program running. A Limited View of the World 101 Figure 6.2 Some games use a smaller portion of the game screen for a scrolling window. Chapter 6 ■ The Basics of Tile-Based Scrolling102 Figure 6.3 The testworld.BMP file is loaded into the virtual memory buffer for scrolling. Figure 6.4 The ScrollScreen program demonstrates how to perform virtual buffer scrolling. Scrolling a Large Mappy-Generated Bitmap You can modify the gameworld.bmp file to display whatever you want, and it can be just about any size you want too (within reason). For instance, I took a map from Mappy and used the Export menu to save a giant bitmap image of the whole tile map, which works great for this example program. Figure 6.5 shows the Export dialog box in Mappy, where you can choose the Current Layer as Big Picture (?scrn.BMP) option to save the entire tile map as one large bitmap file. When you use Mappy to generate a single large bitmap image of a map, it saves the map exactly as it appears inside the Mappy tile editor window, as you can see in Figure 6.6. By modifying the source code in the ScrollScreen program (which is covered in the next section of this chapter), you can load a different bitmap file that is scrolled on the screen. Figure 6.7 shows the gameworld.bmp image being scrolled by this program. Creating the ScrollScreen Program The ScrollScreen program listing is coming up, so create a new project in Visual Basic so you can type it in. Make sure you add a reference to “DirectX 8 for Visual Basic Type Library,” selecting Project, References to bring up the References dialog box. A Limited View of the World 103 Figure 6.5 Exporting a Mappy map as one large bitmap image. Chapter 6 ■ The Basics of Tile-Based Scrolling104 Figure 6.6 The Mappy-generated tile map has been saved as a single large bitmap image. Figure 6.7 The ScrollScreen program running with a different map. caution Make sure you specify the correct width and height for the bitmap file that you use in this program ( GAMEWORLDWIDTH , GAMEWORLDHEIGHT ). Otherwise you are likely to get an Automation error as Direct3D tries to draw the image beyond the borders of the double buffer. You learn how to retrieve the bitmap dimensions directly from the bitmap file in Chapter 8, “Advanced Scrolling Techniques,” but until then, it must be specified manually. You can then type in the following source code into the code window for Form1. Copy the bitmap files out of the project folder on the CD-ROM (located in \sources\chapter06 \ScrollScreen) or create a large bitmap file for use by this program, as it works with any bitmap image as long as it’s bigger than the program’s screen resolution. I recommend generating a file with Mappy as explained in this chapter. One of the interesting things you want to do with this program is experiment with differ- ent screen resolutions to see how the scrolling view changes based on the resolution. I also encourage you to use Mappy to create huge tile maps, which you can fill with tiles. ‘ ‘ Visual Basic Game Programming for Teens ‘ Chapter 6 - ScrollScreen program ‘ Private Declare Function GetTickCount Lib “kernel32” () As Long ‘make sure every variable is declared Option Explicit ‘make all arrays start with 0 Option Base 0 ‘customize the program here Const SCREENWIDTH As Long = 800 Const SCREENHEIGHT As Long = 600 Const FULLSCREEN As Boolean = False Const GAMEWORLDWIDTH As Long = 1600 Const GAMEWORLDHEIGHT As Long = 1152 ‘keyboard codes Const KEY_LEFT As Integer = 72 Const KEY_RIGHT As Integer = 74 Const KEY_UP As Integer = 76 Const KEY_DOWN As Integer = 82 A Limited View of the World 105 ‘the DirectX objects Dim dx As DirectX8 Dim d3d As Direct3D8 Dim d3dx As New D3DX8 Dim dispmode As D3DDISPLAYMODE Dim d3dpp As D3DPRESENT_PARAMETERS Dim d3ddev As Direct3DDevice8 ‘some surfaces Dim backbuffer As Direct3DSurface8 Dim gameworld As Direct3DSurface8 ‘scrolling values Const STEP As Integer = 8 Dim ScrollX As Long Dim ScrollY As Long Dim SpeedX As Integer Dim SpeedY As Integer Private Sub Form_Load() ‘set up the main form Form1.Caption = “ScrollScreen” Form1.AutoRedraw = False Form1.BorderStyle = 1 Form1.ClipControls = False Form1.ScaleMode = 3 Form1.width = Screen.TwipsPerPixelX * (SCREENWIDTH + 12) Form1.height = Screen.TwipsPerPixelY * (SCREENHEIGHT + 30) Form1.Show ‘initialize Direct3D InitDirect3D Me.hwnd, SCREENWIDTH, SCREENHEIGHT, FULLSCREEN ‘get reference to the back buffer Set backbuffer = d3ddev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO) ‘load the bitmap file Set gameworld = LoadSurface(App.Path & “\testworld.bmp”, _ GAMEWORLDWIDTH, GAMEWORLDHEIGHT) ‘this helps to keep a steady framerate Dim start As Long start = GetTickCount() Chapter 6 ■ The Basics of Tile-Based Scrolling106 ‘main loop Do While (True) ‘update the scrolling viewport ScrollScreen ‘set the screen refresh to 40 fps (25 ms) If GetTickCount - start > 25 Then d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0 start = GetTickCount DoEvents End If Loop End Sub Public Sub InitDirect3D( _ ByVal hwnd As Long, _ ByVal lWidth As Long, _ ByVal lHeight As Long, _ ByVal bFullscreen As Boolean) ‘catch any errors here On Local Error GoTo fatal_error ‘create the DirectX object Set dx = New DirectX8 ‘create the Direct3D object Set d3d = dx.Direct3DCreate() If d3d Is Nothing Then MsgBox “Error initializing Direct3D!” Shutdown End If ‘tell D3D to use the current color depth d3d.GetAdapterDisplayMode D3DADAPTER_DEFAULT, dispmode ‘set the display settings used to create the device Dim d3dpp As D3DPRESENT_PARAMETERS d3dpp.hDeviceWindow = hwnd d3dpp.BackBufferCount = 1 d3dpp.BackBufferWidth = lWidth d3dpp.BackBufferHeight = lHeight A Limited View of the World 107 d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC d3dpp.BackBufferFormat = dispmode.Format ‘set windowed or fullscreen mode If bFullscreen Then d3dpp.Windowed = 0 Else d3dpp.Windowed = 1 End If ‘chapter 9 d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE d3dpp.AutoDepthStencilFormat = D3DFMT_D32 ‘create the D3D primary device Set d3ddev = d3d.CreateDevice( _ D3DADAPTER_DEFAULT, _ D3DDEVTYPE_HAL, _ hwnd, _ D3DCREATE_SOFTWARE_VERTEXPROCESSING, _ d3dpp) If d3ddev Is Nothing Then MsgBox “Error creating the Direct3D device!” Shutdown End If Exit Sub fatal_error: MsgBox “Critical error in Start_Direct3D!” Shutdown End Sub Private Function LoadSurface( _ ByVal filename As String, _ ByVal width As Long, _ ByVal height As Long) _ As Direct3DSurface8 On Local Error GoTo fatal_error Dim surf As Direct3DSurface8 Chapter 6 ■ The Basics of Tile-Based Scrolling108 ‘return error by default Set LoadSurface = Nothing ‘create the new surface Set surf = d3ddev.CreateImageSurface(width, height, dispmode.Format) If surf Is Nothing Then MsgBox “Error creating surface!” Exit Function End If ‘load surface from file d3dx.LoadSurfaceFromFile surf, ByVal 0, ByVal 0, filename, _ ByVal 0, D3DX_DEFAULT, 0, ByVal 0 If surf Is Nothing Then MsgBox “Error loading “ & filename & “!” Exit Function End If ‘return the new surface Set LoadSurface = surf fatal_error: Exit Function End Function Public Sub ScrollScreen() ‘update horizontal scrolling position and speed ScrollX = ScrollX + SpeedX If (ScrollX < 0) Then ScrollX = 0 SpeedX = 0 ElseIf ScrollX > GAMEWORLDWIDTH - SCREENWIDTH Then ScrollX = GAMEWORLDWIDTH - SCREENWIDTH SpeedX = 0 End If ‘update vertical scrolling position and speed ScrollY = ScrollY + SpeedY If ScrollY < 0 Then ScrollY = 0 SpeedY = 0 A Limited View of the World 109 [...]... “83,91 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 1 04, 4, 94, 4 ,4, 4 ,4, 4, 94, 4,93,91 ,4, 14, ” & _ “ 84, 96 ,4, 4, 24, 4, 94, 4,137, 94, 4 ,4, 4 ,4, 4 ,4, 1 14, 4, 14, 4 ,4, 93,91 ,4, 4 ,4, 4 ,4, ” & _ 4, 4,1 04, 4 ,4, 4 ,4, 4, 24, 4 ,4, 4 ,4, 4 ,4, 4,132 ,4, 93,91 ,4, 96 ,4, 132 ,4, 4 ,4, 4 ,4, ” & _ 4, 4 ,4, 4 ,4, 4 ,4, 96 ,4, 24, 4,96 ,4, 4,93,91 ,4, 4 ,4, 4 ,4, 4, 14, 4 ,4, 4,132 ,4, 4,” & _ “1 14, 4 ,4, 4 ,4, 4 ,4, 4,137 ,4, 93,91 ,4, 84, 1 14, 4, 94, 4 ,4, 4 ,4, 4,1 14, 4 ,4, 4 ,4, 4,” & _ 4, 4, 84, 4 ,4, 4, 94, 93,91 ,4, 4 ,4, 96 ,4, 132 ,4, 4 ,4, 1 14, 1 04, 4 ,4, 4 ,4, 4,137 ,4, 4,”... 4, 4, 84, 4 ,4, 4, 94, 93,91 ,4, 4 ,4, 96 ,4, 132 ,4, 4 ,4, 1 14, 1 04, 4 ,4, 4 ,4, 4,137 ,4, 4,” & _ “132 ,4, 4 ,4, 93,91 ,4, 24, 4 ,4, 4 ,4, 4 ,4, 96 ,4, 4, 84, 4 ,4, 4, 94, 4 ,4, 4 ,4, 4, 14, 4,” & _ “93,91 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4, 24, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 93,91 ,4, 4 ,4, 1 14, ” & _ Converting the Map Data to an Array 4, 94, 4 ,4, 137 ,4, 4 ,4, 4,1 04, 4 ,4, 4,96 ,4, 94, 4,96 ,4, 93,91 ,4, 137, 84, 4 ,4, 4 ,4, ” & _ 4, 4 ,4, 4 ,4, 4 ,4, 14, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 93,91 ,4, 4 ,4, 4, 14, 4 ,4, 4 ,4, 4,1 14, 96 ,4, ” & _ 4, 4 ,4, 4 ,4, 132 ,4, 137 ,4, 1 14, 93,91, 94, 4,132 ,4, 4 ,4, 4 ,4, 4, 94, 4,1 04, 4, 24, 4,”... 4, 4 ,4, 4 ,4, 4 ,4, 14, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 93,91 ,4, 4 ,4, 4, 14, 4 ,4, 4 ,4, 4,1 14, 96 ,4, ” & _ 4, 4 ,4, 4 ,4, 132 ,4, 137 ,4, 1 14, 93,91, 94, 4,132 ,4, 4 ,4, 4 ,4, 4, 94, 4,1 04, 4, 24, 4,” & _ 4, 4 ,4, 4 ,4, 4 ,4, 4,93,91 ,4, 4 ,4, 4 ,4, 96 ,4, 24, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 84, 4 ,4, 14, 4,” & _ “96 ,4, 93,91 ,4, 4 ,4, 4, 94, 4 ,4, 4 ,4, 4,132 ,4, 4 ,4, 14, 4 ,4, 4,1 14, 4 ,4, 4 ,4, 93,91,” & _ 4, 14, 4 ,4, 4 ,4, 4 ,4, 4,96 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4, 94, 4 ,4, 93,101,102,102,102,” & _ “102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,”... 10 24, 640 ) ‘create the scrolling game world bitmap Set gameworld = d3ddev.CreateImageSurface( _ GAMEWORLDWIDTH, GAMEWORLDHEIGHT, dispmode.Format) If gameworld Is Nothing Then MsgBox “Error creating working surface!” Shutdown End If 121 122 Chapter 7 ■ Scrolling the Game World ‘fill the gameworld bitmap with tiles For Y = 0 To 17 For X = 0 To MAPWIDTH - 1 DrawTile tiles, mapdata(Y * MAPWIDTH + X), 64, ... program to demonstrate how to create a tile-based game world at runtime The output of the program is shown in Figure 7 .4 Figure 7 .4 The TileScroll program demonstrates how to create a tiled game world at runtime 123 1 24 Chapter 7 ■ Scrolling the Game World You have already met most of the code used in this program, so I’d like you to create a new Visual Basic project, add the DirectX Type Library as usual,... DrawTile tiles, mapdata(Y * MAPWIDTH + X), 64, 64, 16, _ gameworld, X * 64, Y * 64 Next X Next Y ‘now the tiles bitmap is no longer needed Set tiles = Nothing End Sub Stepping Stones of the World The process of drawing tiles to fill the game world reminds me of laying down stepping stones, although tiling is admittedly a perfect analogy for what happens Basically, tiles of a larger pattern are laid down... Direct3DSurface8 Dim gameworld As Direct3DSurface8 ‘map data Dim mapdata(MAPWIDTH * MAPHEIGHT) As Integer ‘scrolling values Const STEP As Long = 8 Dim ScrollX As Long Dim ScrollY As Long Dim SpeedX As Integer Dim SpeedY As Integer Private Sub Form_Load() ‘set up the main form Form1.Caption = “DrawTile” Form1.ScaleMode = 3 Form1.width = Screen.TwipsPerPixelX * (SCREENWIDTH + 12) Form1.height = Screen.TwipsPerPixelY... the other (usually just scrolling up and down—vertically—or left to right—horizontally) These types of games are called shooters for the most part, although the horizontally scrolling games are usually platformers (think about games like Super Mario World, which was called Super Mario Advance 2 on the Game Boy Advance) Not only does Super Mario World have large horizontally scrolling levels, but those... extension of BAS Module files are just simple text files, and they help to clean up your projects, since the code in a form can become quite long in a typical game note I realize that a “real” Visual Basic program should use classes and object-oriented programming, which is a “better” form of code reuse than pasting code into a source code module That is an advanced feature, as far as I’m concerned,... The same map1.BMP file also contains the exported tile images, shown in Figure 7 .4; I explained how to use Mappy’s export features in the previous chapter ‘ ‘ Visual Basic Game Programming for Teens ‘ Chapter 6 - TileScroll program ‘ Private Declare Function GetTickCount Lib “kernel32” () As Long Option Explicit Option Base 0 Const MAPWIDTH As Long . _ “83,91 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 1 04, 4, 94, 4 ,4, 4 ,4, 4, 94, 4,93,91 ,4, 14, ” & _ “ 84, 96 ,4, 4, 24, 4, 94, 4,137, 94, 4 ,4, 4 ,4, 4 ,4, 1 14, 4, 14, 4 ,4, 93,91 ,4, 4 ,4, 4 ,4, ” & _ 4, 4,1 04, 4 ,4, 4 ,4, 4, 24, 4 ,4, 4 ,4, 4 ,4, 4,132 ,4, 93,91 ,4, 96 ,4, 132 ,4, 4 ,4, 4 ,4, ”. _ 4, 4, 84, 4 ,4, 4, 94, 93,91 ,4, 4 ,4, 96 ,4, 132 ,4, 4 ,4, 1 14, 1 04, 4 ,4, 4 ,4, 4,137 ,4, 4,” & _ “132 ,4, 4 ,4, 93,91 ,4, 24, 4 ,4, 4 ,4, 4 ,4, 96 ,4, 4, 84, 4 ,4, 4, 94, 4 ,4, 4 ,4, 4, 14, 4,” & _ “93,91 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4, 24, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 93,91 ,4, 4 ,4, 1 14, ”. _ 4, 4 ,4, 4 ,4, 4 ,4, 14, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 93,91 ,4, 4 ,4, 4, 14, 4 ,4, 4 ,4, 4,1 14, 96 ,4, ” & _ 4, 4 ,4, 4 ,4, 132 ,4, 137 ,4, 1 14, 93,91, 94, 4,132 ,4, 4 ,4, 4 ,4, 4, 94, 4,1 04, 4, 24, 4,” & _ 4, 4 ,4, 4 ,4, 4 ,4, 4,93,91 ,4, 4 ,4, 4 ,4, 96 ,4, 24, 4 ,4, 4 ,4, 4 ,4, 4 ,4, 84, 4 ,4, 14, 4,”