Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Graphic Programming Advanced Computer Programming.

Similar presentations


Presentation on theme: "Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Graphic Programming Advanced Computer Programming."— Presentation transcript:

1 Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Graphic Programming Advanced Computer Programming Part - 2

2 13A-2 Lecture Objective After completing this Lecture: – Students will be able to understand how games are created in VB – Will be able to Identify Components used in Game Programming – Will use built-in routines to draw lines, ellipse, rectangles, and polygons – Will be able to create transparent items – Will be able to rotate items at any angle – Will be able to play sounds for all events

3 Crash Landing

4 Intro.PNG

5 BG.PNG

6 Ship.PNG

7 Flame.PNG

8 Pad.PNG

9 Crash.PNG

10 Green.PNG

11 Red.PNG

12 tmrMain Timer

13 Crash Landing picShip picFlame picFuel picCrash picBG picPad picSpeedpicAngle picPosition lblSpeed lblAngle lblPosition lblTime lblInfo lblMode

14 Coding Variables Dim Grav As Double Dim G As Double Dim Time As Integer Dim MaxTime As Integer Dim TimeLimited As Boolean Dim Crashed As Boolean Dim Landed As Boolean Dim SpeedOK As Boolean Dim AngleOK As Boolean = True Dim PositionOK As Boolean Dim AppPath As String = Mid(Application.StartupPath, 1, _ InStr(Application.StartupPath, "\bin")) Private FuelBitmap As Bitmap Private FuelGraph As Graphics Dim ShipX As Integer Dim ShipY As Integer Dim X As Integer = 0 Dim Y As Integer = 0 Dim XMin As Integer = -49 Dim YMin As Integer = 20 Dim XMax As Integer = Me.Width – 15 Dim YMax As Integer = Me.Height – 120 Dim PadX As Integer Dim PadY As Integer Dim Angle As Integer Dim Fuel As Double

15 Coding (Subroutines) Private Sub picBG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picBG.Click Me.lblInfo.Visible = False Me.picBG.ImageLocation = AppPath & "images\BG.png" PadX = 5 + Int(Rnd() * XMax - 45) PadY = 120 + Int(Rnd() * (YMax - 55)) If PadY > YMax Then PadY = YMax - 5 Me.picPad.Image = CType(Bitmap.FromFile(AppPath & "images\Pad.png"), Bitmap) Me.picPad.Parent = Me.picBG Me.picPad.BackColor = (Color.Transparent) Me.picPad.Left = PadX Me.picPad.Top = PadY Me.picPad.Visible = True ShipX = -33 + Int(Rnd() * XMax + 33) ShipY = YMin X = 0 Grav = 0 G = 0.25 Angle = 0 Crashed = False Landed = False Time = 0 MaxTime = (Abs(ShipX - PadX) + Abs(ShipY - PadY)) / 2 Fuel = 100.0 FuelGraph.FillRectangle(Brushes.Green, 1, 1, 14, CInt(Fuel)) Me.picFuel.Image = FuelBitmap Me.picFuel.Left = XMax - 20 Me.picFuel.Visible = True Me.picShip.Visible = False Me.picCrash.Visible = False Me.picShip.Left = ShipX Me.picShip.Top = ShipY Me.picShip.Image = CType(Bitmap.FromFile(AppPath & "images\ship.png"), Bitmap) Me.picShip.Parent = Me.picBG Me.picShip.BackColor = (Color.Transparent) Me.picShip.Visible = True Me.tmrMain.Enabled = True Me.lblSpeed.Visible = True Me.picSpeed.Visible = True Me.lblAngle.Visible = True Me.picAngle.Visible = True Me.lblPosition.Visible = True Me.picPosition.Visible = True Me.lblMode.Visible = False End Sub

16 Coding (Subroutines) Private Sub tmrMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMain.Tick If TimeLimited = True Then Time = Time + 1 lblTime.Text = MaxTime - Time If Time >= MaxTime And Crashed = False Then Crashed = True tmrMain.Enabled = False Me.lblInfo.Text = "Time Over!" Me.lblInfo.Visible = True Me.lblInfo.BringToFront() Me.picCrash.Left = 8 Me.picCrash.Top = 2 Me.picCrash.ImageLocation = AppPath & "images\Crash.png" Me.picCrash.Visible = True My.Computer.Audio.Play(AppPath & "sounds\Crash.wav") Me.tmrMain.Enabled = False Exit Sub End If 'If Fuel <= 0.0 Then ' Un-comment if Game ends on fuel empty ' Crashed = True ' tmrMain.Enabled = False ' Me.lblInfo.Text = "Out of Fule!" ' Me.lblInfo.Visible = True ' Me.lblInfo.BringToFront() ' Me.picCrash.Left = 8 ' Me.picCrash.Top = 2 ' Me.picCrash.ImageLocation = AppPath & "images\Crash.png" ' Me.picCrash.Visible = True ' My.Computer.Audio.Play(AppPath & "sounds\Crash.wav") 'End If FuelGraph.FillRectangle(Brushes.Black, 1, 1, 14, CInt(100 - Fuel)) Me.picFuel.Image = FuelBitmap Grav = Grav + G ShipY = ShipY + Grav ShipX = ShipX + X If Grav > 10 Then Grav = 10 If Grav < -10 Then Grav = -10 If ShipX > XMax Then ShipX = XMin If ShipX < XMin Then ShipX = XMax If ShipY < YMin Then ShipY = YMin Grav = 0 G = 0.25 End If Me.picShip.Left = ShipX Me.picShip.Top = ShipY If Grav < 5 Then Me.picSpeed.ImageLocation = AppPath & "images\Green.PNG" SpeedOK = True Else Me.picSpeed.ImageLocation = AppPath & "images\Red.PNG" SpeedOK = False End If If Angle > -10 And Angle < 10 Then Me.picAngle.ImageLocation = AppPath & "images\Green.PNG" AngleOK = True Else Me.picAngle.ImageLocation = AppPath & "images\Red.PNG" AngleOK = False End If If ShipX > PadX - 10 And ShipX < PadX + 10 Then Me.picPosition.ImageLocation = AppPath & "images\Green.PNG" PositionOK = True Else Me.picPosition.ImageLocation = AppPath & "images\Red.PNG" PositionOK = False End If Me.picFlame.Visible = False If SpeedOK = True And AngleOK = True And PositionOK = True And (ShipY > PadY - 60 And ShipY < PadY - 55) Then ShipY = PadY Crashed = False Landed = True Me.lblInfo.Text = "Perfect Landing!" Me.lblInfo.Visible = True Me.lblInfo.BringToFront() X = 0 Grav = 0 G = 0 Angle = 0 Me.tmrMain.Enabled = False Me.lblMode.Visible = True My.Computer.Audio.Play(AppPath & "sounds\Landing.wav") Else If ((ShipX > PadX - 55 And ShipX PadY - 60 And ShipY YMax Then Me.picCrash.Parent = Me.picShip Me.picCrash.BackColor = (Color.Transparent) Me.picCrash.Left = 8 Me.picCrash.Top = 2 Me.picCrash.ImageLocation = AppPath & "images\Crash.png" Me.picCrash.Visible = True Me.lblInfo.Text = "YOU CRASHED!" Me.lblInfo.Visible = True Me.lblInfo.BringToFront() X = 0 Grav = 0 G = 0 Crashed = True Landed = False Me.tmrMain.Enabled = False Me.lblMode.Visible = True My.Computer.Audio.Play(AppPath & "sounds\Crash.wav") End If End Sub

17 Coding (Subroutines) Private Sub frmCrash_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = 112 Then ' F1 Key is pressed to get help If Me.tmrMain.Enabled = True Then Me.tmrMain.Enabled = False MsgBox("Welcome to Crash Landing!" & Chr(13) & Chr(13) & "Your Aim is to safely Land your Rocket Ship on the Landing Pad." & Chr(13) & Chr(10) & "The Speed, Angle and Position indicators must be Green for Safe Landing." & Chr(13) & Chr(10) & "At the end of each game you can press T to toggle Time Limited Mode." & Chr(13) & Chr(13) & "Use these arrow keys to control the Ship:" & " Left Right" & Chr(13) & " v" & Chr(13) & " Thrust", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Crash Landing Help...") Me.tmrMain.Enabled = True Else MsgBox("Welcome to Crash Landing!" & Chr(13) & Chr(13) & "Your Aim is to safely Land your Rocket Ship on the Landing Pad." & Chr(13) & Chr(10) & "The Speed, Angle and Position indicators must be Green for Safe Landing." & Chr(13) & Chr(10) & "At the end of each game you can press T to toggle Time Limited Mode." & Chr(13) & Chr(13) & "Use these arrow keys to control the Ship:" & " Left Right" & Chr(13) & " v" & Chr(13) & " Thrust", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Crash Landing Help...") End If If (Landed = True Or Crashed = True) And e.KeyValue = 84 Then ' T key is pressed to Toggle Time Limited Mode TimeLimited = Not TimeLimited Me.lblTime.Visible = TimeLimited End If If Crashed = True Or Landed = True Then Exit Sub If Fuel > 0.0 Then If e.KeyValue = 40 Then ' Down Key is Pressed Grav = Grav - G * 2 Fuel = Fuel - 0.5 Me.picFlame.Left = ShipX + 23 + (Angle / 4) Mod 10 Me.picFlame.Top = ShipY + 52 If Grav <> 0 Then Me.picFlame.Image = CType(Bitmap.FromFile(AppPath & "images\flame.png"), Bitmap) Me.picFlame.Parent = Me.picBG Me.picFlame.BackColor = (Color.Transparent) Me.picFlame.BringToFront() Me.picFlame.Visible = True My.Computer.Audio.Play(AppPath & "sounds\Fire.wav") End If If e.KeyValue = 37 Then ' Left Key is Pressed X = X - 1 Fuel = Fuel - 0.5 Angle = Angle + 5 End If If e.KeyValue = 39 Then ' Right Key is Pressed X = X + 1 Fuel = Fuel - 0.5 Angle = Angle - 5 End If Me.picShip.Image = CType(Bitmap.FromFile(AppPath & "images\ship.png"), Bitmap) RotateShip(Angle) End If End Sub

18 Coding (Subroutines) Private Sub frmCrash_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.picBG.ImageLocation = AppPath & "images\Intro.png" My.Computer.Audio.Play(AppPath & "sounds\StartMusic.wav") FuelBitmap = New Bitmap(picFuel.Width, picFuel.Height) FuelGraph = Graphics.FromImage(FuelBitmap) Me.picFuel.Image = FuelBitmap Me.lblMode.Left = (Me.Width - Me.lblMode.Width - 10) / 2 End Sub

19 Coding (Subroutines) Private frmCrash_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize XMax = Me.Width - 15 If XMax < 40 Then XMax = 40 YMax = Me.Height - 120 If YMax < 40 Then YMax = 40 Me.picFuel.Left = XMax - 20 Me.lblInfo.Left = (Me.Width - Me.lblInfo.Width) / 2 Me.lblInfo.Top = (Me.Height - Me.lblInfo.Height) / 2.4 Me.lblMode.Left = (Me.Width - Me.lblMode.Width) / 2 Me.lblMode.Top = (Me.Height - 60) End Sub

20 Coding (Subroutines) Private Sub lblInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblInfo.Click picBG_Click(sender, e) End Sub Private Sub picShip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picShip.Click picBG_Click(sender, e) End Sub Private Sub picPad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picPad.Click picBG_Click(sender, e) End Sub Private Sub picCrash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picCrash.Click picBG_Click(sender, e) End Sub

21 Coding (Subroutines) Private Sub RotateShip(ByVal RAngle As Single) ' Object Rotate Code courtesy of VB-Helper.com http://www.vb- helper.com/howto_net_image_rotate.htmlhttp://www.vb- helper.com/howto_net_image_rotate.html ' Copy the output bitmap from the source image. Dim bm_in As New Bitmap(Me.picShip.Image) ' Make an array of points defining the ' image's corners. Dim wid As Single = bm_in.Width Dim hgt As Single = bm_in.Height Dim corners As Point() = { _ New Point(0, 0), _ New Point(wid, 0), _ New Point(0, hgt), _ New Point(wid, hgt)} ' Translate to center the bounding box at the origin. Dim cx As Single = wid / 2 Dim cy As Single = hgt / 2 Dim i As Long For i = 0 To 3 corners(i).X -= cx corners(i).Y -= cy Next i ' Rotate. Dim theta As Single = Single.Parse(Angle) * PI / 180.0 Dim sin_theta As Single = Sin(theta) Dim cos_theta As Single = Cos(theta) Dim Xx As Single Dim Yy As Single For i = 0 To 3 Xx = corners(i).X Yy = corners(i).Y corners(i).X = Xx * cos_theta + Yy * sin_theta corners(i).Y = -Xx * sin_theta + Yy * cos_theta Next i ' Translate so Xx >= 0 and Yy >=0 for all corners. Dim xmin As Single = corners(0).X Dim ymin As Single = corners(0).Y For i = 1 To 3 If xmin > corners(i).X Then xmin = corners(i).X If ymin > corners(i).Y Then ymin = corners(i).Y Next i For i = 0 To 3 corners(i).X -= xmin corners(i).Y -= ymin Next i ' Create an output Bitmap and Graphics object. Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin)) Dim gr_out As Graphics = Graphics.FromImage(bm_out) ' Drop the last corner lest we confuse DrawImage, ' which expects an array of three corners. ReDim Preserve corners(2) ' Draw the result onto the output Bitmap. gr_out.DrawImage(bm_in, corners) ' Display the result. Me.picShip.Image = bm_out End Sub

22 Assignment 3 Create Your own Crash Landing Game – Create Software Requirement Document – Upload your code to the CMS system

23 Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education The End Questions?


Download ppt "Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Graphic Programming Advanced Computer Programming."

Similar presentations


Ads by Google