Graphic Programming Advanced Computer Programming Mc GrawHill Technology

  • Slides: 23
Download presentation
Graphic Programming Advanced Computer Programming Mc. Graw-Hill Technology Education Part - 2 Copyright ©

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

Lecture Objective • After completing this Lecture: – Students will be able to understand

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 2

Crash Landing

Crash Landing

Intro. PNG

Intro. PNG

BG. PNG

BG. PNG

Ship. PNG

Ship. PNG

Flame. PNG

Flame. PNG

Pad. PNG

Pad. PNG

Crash. PNG

Crash. PNG

Green. PNG

Green. PNG

Red. PNG

Red. PNG

tmr. Main Timer

tmr. Main Timer

Crash Landing pic. Speed pic. Angle pic. Position lbl. Time pic. Ship pic. Flame

Crash Landing pic. Speed pic. Angle pic. Position lbl. Time pic. Ship pic. Flame lbl. Speed pic. Fuel lbl. Angle lbl. Position lbl. Info lbl. Mode pic. BG pic. Pad pic. Crash

Coding Dim Ship. X As Integer Dim Ship. Y As Integer Dim X As

Coding Dim Ship. X As Integer Dim Ship. Y As Integer Dim X As Integer = 0 Dim Grav As Double Dim Y As Integer = 0 Dim G As Double Dim XMin As Integer = -49 Dim Time As Integer Dim YMin As Integer = 20 Dim Max. Time As Integer Dim XMax As Integer = Me. Width – 15 Dim Time. Limited As Boolean Dim YMax As Integer = Me. Height – 120 Dim Crashed As Boolean Dim Pad. X As Integer Dim Landed As Boolean Dim Pad. Y As Integer Dim Speed. OK As Boolean Dim Angle As Integer Dim Angle. OK As Boolean = True. Dim Fuel As Double Dim Position. OK As Boolean Dim App. Path As String = Mid(Application. Startup. Path, 1, _ In. Str(Application. Startup. Path, "bin")) Private Fuel. Bitmap As Bitmap Private Fuel. Graph As Graphics • Variables

Coding (Subroutines) Private Sub pic. BG_Click(By. Val sender As System. Object, By. Val e

Coding (Subroutines) Private Sub pic. BG_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles pic. BG. Click Me. lbl. Info. Visible = False Me. pic. BG. Image. Location = App. Path & "imagesBG. png" Pad. X = 5 + Int(Rnd() * XMax - 45) Pad. Y = 120 + Int(Rnd() * (YMax - 55)) If Pad. Y > YMax Then Pad. Y = YMax - 5 Me. pic. Pad. Image = CType(Bitmap. From. File(App. Path & "imagesPad. png"), Bitmap) Me. pic. Pad. Parent = Me. pic. BG Me. pic. Pad. Back. Color = (Color. Transparent) Me. pic. Pad. Left = Pad. X Me. pic. Pad. Top = Pad. Y Me. pic. Pad. Visible = True Ship. X = -33 + Int(Rnd() * XMax + 33) Ship. Y = YMin X=0 Grav = 0 G = 0. 25 Angle = 0 Crashed = False Landed = False Time = 0 Max. Time = (Abs(Ship. X - Pad. X) + Abs(Ship. Y - Pad. Y)) / 2 Fuel = 100. 0 Fuel. Graph. Fill. Rectangle(Brushes. Green, 1, 1, 14, CInt(Fuel)) Me. pic. Fuel. Image = Fuel. Bitmap Me. pic. Fuel. Left = XMax - 20 Me. pic. Fuel. Visible = True Me. pic. Ship. Visible = False Me. pic. Crash. Visible = False Me. pic. Ship. Left = Ship. X Me. pic. Ship. Top = Ship. Y Me. pic. Ship. Image = CType(Bitmap. From. File(App. Path & "imagesship. png"), Bitmap) Me. pic. Ship. Parent = Me. pic. BG Me. pic. Ship. Back. Color = (Color. Transparent) Me. pic. Ship. Visible = True Me. tmr. Main. Enabled = True Me. lbl. Speed. Visible = True Me. pic. Speed. Visible = True Me. lbl. Angle. Visible = True Me. pic. Angle. Visible = True Me. lbl. Position. Visible = True Me. pic. Position. Visible = True Me. lbl. Mode. Visible = False End Sub

Coding (Subroutines) Private Sub tmr. Main_Tick(By. Val sender As System. Object, By. Val e

Coding (Subroutines) Private Sub tmr. Main_Tick(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles tmr. Main. Tick If Time. Limited = True Then Time = Time + 1 lbl. Time. Text = Max. Time - Time If Time >= Max. Time And Crashed = False Then Crashed = True tmr. Main. Enabled = False Me. lbl. Info. Text = "Time Over!" Me. lbl. Info. Visible = True Me. lbl. Info. Bring. To. Front() Me. pic. Crash. Left = 8 Me. pic. Crash. Top = 2 Me. pic. Crash. Image. Location = App. Path & "imagesCrash. png" Me. pic. Crash. Visible = True My. Computer. Audio. Play(App. Path & "soundsCrash. wav") Me. tmr. Main. Enabled = False Exit Sub End If 'If Fuel <= 0. 0 Then ' Un-comment if Game ends on fuel empty ' Crashed = True ' tmr. Main. Enabled = False ' Me. lbl. Info. Text = "Out of Fule!" ' Me. lbl. Info. Visible = True ' Me. lbl. Info. Bring. To. Front() ' Me. pic. Crash. Left = 8 ' Me. pic. Crash. Top = 2 ' Me. pic. Crash. Image. Location = App. Path & "imagesCrash. png" ' Me. pic. Crash. Visible = True ' My. Computer. Audio. Play(App. Path & "soundsCrash. wav") 'End If Fuel. Graph. Fill. Rectangle(Brushes. Black, 1, 1, 14, CInt(100 - Fuel)) Me. pic. Fuel. Image = Fuel. Bitmap Grav = Grav + G Ship. Y = Ship. Y + Grav Ship. X = Ship. X + X If Grav > 10 Then Grav = 10 If Grav < -10 Then Grav = -10 If Ship. X > XMax Then Ship. X = XMin If Ship. X < XMin Then Ship. X = XMax If Ship. Y < YMin Then Ship. Y = YMin Grav = 0 G = 0. 25 End If Me. pic. Ship. Left = Ship. X Me. pic. Ship. Top = Ship. Y If Grav < 5 Then Me. pic. Speed. Image. Location = App. Path & "imagesGreen. PNG" Speed. OK = True Else Me. pic. Speed. Image. Location = App. Path & "imagesRed. PNG" Speed. OK = False End If If Angle > -10 And Angle < 10 Then Me. pic. Angle. Image. Location = App. Path & "imagesGreen. PNG" Angle. OK = True Else Me. pic. Angle. Image. Location = App. Path & "imagesRed. PNG" Angle. OK = False End If If Ship. X > Pad. X - 10 And Ship. X < Pad. X + 10 Then Me. pic. Position. Image. Location = App. Path & "imagesGreen. PNG" Position. OK = True Else Me. pic. Position. Image. Location = App. Path & "imagesRed. PNG" Position. OK = False End If Me. pic. Flame. Visible = False If Speed. OK = True And Angle. OK = True And Position. OK = True And (Ship. Y > Pad. Y - 60 And Ship. Y < Pad. Y - 55) Then Ship. Y = Pad. Y Crashed = False Landed = True Me. lbl. Info. Text = "Perfect Landing!" Me. lbl. Info. Visible = True Me. lbl. Info. Bring. To. Front() X=0 Grav = 0 G=0 Angle = 0 Me. tmr. Main. Enabled = False Me. lbl. Mode. Visible = True My. Computer. Audio. Play(App. Path & "soundsLanding. wav") Else If ((Ship. X > Pad. X - 55 And Ship. X < Pad. X + 58) And (Ship. Y > Pad. Y - 60 And Ship. Y < Pad. Y + 15)) Or Ship. Y > YMax Then Me. pic. Crash. Parent = Me. pic. Ship Me. pic. Crash. Back. Color = (Color. Transparent) Me. pic. Crash. Left = 8 Me. pic. Crash. Top = 2 Me. pic. Crash. Image. Location = App. Path & "imagesCrash. png" Me. pic. Crash. Visible = True Me. lbl. Info. Text = "YOU CRASHED!" Me. lbl. Info. Visible = True Me. lbl. Info. Bring. To. Front() X=0 Grav = 0 G=0 Crashed = True Landed = False Me. tmr. Main. Enabled = False Me. lbl. Mode. Visible = True My. Computer. Audio. Play(App. Path & "soundsCrash. wav") End If End Sub

Coding (Subroutines) Private Sub frm. Crash_Key. Down(By. Val sender As Object, By. Val e

Coding (Subroutines) Private Sub frm. Crash_Key. Down(By. Val sender As Object, By. Val e As System. Windows. Forms. Key. Event. Args) Handles Me. Key. Down If e. Key. Code = 112 Then ' F 1 Key is pressed to get help If Me. tmr. Main. Enabled = True Then Me. tmr. Main. Enabled = False Msg. Box("Welcome to Crash Landing!" & 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) & "Use these arrow keys to control the Ship: " & " Left <- + -> Right" & Chr(13) & " v" & Chr(13) & " Thrust", Msg. Box. Style. Ok. Only Or Msg. Box. Style. Information, "Crash Landing Help. . . ") Me. tmr. Main. Enabled = True Else Msg. Box("Welcome to Crash Landing!" & 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) & "Use these arrow keys to control the Ship: " & " Left <- + -> Right" & Chr(13) & " v" & Chr(13) & " Thrust", Msg. Box. Style. Ok. Only Or Msg. Box. Style. Information, "Crash Landing Help. . . ") End If If (Landed = True Or Crashed = True) And e. Key. Value = 84 Then ' T key is pressed to Toggle Time Limited Mode Time. Limited = Not Time. Limited Me. lbl. Time. Visible = Time. Limited End If If Crashed = True Or Landed = True Then Exit Sub If Fuel > 0. 0 Then If e. Key. Value = 40 Then ' Down Key is Pressed Grav = Grav - G * 2 Fuel = Fuel - 0. 5 Me. pic. Flame. Left = Ship. X + 23 + (Angle / 4) Mod 10 Me. pic. Flame. Top = Ship. Y + 52 If Grav <> 0 Then Me. pic. Flame. Image = CType(Bitmap. From. File(App. Path & "imagesflame. png"), Bitmap) Me. pic. Flame. Parent = Me. pic. BG Me. pic. Flame. Back. Color = (Color. Transparent) Me. pic. Flame. Bring. To. Front() Me. pic. Flame. Visible = True My. Computer. Audio. Play(App. Path & "soundsFire. wav") End If If e. Key. Value = 37 Then X=X-1 Fuel = Fuel - 0. 5 Angle = Angle + 5 End If ' Left Key is Pressed If e. Key. Value = 39 Then X=X+1 Fuel = Fuel - 0. 5 Angle = Angle - 5 End If ' Right Key is Pressed Me. pic. Ship. Image = CType(Bitmap. From. File(App. Path & "imagesship. png"), Bitmap) Rotate. Ship(Angle) End If End Sub

Coding (Subroutines) Private Sub frm. Crash_Load(By. Val sender As Object, By. Val e As

Coding (Subroutines) Private Sub frm. Crash_Load(By. Val sender As Object, By. Val e As System. Event. Args) Handles Me. Load Me. pic. BG. Image. Location = App. Path & "imagesIntro. png" My. Computer. Audio. Play(App. Path & "soundsStart. Music. wav") Fuel. Bitmap = New Bitmap(pic. Fuel. Width, pic. Fuel. Height) Fuel. Graph = Graphics. From. Image(Fuel. Bitmap) Me. pic. Fuel. Image = Fuel. Bitmap Me. lbl. Mode. Left = (Me. Width - Me. lbl. Mode. Width - 10) / 2 End Sub

Coding (Subroutines) Private frm. Crash_Resize(By. Val sender As Object, By. Val e As System.

Coding (Subroutines) Private frm. Crash_Resize(By. Val sender As Object, By. Val e As System. Event. Args) Handles Me. Resize XMax = Me. Width - 15 If XMax < 40 Then XMax = 40 YMax = Me. Height - 120 If YMax < 40 Then YMax = 40 Me. pic. Fuel. Left = XMax - 20 Me. lbl. Info. Left = (Me. Width - Me. lbl. Info. Width) / 2 Me. lbl. Info. Top = (Me. Height - Me. lbl. Info. Height) / 2. 4 Me. lbl. Mode. Left = (Me. Width - Me. lbl. Mode. Width) / 2 Me. lbl. Mode. Top = (Me. Height - 60) End Sub

Coding (Subroutines) Private Sub lbl. Info_Click(By. Val sender As System. Object, By. Val e

Coding (Subroutines) Private Sub lbl. Info_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles lbl. Info. Click pic. BG_Click(sender, e) End Sub Private Sub pic. Ship_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles pic. Ship. Click pic. BG_Click(sender, e) End Sub Private Sub pic. Pad_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles pic. Pad. Click pic. BG_Click(sender, e) End Sub Private Sub pic. Crash_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles pic. Crash. Click pic. BG_Click(sender, e) End Sub

Coding (Subroutines) Private Sub Rotate. Ship(By. Val RAngle As Single) ' Object Rotate Code

Coding (Subroutines) Private Sub Rotate. Ship(By. Val RAngle As Single) ' Object Rotate Code courtesy of VB-Helper. com http: //www. vbhelper. com/howto_net_image_rotate. html ' Copy the output bitmap from the source image. Dim bm_in As New Bitmap(Me. pic. Ship. 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. From. Image(bm_out) ' Drop the last corner lest we confuse Draw. Image, ' which expects an array of three corners. Re. Dim Preserve corners(2) ' Draw the result onto the output Bitmap. gr_out. Draw. Image(bm_in, corners) ' Display the result. Me. pic. Ship. Image = bm_out End Sub

Assignment 3 1. Create Your own Crash Landing Game 1. Create Software Requirement Document

Assignment 3 1. Create Your own Crash Landing Game 1. Create Software Requirement Document 2. Upload your code to the CMS system

The End Questions? Mc. Graw-Hill Technology Education Copyright © 2006 by The Mc. Graw-Hill

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