1. Trang chủ
  2. » Công Nghệ Thông Tin

Visual C# Game Programming for Teens phần 6 pps

47 258 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 47
Dung lượng 722,79 KB

Nội dung

portal. In our program, the Space key is the trigger. When the portal is engaged, the player is teleported to the target coordinate (101,16), as shown in Figure 9.5. Trick Getting tired of the same old ground tiles in every example? Replace them! You are encouraged to use a different set of ground tiles or add new ones to this collection. I am only using these same tiles for consistency. You may replace the tiles in the level editor and in the game. The only requirement is that your tile palette image be oriented like the one presented in the book and that the tiles remain at 32x32 pixels in size. Otherwise, some coding changes will be needed. Looking for Tile Collisions The Portal demo program also looks for the Collidable property in tiles and reports on the screen when a collidable tile is identified. Figure 9.6 shows the message that is printed when the player walks over a collidable tile. Although the Figure 9.5 The portal has sent the player across the map! It’s a Data-Driven Game World 217 sprite doesn’t respond to collidable tiles yet in this example, we can use this information to enable collision response in the next major revision to the game. Hint This quick example is not quite polished yet, so expect to see some jittery sprites and timing problems. The point is to get these features to work first, and then make them work great afterward! Secret Doors With the portal system working, we can now use it to create secret doors to appear to walk through walls! A portal is just a tile property that defines a target location for the player to be moved to. If you set the target for a portal to a tile just one or two spaces away, then it can appear as if the player is hopping over Figure 9.6 Detecting collidable tiles. 218 Chapter 9 n Going Deeper into the Dungeon with Portals an obstacle. You could use this technique to get past solid objects or walls, which would be even more effective if a trigger object or key is required. Portal Demo Program Here is the source code for the Portal demo program. using System; using System.Collections.Generic; using System.Threading; using System.Data; using System.Drawing; using System.Windows.Forms; using RPG; namespace Portal_Project { public partial class Form1 : Form { public struct keyStates { public bool up, down, left, right; } Game game; Level level; keyStates keyState; bool gameover = false; Sprite hero; int heroDir = 0; bool portalFlag = false; Point portalTarget; int drawLast = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { It’s a Data-Driven Game World 219 this.Text = "Portal Demo"; //create game object Form form = (Form)this; game = new Game(ref form, 800, 600); //create tilemap level = new Level(ref game, 25, 19, 32); level.loadTilemap("portals.level"); level.loadPalette("palette.bmp", 5); //load hero hero = new Sprite(ref game); hero.Image = game.LoadBitmap("hero_sword_walk.png"); hero.Columns = 9; hero.TotalFrames =9*8; hero.Size = new Size(96, 96); hero.Position = new Point(400 - 48, 300 - 48); hero.AnimateWrapMode = Sprite.AnimateWrap.WRAP; hero.AnimationRate = 20; while (!gameover) { doUpdate(); } Application.Exit(); } private void Form1_KeyUp(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Escape : gameover = true; break; case Keys.Up: case Keys.W : keyState.up = false; break; case Keys.Down: case Keys.S : keyState.down = false; break; case Keys.Left: case Keys.A : keyState.left = false; break; case Keys.Right: 220 Chapter 9 n Going Deeper into the Dungeon with Portals case Keys.D : keyState.right = false; break; case Keys.Space: if (portalFlag) level.GridPos = portalTarget; break; } } private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Up: case Keys.W: keyState.up = true; break; case Keys.Down: case Keys.S: keyState.down = true; break; case Keys.Left: case Keys.A: keyState.left = true; break; case Keys.Right: case Keys.D: keyState.right = true; break; } } private void doUpdate() { //move the tilemap scroll position int steps = 8; PointF pos = level.ScrollPos; //up key movement if (keyState.up) { if (hero.Y > 300 - 48) hero.Y -= steps; else { pos.Y -= steps; if (pos.Y <= 0) hero.Y -= steps; } } //down key movement It’s a Data-Driven Game World 221 else if (keyState.down) { if (hero.Y < 300 - 48) hero.Y += steps; else { pos.Y += steps; if (pos.Y >= (127 - 19) * 32) hero.Y += steps; } } //left key movement if (keyState.left) { if (hero.X > 400 - 48) hero.X -= steps; else { pos.X -= steps; if (pos.X <= 0) hero.X -= steps; } } //right key movement else if (keyState.right) { if (hero.X < 400 - 48) hero.X += steps; else { pos.X += steps; if (pos.X >= (127 - 25) * 32) hero.X += steps; } } //update scroller position level.ScrollPos = pos; level.Update(); //limit player sprite to the screen boundary if (hero.X < -32) hero.X = -32; else if (hero.X > 800 - 65) hero.X = 800 - 65; if (hero.Y < -48) hero.Y = -48; 222 Chapter 9 n Going Deeper into the Dungeon with Portals else if (hero.Y > 600 - 81) hero.Y = 600 - 81; //orient the player in the right direction if (keyState.up && keyState.right) heroDir = 1; else if (keyState.right && keyState.down) heroDir = 3; else if (keyState.down && keyState.left) heroDir = 5; else if (keyState.left && keyState.up) heroDir = 7; else if (keyState.up) heroDir = 0; else if (keyState.right) heroDir = 2; else if (keyState.down) heroDir = 4; else if (keyState.left) heroDir = 6; else heroDir = -1; //get the untimed core frame rate int frameRate = game.FrameRate(); //drawing code should be limited to 60 fps int ticks = Environment.TickCount; if (ticks > drawLast + 16) { drawLast = ticks; //draw the tilemap level.Draw(0, 0, 800, 600); //draw the hero int startFrame = heroDir * 9; int endFrame = startFrame + 8; if (heroDir > -1) hero.Animate(startFrame, endFrame); hero.Draw(); //print da stats game.Print(700, 0, frameRate.ToString()); int y = 0; game.Print(0, y, "Scroll " + level.ScrollPos.ToString()); y += 20; game.Print(0, y, "Player " + hero.Position.ToString()); y += 20; Point feet = HeroFeet(); It’s a Data-Driven Game World 223 int tilex = (int)(level.ScrollPos.X + feet.X) / 32; int tiley = (int)(level.ScrollPos.Y + feet.Y) / 32; Level.tilemapStruct ts = level.getTile(tilex, tiley); game.Print(0, y, "Tile " + tilex.ToString() + "," + tiley.ToString() +"="+ts.tilenum.ToString()); y += 20; if (ts.collidable) { game.Print(0, y, "Collidable"); y += 20; } if (ts.portal) { game.Print(0, y, "Portal to " + ts.portalx.ToString() + "," + ts.portaly.ToString()); portalFlag = true; portalTarget = new Point(ts.portalx - feet.X / 32, ts.portaly - feet.Y / 32); y += 20; } else portalFlag = false; //highlight collision areas around player game.Device.DrawRectangle(Pens.Blue, hero.Bounds); game.Device.DrawRectangle(Pens.Red, feet.X + 16 - 1, feet.Y + 16 - 1, 2, 2); game.Device.DrawRectangle(Pens.Red, feet.X, feet.Y, 32, 32); //refresh window game.Update(); Application.DoEvents(); } else { //throttle the cpu Thread.Sleep(1); } } 224 Chapter 9 n Going Deeper into the Dungeon with Portals //return bottom center position of hero sprite //where feet are touching ground private Point HeroFeet() { return new Point((int)(hero.X + 32), (int)(hero.Y + 32 + 16)); } } } Level Up! This chapter saw some dramatic improvements to both the Level class and the Dungeon Crawler game engine code, with the addition of code to detect collidable tiles, and code to make portals active, allowing us to teleport the player to a new location. Although the level editor provides the “portalfile” field to enable teleporting to a position in a different level file, we will reserve that feature for later. Believe it or not, we now have a game world that is suitable as an environment for the Dungeon Crawler game! That means we can shift focus from the game world and level editing over to a new subject—people and monsters! Level Up! 225 This page intentionally left blank [...]... Intellect Charisma Hit Points 12D6 12D6 12D6 12D6 0 12D8 +20 +18 + 16 0 0 +STA Zombie Zombies are the mainstay of the undead horde you will find in the depths of the dungeon A combination of lost adventurers and foolhardy peasants who were Table 10.9 Level 16 Zombie Attribute Roll Modifiers Strength Dexterity Stamina Intellect Charisma Hit Points 16D6 16D6 16D6 16D6 0 16D8 +22 +12 +28 0 0 +STA The Character... Warrior Attributes Attribute Roll Strength Dexterity Stamina Intellect Charisma Hit Points 2D6 2D6 2D6 2D6 2D6 1D8 Modifiers (+15) +8 +3 +4 0 0 +STA Table 10.2 Paladin Attributes Attribute Roll Strength Dexterity Stamina Intellect Charisma Hit Points 2D6 2D6 2D6 2D6 2D6 1D8 Modifiers (+15) +3 +3 +8 0 +1 +STA 235 2 36 Chapter 10 n Creating Characters and Monsters Paladin Class The paladin is a balanced melee... almost everywhere in the game world At level 4, these guys are pretty tough for a new player but are soon dealt with handily once the player goes up a few levels The skeleton warrior has high strength and stamina, and a lot of hit points! Table 10 .6 Level 4 Skeleton Warrior Attribute Roll Modifiers Strength Dexterity Stamina Intellect Charisma Hit Points 4D6 4D6 4D6 4D6 0 4D8 +10 +6 +8 0 0 +STA Skeleton... Modifiers (+15) 2D6 2D6 2D6 2D6 2D6 1D8 0 +6 +1 +8 0 +STA Peasants as NPCs In addition to these player character classes, you might want to create base classes for some of the regular people in the world, like townsfolk, peasants, farmers, and so on These non-combat NPCs might all just share the same character class (with weak combat skills, poor experience, and so on) We will need NPCs like this for the quest... tools programming just because of the language! The character editor was developed over a period of several weeks and has reached a level of refinement that works exceptionally well with the codebase for our dungeon crawler engine I considered porting it to C# just for this chapter but I would rather spend that time working on the game instead of re-inventing the editor just for the sake of the programming. .. that for the unusual monsters that are rarely encountered or the game will be way too hard to play You will generally want to have at least one type of bad guy for each type of character class available to the player, and duplicate that 237 238 Chapter 10 n Creating Characters and Monsters Table 10.5 Peasant Attribute Roll Modifiers Strength Dexterity Stamina Intellect Charisma Hit Points 1D6 1D6 1D6... third part of the book focuses on the gameplay perspective—creating the various parts of our Dungeon Crawler game that bring it to life Up to this point, we have been so focused on just getting something up on the screen and working as it should, we haven’t had much time to explore gameplay Now we have a game world and all of the engine-level features we need for the game This part starts with a chapter... the character editor) If you want to create a level 10 monster, then I recommend rolling 10D6 for its attributes If desired level is L, then each attribute roll is LD6 The modifiers may then be used to adjust the dice rolls to ensure minimum or maximum values are reached for the monster’s intended abilities For instance, if you want to create a zombie with a minimum of 20 STR while still using the... arrows for their favorite bow Hunters have a high DEX to improve ranged chance to hit, with less use for traits like STR and INT Abilities revolve around ranged attack modifiers that improve accuracy (chance to hit) A good example from which to draw inspiration is the Tolkien character Legolas Table 10.3 Hunter Attributes Attribute Roll Strength Dexterity Stamina Intellect Charisma Hit Points 2D6 2D6 2D6... sure to close in fast and take them out before they get too many shots off Table 10.7 Level 8 Skeleton Archer Attribute Roll Strength Dexterity Stamina Intellect Charisma Hit Points 8D6 8D6 8D6 8D6 0 8D8 Modifiers +14 +20 + 16 0 0 +STA 239 240 Chapter 10 n Creating Characters and Monsters Berserker Berserkers are lost explorers who have gone insane while trying to find their way out of the dungeon, and . (+15) Strength 2D6 +8 Dexterity 2D6 +3 Stamina 2D6 +4 Intellect 2D6 0 Charisma 2D6 0 Hit Points 1D8 +STA Table 10.2 Paladin Attributes Attribute Roll Modifiers (+15) Strength 2D6 +3 Dexterity 2D6 +3 Stamina. "Portal Demo"; //create game object Form form = (Form)this; game = new Game( ref form, 800, 60 0); //create tilemap level = new Level(ref game, 25, 19, 32); level.loadTilemap("portals.level"); level.loadPalette("palette.bmp",. 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { It’s a Data-Driven Game World 219 this.Text = "Portal Demo"; //create game object Form form

Ngày đăng: 14/08/2014, 01:20

TỪ KHÓA LIÊN QUAN