Dim Backup As System.Drawing.Bitmap 'Memory buffer - will be drawn in one fell swoop. Dim GFX, FGFX As System.Drawing.Graphics 'Should draw to the memory buffer declared above. Dim B As Integer '*** 1 *** , other declarations go here. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Backup = New Bitmap(640, 480) '640x480 is the size of the memory bitmap, which will 'effectively be the size of the display, since nothing can go off of it. GFX = Graphics.FromImage(Backup) 'Draws to the memory buffer. FGFX = Me.CreateGraphics() 'Draws to the display (form). '*** 2 *** , other instantiations go here. End Sub Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click B += 10 'B symbolizes the location of something moving on the form. GFX.FillRectangle(Brushes.Black, Me.ClientRectangle) GFX.FillRectangle(Brushes.DarkGoldenrod, New Rectangle(B, B, 100, 100)) 'Draws to the backup bitmap. '*** 3 *** , delete this, we are not going to click a map. Present() End Sub Private Sub Present() '*** 4 *** , typically nothing else will go in this routine. FGFX.DrawImage(Backup, 0, 0) 'Present the memory bitmap to the display. End Sub Private Sub Clock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clock.Tick '*** 5 *** , game processing and general logic goes here. '*** 6 *** , drawing the state of the game goes here. Present() End Sub |
System.Drawing.Bitmap backup; //Memory bitmap that will hold drawings that will be drawn all at once. System.Drawing.Graphics gfx, fgfx; //Draws the drawings to the memory bitmap above. int b; // *** 1 *** , all other declarations go here. private void Form1_Load(object sender, EventArgs e) { backup = new Bitmap(640, 480); //640x480 is the size of the memory bitmap, which is effectively going to be the display size. gfx = Graphics.FromImage(backup); //Set gfx to draw to this backup bitmap. fgfx = this.CreateGraphics(); //Set fgfx to draw to the display (form). // *** 2 *** , all other instantiations go here. } void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { } void Form1_Click(object sender, System.EventArgs e) { b += 10; //b symbolizes a location of something on the form. gfx.FillRectangle(Brushes.Black, this.ClientRectangle); //Black background. gfx.FillRectangle(Brushes.DarkGoldenrod, new Rectangle(b, b, 100, 100)); // *** 3 *** , delete this routine, we will not be clicking a map. Present(); } void Present() { // *** 4 *** , typically nothing else will go in this subroutine. fgfx.DrawImage(backup, 0, 0); //Present memory bitmap to the display. } private void clock_Tick(object sender, EventArgs e) { // *** 5 *** , game processing and general logic will go here. // *** 6 *** , drawing the state of the game goes here. Present(); } |
' *** 1 *** , all other declarations go here. Dim Tileset As Bitmap 'Holds the tiles that we will draw. ' *** 2 *** , all other instantiations go here. Tileset = New Bitamp("tileset.bmp") 'Put the picture into our bitmap. ' *** 6 *** , drawing the state of the game goes here. GFX.DrawImage(Tileset, 0, 0, New Rectangle(0, 0, 16, 16), GraphicsUnit.Pixel) 'Draw single piece of grass tile. |
// *** 1 *** , all other declarations go here. Bitmap tileset; //Will hold our tileset picture. // *** 2 *** , all other instantiations go here. tileset = new Bitamp("tileset.bmp"); // Put picture into our bitmap. // *** 6 *** , drawing the state of the game goes here. gfx.DrawImage(tileset, 0, 0, new Rectangle(0, 0, 16, 16), GraphicsUnit.Pixel); //Draw a single tile to the display. |
' *** 6 *** , drawing the state of the game goes here. Dim LV As Integer For LV = 0 To 24 GFX.DrawImage(Tileset, 16 * LV, 0, New Rectangle(0, 0, 16, 16), GraphicsUnit.Pixel) 'Draw a grass tile. Next 'The for loop will end up drawing 25 tiles. |
// *** 6 *** , drawing the state of the game goes here. int lv; for (lv = 0; lv < 25; lv++) { gfx.DrawImage(tileset, lv * 16, 0, new Rectangle(0, 0, 16, 16), GraphicsUnit.Pixel); //Draw a single tile to the display. } |
' *** 6 *** , drawing the state of the game goes here. Dim LX, LY As Integer For LY = 0 To 15 For LX = 0 To 24 GFX.DrawImage(Tileset, 16 * LX, 16 * LY, New Rectangle(0, 0, 16, 16), GraphicsUnit.Pixel) 'Draw a grass tile. Next Next 'The for loop will end up drawing a grid of 400 tiles. |
// *** 6 *** , drawing the state of the game goes here. int lx,ly; for (ly = 0; ly < 16; ly++) { for (lx = 0; lx < 25; lx++) { gfx.DrawImage(tileset, lx * 16, ly * 16, new Rectangle(0, 0, 16, 16), GraphicsUnit.Pixel); //Draw a single tile to the display. } } |
' *** 6 *** If LX = 3 AndAlso LY = 4 Then GFX.DrawIamge(Tileset, 16 * LX, 16 * LY, New Rectangle(16, 0, 16, 16), GraphicsUnit.Pixel) 'Hardcoded desert tile at coord 3,4. Else GFX.DrawImage(Tileset, 16 * LX, 16 * LY, New Rectangle(0, 0, 16, 16), GraphicsUnit.Pixel) 'Draw a grass tile. End If |
// *** 6 *** if (lx == 3 && ly == 4) gfx.DrawIamge(tileset, lx * 16, ly * 16, new Rectangle(16, 0, 16, 16), GraphicsUnit.Pixel); //Hardcoded desert tile. else gfx.DrawImage(tileset, lx * 16, ly * 16, new Rectangle(0, 0, 16, 16), GraphicsUnit.Pixel); //Draw a single tile to the display. |
' *** 1 *** Dim Map(,) As Integer 'Holds map tile indices. ' *** 2 *** Map = New Integer(24, 15) {} Map(3, 4) = 1 Map(5, 7) = 1 Map(5, 11) = 2 Map(16, 4) = 2 Map(21, 3) = 3 Map(22, 8) = 4 'Put in some test tiles. |
// *** 1 *** int[,] map; //Holds map tile indices. // *** 2 *** map = new int[25, 16]; map[3, 4] = 1; map[5, 7] = 1; map[5, 11] = 2; map[16, 4] = 2; map[21, 3] = 3; map[22, 8] = 4; //Put in some values for our map here for now. |
' *** 6 *** Dim LX, LY As Integer Dim ThisIndex As Integer Dim Scx As Integer 'Source coordinate's X-location. For LY = 0 To 15 For LX = 0 To 24 ThisIndex = Map(LX, LY) 'Gets the tile index for this tile on the map. Sc = 16 * ThisIndex 'Returns the source coordinate's X-location for this tile index. GFX.DrawImage(Tileset, 16 * LX, 16 * LY, New Rectangle(Scx, 0, 16, 16), GraphicsUnit.Pixel) 'Draw a tile. Next Next 'The for loop will end up drawing a grid of 400 tiles. |
// *** 6 *** int lx,ly; int thisindex; int scx; //Source coordinate's x-location for (ly = 0; ly < 16; ly++) { for (lx = 0; lx < 25; lx++) { thisindex = map[lx, ly]; //The tile index for this tile on the map. sc = thisindex * 16; //Source coordinate X location for this tile index. gfx.DrawImage(tileset, lx * 16, ly * 16, new Rectangle(scx, 0, 16, 16), GraphicsUnit.Pixel); //Draw a single tile to the display. } } |
Private Sub SaveMap() Dim SW As StreamWriter, LX, LY As Integer SW = New StreamWriter("C:\testmap.map") For LY = 0 To 15 For LX = 0 To 24 SW.Write(Map(LX, LY).ToString()) 'Converts the tile index to a one-character string, and saves it to the file. Next Next SW.Close() 'Don't forget to close the streamwriter. End Sub Private Sub LoadMap() Dim SR As StreamReader, LX, LY As Integer, LineIndex As Integer, Line As String SR = New StreamReader("C:\testmap.map") Line = SR.ReadToEnd() SR.Close() For LY = 0 To 15 For LX = 0 To 24 LineIndex = LX + 25 * LY 'Gets the character index in the line that is supposed to correspond with these values for lx and ly. Map(LX, LY) = Integer.Parse(Line.Substring(LineIndex, 1)) Next Next End Sub |
private void savemap() { StreamWriter sw; int lx, ly; sw = new StreamWriter(@"C:\testmap.map"); for (ly = 0; ly < 16; ly++) for (lx = 0; lx < 25; lx++) sw.Write(map[lx, ly].ToString()); sw.Close(); } private void loadmap() { StreamReader sr; int lx, ly, lineindex; string line; sr = new StreamReader(@"C:\testmap.map"); line = sr.ReadToEnd(); sr.Close(); for (ly = 0; ly < 16; ly++) for (lx = 0; lx < 25; lx++) { lineindex = ly * 25 + lx; // The index in the line that would correspond to this map position. map[lx, ly] = int.Parse(line.Substring(lineindex, 1)); //Convert the string into a one-digit number and put it back into the map. } //Puts data into map. } |
Private Sub SaveMap() Dim SW As StreamWriter, LX, LY, Finalvalue As Integer SW = New StreamWriter("C:\testmap.map") For LY = 0 To 15 For LX = 0 To 24 Finalvalue = 32 + Map(LX, LY) SW.Write(ChrW(Finalvalue)) 'Converts the tile index to a one-character string, and saves it to the file. Next SW.WriteLine() 'New line in the file. Next SW.Close() 'Don't forget to close the streamwriter. End Sub Private Sub LoadMap() Dim SR As StreamReader, LX, LY As Integer, LineIndex As Integer, Line As String SR = New StreamReader("C:\testmap.map") For LY = 0 To 15 Line = SR.ReadLine() 'Read one line which corresponds to one row (all Y values same). For LX = 0 To 24 LineIndex = LX 'Gets the character index in the line that is supposed to correspond with these values for lx and ly. Map(LX, LY) = AscW(Line.Chars(LineIndex)) - 32 Next Next SR.Close() End Sub |
private void savemap() { StreamWriter sw; int lx, ly, finalvalue; sw = new StreamWriter(@"C:\testmap.map"); for (ly = 0; ly < 16; ly++) { for (lx = 0; lx < 25; lx++) { finalvalue = map[lx, ly] + 32; //Get a final integer value that will convert this tile index to a readable character. sw.Write((char)finalvalue); } sw.WriteLine(); //Write a new line into the file. } sw.Close(); } private void loadmap() { StreamReader sr; int lx, ly, lineindex; string line; sr = new StreamReader(@"C:\testmap.map"); for (ly = 0; ly < 16; ly++) { line = sr.ReadLine(); for (lx = 0; lx < 25; lx++) { lineindex = lx; // The index in the line that would correspond to this map position. map[lx, ly] = (int)line[lx] - 32; //Convert the string into a one-digit number and put it back into the map. } } //Puts data into map. sr.Close(); } |
Private Sub SaveMap() Dim BW As BinaryWriter, FS As FileStream, LX, LY As Integer FS = New FileStream("C:\testmap.map", FileMode.Create) BW = New BinaryWriter(FS) For LY = 0 To 15 For LX = 0 To 24 BW.Write(Map(LX, LY)) 'Converts the tile index to a one-character string, and saves it to the file. Next Next BW.Close() 'Don't forget to close the binarywriter. FS.Close() End Sub Private Sub LoadMap() Dim BR As BinaryReader, FS As FileStream, LX, LY As Integer FS = New FileStream("C:\testmap.map", FileMode.Open) BR = New BinaryReader(FS) For LY = 0 To 15 For LX = 0 To 24 Map(LX, LY) = BR.ReadInt32() Next Next BR.Close() 'Close our streams. FS.Close() End Sub |
private void savemap() { BinaryWriter bw; FileStream fs; int lx, ly; fs = new FileStream(@"C:\testmap.map", FileMode.Create); bw = new BinaryWriter(fs); for (ly = 0; ly < 16; ly++) for (lx = 0; lx < 25; lx++) bw.Write(map[lx,ly]); // Puts a 32-bit integer into the file. //Don't forget to close the streams. bw.Close(); fs.Close(); } private void loadmap() { BinaryReader br; FileStream fs; int lx, ly; fs = new FileStream (@"C:\testmap.map", FileMode.Open); br = new BinaryReader(fs); for (ly = 0; ly < 16; ly++) for (lx = 0; lx < 25; lx++) map[lx, ly] = br.ReadInt32(); //Gets a 32-bit integer from the file. //Don't forget to close the streams. br.Close(); fs.Close(); } |
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown Dim Mx, My As Integer Dim ThisIndex As Integer Mx = e.X \ 16 My = e.Y \ 16 'Map coordinates of the tile we have clicked. If Mx >= 0 AndAlso Mx < 24 AndAlso My >= 0 AndAlso My < 15 Then ThisIndex = Map(Mx, My) ThisIndex = (ThisIndex + 1) Mod 8 'Cycle this tile index to the next one, looping back at 8. Map(Mx, My) = ThisIndex 'Update the map tile index. End If End Sub |
void Form1_MouseDown(object sender, MouseEventArgs e) { int mx, my, thisindex; mx = e.X / 16; my = e.Y / 16; //Get map coordinates of where the user clicked. if (mx >= 0 && mx <= 24 && my >= 0 && my <= 15) { thisindex = map[mx, my]; thisindex = (thisindex + 1) % 8; //Get the tile index at this map coordinate and move it up, cycling back to 0 at 8. map[mx, my] = thisindex; //Reassign the tile index to the map array } } |
' *** 1 *** Dim Charpic As Bitmap 'Holds picture of main character. Dim Charloc As Point 'Holds character map coordinates. ' *** 2 *** Charpic = New Bitmap("Riff.bmp") 'Put character picture in. Charpic.MakeTransparent(Color.White) 'Make the color white see-through. Charloc = New Point(1, 1) 'Set map coordinates for player to 1, 1. ' *** 6 *** GFX.DrawImage(Tileset, 16 * LX, 16 * LY, New Rectangle(Scx, 0, 16, 16), GraphicsUnit.Pixel) 'Draw a tile. GFX.DrawImage(Charpic, 16 * Charloc.X, 16 * Charloc.Y) 'Draw character on the map so that he appears at this map coordinate. |
// *** 1 *** Bitmap charpic; //Holds the character picture. Point charloc; //Holds character location. // *** 2 *** charpic = new Bitmap("Riff.bmp"); charpic.MakeTransparent(Color.White); //Make white to be transparent. charloc = new Point(1, 1); //Put character at map coordinate 1,1. // *** 6 *** gfx.DrawImage(tileset, lx * 16, ly * 16, new Rectangle(scx, 0, 16, 16), GraphicsUnit.Pixel); //Draw a single tile to the display. gfx.DrawImage(charpic, charloc.X * 16, charloc.Y * 16); //Draw the character so that it appears at this map coordinate. |
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown 'Check keycode against left, right, up, and down. If e.KeyCode = Keys.Left Then GoLeft = True ElseIf e.KeyCode = Keys.Right Then GoRight = True ElseIf e.KeyCode = Keys.Up Then GoUp = True ElseIf e.KeyCode = Keys.Down Then GoDown = True End If End Sub Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp 'Check keycode against left, right, up, and down. If e.KeyCode = Keys.Left Then GoLeft = False ElseIf e.KeyCode = Keys.Right Then GoRight = False ElseIf e.KeyCode = Keys.Up Then GoUp = False ElseIf e.KeyCode = Keys.Down Then GoDown = False End If End Sub ''' <summary> ''' Changes the character's location based on which keys are pressed. ''' </summary> ''' <remarks>None.</remarks> Private Sub Movement() If GoLeft Then Charloc.Offset(-1, 0) 'Move left. End If If GoRight Then Charloc.Offset(1, 0) 'Move right... etc. End If If GoUp Then Charloc.Offset(0, -1) End If If GoDown Then Charloc.Offset(0, 1) End If End Sub Dim GoLeft, GoUp, GoRight, GoDown As Boolean ' *** 5 *** Movement() ' Changes the character's location based on which keys are pressed. |
void Form1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Left) goleft = false; else if (e.KeyCode == Keys.Right) goright = false; else if (e.KeyCode == Keys.Up) goup = false; else if (e.KeyCode == Keys.Down) godown = false; // Check keycode against left, right, up, and down. } void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Left) goleft = true; else if (e.KeyCode == Keys.Right) goright = true; else if (e.KeyCode == Keys.Up) goup = true; else if (e.KeyCode == Keys.Down) godown = true; // Check keycode against left, right, up, and down. } /// <summary> /// Changes character location based on which keys are pressed. /// </summary> private void movement() { if (goleft) charloc.Offset(-1, 0); if (goright) charloc.Offset(1, 0); if (goup) charloc.Offset(0, -1); if (godown) charloc.Offset(0, 1); } // *** 5 *** movement();// Changes character location based on which keys are pressed. |
' *** 1 *** Dim Blocked() As Integer 'Holds tile indices that cannot be walked on. ' *** 2 *** Blocked = New Integer() {3, 6} 'These are tile indices that the player cannot walk on. ' *** 5 *** Dim Collided As Boolean If Not (Charloc.X >= 0 AndAlso Charloc.X <= 24 AndAlso Charloc.Y >= 0 AndAlso Charloc.Y <= 15) Then Collided = True ElseIf Array.BinarySearch(Blocked, Map(Charloc.X, Charloc.Y)) >= 0 Then Collided = True End If If Collided Then 'Move back to where you came from. If GoLeft Then Charloc.Offset(1, 0) End If If GoRight Then Charloc.Offset(-1, 0) End If If GoUp Then Charloc.Offset(0, 1) End If If GoDown Then Charloc.Offset(0, -1) End If End If |
// *** 1 *** int[] blocked; //List of tiles that cannot be walked on. // *** 2 *** blocked = new int[] { 3, 6 }; // *** 5 *** bool collided = false; if (!(charloc.X >= 0 && charloc.X <= 24 && charloc.Y >= 0 && charloc.Y <= 15)) collided = true; else if (Array.BinarySearch(blocked, map[charloc.X, charloc.Y]) >= 0) collided = true; if (collided) { if (goleft) charloc.Offset(1, 0); if (goright) charloc.Offset(-1, 0); if (goup) charloc.Offset(0, 1); if (godown) charloc.Offset(0, -1); } |
''' <summary> ''' Changes the character's location based on which keys are pressed. ''' </summary> ''' <remarks>None.</remarks> Private Sub Movement() Dim Collided As Boolean = False If GoLeft Then Charloc.Offset(-1, 0) 'Move left. End If If GoRight Then Charloc.Offset(1, 0) 'Move right... etc. End If 'Do horizontal movements first. 'Then check for collision in the horizontal. If Not (Charloc.X >= 0 AndAlso Charloc.X <= 24) Then Collided = True ElseIf Array.BinarySearch(Blocked, Map(Charloc.X, Charloc.Y)) >= 0 Then Collided = True End If 'A collision occured while moving horizontally. If Collided Then 'Move back to where you came from. If GoLeft Then Charloc.Offset(1, 0) End If If GoRight Then Charloc.Offset(-1, 0) End If Collided = False End If 'Followed by vertical movements. If GoUp Then Charloc.Offset(0, -1) End If If GoDown Then Charloc.Offset(0, 1) End If 'Then check for collision in the vertical. If Not (Charloc.Y >= 0 AndAlso Charloc.Y <= 15) Then Collided = True ElseIf Array.BinarySearch(Blocked, Map(Charloc.X, Charloc.Y)) >= 0 Then Collided = True End If 'Collision occured while moving vertically. If Collided Then 'Move back to where you came from. If GoUp Then Charloc.Offset(0, 1) End If If GoDown Then Charloc.Offset(0, -1) End If End If End Sub |
/// <summary> /// Changes character location based on which keys are pressed. /// </summary> private void movement() { bool collided = false; if (goleft) charloc.Offset(-1, 0); if (goright) charloc.Offset(1, 0); //Move player to tile in this direction. if (!(charloc.X >= 0 && charloc.X <= 24 )) collided = true; else if (Array.BinarySearch(blocked, map[charloc.X, charloc.Y]) >= 0) collided = true; if (collided) { if (goleft) charloc.Offset(1, 0); if (goright) charloc.Offset(-1, 0); collided = false; } if (goup) charloc.Offset(0, -1); if (godown) charloc.Offset(0, 1); //Move player to tile in this direction. if (!(charloc.Y >= 0 && charloc.Y <= 15)) collided = true; else if (Array.BinarySearch(blocked, map[charloc.X, charloc.Y]) >= 0) collided = true; if (collided) { if (goup) charloc.Offset(0, 1); if (godown) charloc.Offset(0, -1); } } |
' *** 1 *** Dim Frames() As Bitmap 'Holds the separate animation frames. ' *** 2 *** Frames = New Bitmap() {New Bitmap("Solagon.gif"), New Bitmap("Solagon2.gif"), New Bitmap("Solagon3.gif"), New Bitmap("Solagon4.gif")} 'Put in each picture into our array... four elements from 0 to 3. Frames(0).MakeTransparent(Color.White) Frames(1).MakeTransparent(Color.White) Frames(2).MakeTransparent(Color.White) Frames(3).MakeTransparent(Color.White) 'Make all white to be transparent. |
// *** 1 *** Bitmap[] frames; // Holds the frames of animation that we will use. // *** 2 *** frames = new Bitmap[] { new Bitmap("Solagon.gif"), new Bitmap("Solagon2.gif"), new Bitmap("Solagon3.gif"), new Bitmap("Solagon4.gif") }; //Put frames into our array of animation frames. frames[0].MakeTransparent(Color.White); frames[1].MakeTransparent(Color.White); frames[2].MakeTransparent(Color.White); frames[3].MakeTransparent(Color.White); //Make all white to be transparent. |
' *** 1 *** Dim Cyc As Integer 'Frame index cycler. ' *** 5 *** Cyc = (Cyc + 1) Mod 4 'Cycles: 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, ... ' *** 6 *** GFX.DrawImage(Frames(Cyc), 16 * Charloc.X, 16 * Charloc.Y) 'Draw animated character on the map so that he appears at this map coordinate. |
// *** 1 *** int cyc; //Cycles through frame indices for animation. // *** 5 *** cyc = (cyc + 1) % 4; //Cycles through 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, ... // *** 6 *** gfx.DrawImage(frames[cyc], charloc.X * 16, charloc.Y * 16); //Draw the animated character so that it appears at this map coordinate. |
Const MAPWIDTH As Integer = 25 'Number of columns in the map. Const MAPHEIGHT As Integer = 16 'Number of rows in the map. Const MAPWIDTHEX As Integer = MAPWIDTH - 1 'X - Index of the last column Const MAPHEIGHTEX As Integer = MAPHEIGHT - 1 'Y - Index of the last row Const TILESIZE As Integer = 16 'Width or height of each tile in pixels. |
const int themapwidth = 25; //Number of columns in the map. const int themapheight = 16; //Number of rows in the map. const int thempawidthex = themapwidth - 1; //Index of the last column in the map. const int themapheightex = themapheight - 1; //Index of the last row in the map. const int thetilesize = 16; //Width or height of tile in pixels. |
Dim Camera As Point 'Has map coordinate that will appear in the upper-left of the scrolling display. Const DISPLAYEXTRAX As Integer = 3 'Number of tiles that will appear between the player and the left (right) edge of the display. Const DISPLAYEXTRAY As Integer = 3 'Number of tiles that will appear between the player and the top (bottom) edge of the display. Const DISPLAYWIDTH As Integer = DISPLAYEXTRAX * 2 + 1 'Number of columns in the display. Const DISPLAYHEIGHT As Integer = DISPLAYEXTRAY * 2 + 1 'Number of rows in the display. |
Point camera; //Holds map coordinate that will appear in the upper-left corner of the scrolling display. const int thedisplayextrax = 3; //Number of tiles shown between player and left/right edge. const int thedisplayextray = 3; //Number of tiles shown between player and top/bottom edge. const int thedisplaywidth = 2 * thedisplayextrax + 1; //Number of columns in the scrolling display. const int thedisplayheight = 2 * thedisplayextray + 1; //Number of rows in the scrolling display. |
Camera.X = Charloc.X - DISPLAYEXTRAX Camera.Y = Charloc.Y - DISPLAYEXTRAY 'Set camera coordinates based on the character's location. |
camera.X = charloc.X - thedisplayextrax; camera.Y = charloc.Y - thedisplayextray; //Update camera location based on where the player is. |
If Charloc.X - DISPLAYEXTRAX < 0 Then Camera.X = 0 'Player too far to the left. ElseIf Charloc.X - DISPLAYEXTRAX > MAPWIDTH - DISPLAYWIDTH Then Camera.X = MAPWIDTH - DISPLAYWIDTH 'Player too far to the right. Else Camera.X = Charloc.X - DISPLAYEXTRAX End If If Charloc.Y - DISPLAYEXTRAY < 0 Then Camera.Y = 0 'Player too far up. ElseIf Charloc.Y - DISPLAYEXTRAY > MAPHEIGHT - DISPLAYHEIGHT Then Camera.Y = MAPHEIGHT - DISPLAYHEIGHT 'Player too far down. Else Camera.Y = Charloc.Y - DISPLAYEXTRAY End If 'Set camera coordinates based on the character's location. |
if (charloc.X - thedisplayextrax < 0) camera.X = 0; //Player too far to the left. else if (charloc.X - thedisplayextrax > themapwidth - thedisplaywidth) camera.X = themapwidth - thedisplaywidth; //Player too far right. else camera.X = charloc.X - thedisplayextrax; if (charloc.Y - thedisplayextray < 0) camera.Y = 0; //Player too far up. else if (charloc.Y - thedisplayextray > themapheight - thedisplayheight) camera.Y = themapheight - thedisplayheight; //Player too far down. else camera.Y = charloc.Y - thedisplayextray; //Update camera location based on where the player is. |
Const DISPLAYWIDTHEX As Integer = DISPLAYEXTRAX * 2 'Number of visible scrolling columns that the player is not standing on. Const DISPLAYHEIGHTEX As Integer = DISPLAYEXTRAY * 2 'Number of visible scrolling rows that the player is not standing on. For LY = 0 To DISPLAYHEIGHTEX For LX = 0 To DISPLAYWIDTHEX ThisIndex = Map(LX + Camera.X, LY + Camera.Y) 'Gets the tile index for this tile on the map. Scx = TILESIZE * ThisIndex 'Returns the source coordinate's X-location for this tile index. GFX.DrawImage(Tileset, TILESIZE * LX, TILESIZE * LY, New Rectangle(Scx, 0, TILESIZE, TILESIZE), GraphicsUnit.Pixel) 'Draw a tile. GFX.DrawImage(Frames(Cyc), TILESIZE * (Charloc.X - Camera.X), TILESIZE * (Charloc.Y - Camera.Y)) 'Draw animated character on the scrolling map so that he is always visible. Next Next 'The for loop will end up drawing a grid of 400 tiles. |
const int thedisplaywidthex = 2 * thedisplayextrax; //Number of columns in scrolling display that the player is not on. const int thedisplayheightex = 2 * thedisplayextray; //Number of rows in scrolling display that the player is not on. for (ly = 0; ly < thedisplayheight ; ly++) { for (lx = 0; lx < thedisplaywidth ; lx++) { thisindex = map[lx + camera.X , ly + camera.Y ]; //The tile index for this tile on the map. scx = thisindex * thetilesize ; //Source coordinate X location for this tile index. gfx.DrawImage(tileset, lx * thetilesize, ly * thetilesize, new Rectangle(scx, 0, thetilesize,thetilesize), GraphicsUnit.Pixel); //Draw a single tile to the display. gfx.DrawImage(frames[cyc], (charloc.X - camera.X) * thetilesize, (charloc.Y - camera.Y) * thetilesize); //Draw the animated character so that it appears at this map coordinate. } } |
Const HEXAGONWIDTH As Integer = 32 'Width of tile in pixels Const HEXAGONHEIGHT As Integer = 32 'Height of tile in pixels. Const HEXAGONEDGESWIDTH As Integer = HEXAGONWIDTH \ 4 'Width of a hexagon's edge Const HEXAGONEDGESHEIGHT As Integer = HEXAGONHEIGHT \ 2 'Height of a hexagon's edge Const HEXAGONTOPENDWIDTH As Integer = HEXAGONWIDTH \ 2 'Width of flat top part of hexagon. |
const int thehexagonwidth = 32; //Width of hexagon. const int thehexagonheight = 32; //Height of hexagon. const int thehexagonedgewidth = thehexagonwidth / 4; //Width of the edge part of the hexagon. const int thehexagonedgeheight = thehexagonheight / 2; //Height of the edge part of the hexagon. const int thehexagontopendwidth = thehexagonwidth / 2; //Width of the top flat part of the hexagon. |
Const ZEROPOINTX As Integer = MAPHEIGHTEX * (HEXAGONEDGESWIDTH + HEXAGONTOPENDWIDTH) 'Distance 0,0 tile is from the left end of the display. Const ZEROPOINTY As Integer = 0 |
const int thezeropointx = themapheightex * (thehexagontopendwidth + thehexagonedgewidth); //X display coordinate of the 0,0 map coordinate tile. const int thezeropointy = 0; //Y display coordinate of the 0,0 map coordinate tile. |
Dim Tileset As Bitmap 'Holds the tiles that we will draw. Tileset = New Bitmap("hextiles.bmp") 'Put the picture into our bitmap. Tileset.MakeTransparent(Color.White) 'Make the white part surround the hexagons to be transparent. |
Bitmap tileset; //Will hold our tileset picture. tileset = new Bitmap("hextiles.bmp"); // Put picture into our bitmap. tileset.MakeTransparent(Color.White); //Make white areas around hexagon transparent. |
Dim Xfx, Xfy As Integer 'Transformed map coordinates to display coordinate. GFX.FillRectangle(Brushes.Black, Me.ClientRectangle) 'Fill background with black to hide things not drawn due to transparency. For LY = 0 To MAPHEIGHTEX For LX = 0 To MAPWIDTHEX ThisIndex = Map(LX, LY) 'Gets the tile index for this tile on the map. Scx = HEXAGONWIDTH * ThisIndex 'Returns the source coordinate's X-location for this tile index. Xfx = (HEXAGONEDGESWIDTH + HEXAGONTOPENDWIDTH) * (LX - LY) + ZEROPOINTX Xfy = HEXAGONEDGESHEIGHT * (LX LY) 'Transform map coordinates to display coordinates. GFX.DrawImage(Tileset, Xfy, Xfy, New Rectangle(Scx, 0, HEXAGONWIDTH, HEXAGONHEIGHT), GraphicsUnit.Pixel) 'Draw a tile. Next Next 'The for loop will end up drawing a grid of 400 tiles. |
int xfx, xfy; //Display coordinates for a particular map coordinate. gfx.FillRectangle(Brushes.Black, this.ClientRectangle); //Fill with black to erase anything not overdrawn due to transparency. for (ly = 0; ly < themapheight ; ly++) { for (lx = 0; lx < themapwidth ; lx++) { thisindex = map[lx, ly ]; //The tile index for this tile on the map. scx = thisindex * thehexagonwidth ; //Source coordinate X location for this tile index. xfx = (thehexagonedgewidth + thehexagontopendwidth) * (lx ly) + thezeropointx; xfy = thehexagonedgeheight * (lx + ly); // Transforms the map coordinates to display coordinates for the tile. gfx.DrawImage(tileset, xfx, xfy, new Rectangle(scx, 0, thehexagonwidth, thehexagonheight), GraphicsUnit.Pixel); //Draw a single tile to the display. } } |
Const CHARACTEROFFDISTANCE As Integer = HEXAGONHEIGHT \ 4 'Distance character is from corner of hexagon's square Xfx = (HEXAGONEDGESWIDTH + HEXAGONTOPENDWIDTH) * (Charloc.X - Charloc.Y) + ZEROPOINTX + CHARACTEROFFDISTANCE Xfy = HEXAGONEDGESHEIGHT * (Charloc.X + Charloc.Y) + CHARACTEROFFDISTANCE 'Transform map coordinates to display coordinates. GFX.DrawImage(Frames(Cyc), Xfx, Xfy) 'Draw animated character on the scrolling map so that he is always visible. |
const int thecharacteroffdistance = thehexagonheight / 4; //Distance character is from top-left of hexagon. xfx = (thehexagonedgewidth + thehexagontopendwidth) * (charloc.X - charloc.Y) + thezeropointx + thecharacteroffdistance; xfy = thehexagonedgeheight * (charloc.X + charloc.Y) + thecharacteroffdistance; // Transforms the map coordinates to display coordinates for the tile. gfx.DrawImage(frames[cyc], xfx, xfy); //Draw the animated character so that it appears at this map coordinate. |
Dim GoUpLeft, GoUp, GoUpRight, GoDownLeft, GoDown, GoDownRight As Boolean Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown 'Check keycode against left, right, up, and down. If e.KeyCode = Keys.NumPad7 Then GoUpLeft = True ElseIf e.KeyCode = Keys.NumPad9 Then GoUpRight = True ElseIf e.KeyCode = Keys.NumPad8 Then GoUp = True ElseIf e.KeyCode = Keys.NumPad2 Then GoDown = True ElseIf e.KeyCode = Keys.NumPad1 Then GoDownLeft = True ElseIf e.KeyCode = Keys.NumPad3 Then GoDownRight = True End If End Sub Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp 'Check keycode against left, right, up, and down. If e.KeyCode = Keys.NumPad7 Then GoUpLeft = False ElseIf e.KeyCode = Keys.NumPad9 Then GoUpRight = False ElseIf e.KeyCode = Keys.NumPad8 Then GoUp = False ElseIf e.KeyCode = Keys.NumPad2 Then GoDown = False ElseIf e.KeyCode = Keys.NumPad1 Then GoDownLeft = False ElseIf e.KeyCode = Keys.NumPad3 Then GoDownRight = False End If End Sub If GoUpLeft Then Charloc.Offset(-1, 0) 'Move left. End If If GoDownRight Then Charloc.Offset(1, 0) 'Move right... etc. End If 'Do horizontal movements first. 'Then check for collision in the horizontal. If Not (Charloc.X >= 0 AndAlso Charloc.X <= MAPWIDTHEX) Then Collided = True ElseIf Array.BinarySearch(Blocked, Map(Charloc.X, Charloc.Y)) >= 0 Then Collided = True End If 'A collision occured while moving horizontally. If Collided Then 'Move back to where you came from. If GoUpLeft Then Charloc.Offset(1, 0) End If If GoDownRight Then Charloc.Offset(-1, 0) End If Collided = False End If 'Followed by vertical movements. If GoUpRight Then Charloc.Offset(0, -1) End If If GoDownLeft Then Charloc.Offset(0, 1) End If 'Then check for collision in the vertical. If Not (Charloc.Y >= 0 AndAlso Charloc.Y <= MAPHEIGHTEX) Then Collided = True ElseIf Array.BinarySearch(Blocked, Map(Charloc.X, Charloc.Y)) >= 0 Then Collided = True End If 'Collision occured while moving vertically. If Collided Then 'Move back to where you came from. If GoUpRight Then Charloc.Offset(0, 1) End If If GoDownLeft Then Charloc.Offset(0, -1) End If End If 'And then diagonal movements. If GoUp Then Charloc.Offset(-1, -1) End If If GoDown Then Charloc.Offset(1, 1) End If 'Then check for collision in the vertical. If Not (Charloc.X >= 0 AndAlso Charloc.X <= MAPWIDTHEX AndAlso Charloc.Y >= 0 AndAlso Charloc.Y <= MAPHEIGHTEX) Then Collided = True ElseIf Array.BinarySearch(Blocked, Map(Charloc.X, Charloc.Y)) >= 0 Then Collided = True End If 'Collision occured while moving vertically. If Collided Then 'Move back to where you came from. If GoUp Then Charloc.Offset(1, 1) End If If GoDown Then Charloc.Offset(-1, -1) End If End If |
bool goupleft, goupright, godownleft, godownright, goup, godown; //Indicates keystates for these keys. void Form1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.NumPad7) goupleft = false; else if (e.KeyCode == Keys.NumPad9) goupright = false; else if (e.KeyCode == Keys.NumPad8 ) goup = false; else if (e.KeyCode == Keys.NumPad2 ) godown = false; else if (e.KeyCode == Keys.NumPad1) godownleft = false ; else if (e.KeyCode == Keys.NumPad3) godownright = false ; } void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.NumPad7) goupleft = true; else if (e.KeyCode == Keys.NumPad9) goupright = true; else if (e.KeyCode == Keys.NumPad8) goup = true; else if (e.KeyCode == Keys.NumPad2) godown = true; else if (e.KeyCode == Keys.NumPad1) godownleft = true; else if (e.KeyCode == Keys.NumPad3) godownright = true; } if (goupleft) charloc.Offset(-1, 0); if (godownright) charloc.Offset(1, 0); //Move player to tile in this direction. if (!(charloc.X >= 0 && charloc.X <= thempawidthex )) collided = true; else if (Array.BinarySearch(blocked, map[charloc.X, charloc.Y]) >= 0) collided = true; if (collided) { if (goupleft) charloc.Offset(1, 0); if (godownright) charloc.Offset(-1, 0); collided = false; } if (goup) charloc.Offset(-1, -1); if (godown) charloc.Offset(1, 1); //Move player to tile in this direction. if (!(charloc.X >= 0 && charloc.X <= thempawidthex && charloc.Y >= 0 && charloc.Y <= themapheightex)) collided = true; else if (Array.BinarySearch(blocked, map[charloc.X, charloc.Y]) >= 0) collided = true; if (collided) { if (goup) charloc.Offset(1, 1); if (godown) charloc.Offset(-1, -1); collided = false; } if (goupright) charloc.Offset(0, -1); if (godownleft) charloc.Offset(0, 1); //Move player to tile in this direction. if (!(charloc.Y >= 0 && charloc.Y <= themapheightex)) collided = true; else if (Array.BinarySearch(blocked, map[charloc.X, charloc.Y]) >= 0) collided = true; if (collided) { if (goupright) charloc.Offset(0, 1); if (godownleft) charloc.Offset(0, -1); } |