none
First person player game -need help with code

    Question

  • 1 Imports microsoft.visualbasic  
    2 imports system.drawing.imaging  
    3 public class form1  
    4 inherits system.windows.forms.form  
    5    dim goingup,goingdown,goingleft,goingright as boolean  
    6  
    7  
    8 Private sub form1_keydown(byval sender as object,byval e as system.windows.forms.keyeventargs) Handles me.keydown  
    9 if (e.keycode=keys.space) then  
    10 timer1.interval =50  
    11 timer1.enabled=true  
    12 end if  
    13 end sub  
    14  
    15 'initialize for for background and ships  
    16 Private sub form1_load(byval sender as object,byval e as system.eventargs)  
    17  
    18 'computer controlled ship  
    19 'initialize image,color, coordinates, size  
    20 me.backgroundimage=image.fromfile(ofd.filename)  
    21 ship1box.image =image.fromfile(ofd.filename)  
    22 ship1box.sizemode=pictureboxsizemode.stretchimage  
    23 ship1box.backcolor=color.black  
    24 ship1box.location=new point(ship1box.location.x,ship1box.location.y)  
    25  
    26 'user controlled ship  
    27 'initialize image, coordinates,size, color)  
    28  
    29 ship2box.image=image.fromfile(ofd.filename)  
    30 'note ofd is the path name  
    31 ship2box.sizemode=pictureboxsizemode.stretchimage  
    32 ship2box.backcolor =color.transparent  
    33 shipb2box.location= new point(ship2box.location.x,ship2box.location.y)  
    34  
    35 'blaster initialization, set coordinates on top of user controlled ship  
    36 'set color,size,and whether it is visible or not   
    37  
    38 blastbx3.image=image.fromfile(ofd,filename)  
    39 blastbx3.sizemode=pictureboxsizemode.stretchimage  
    40 blastbx3.location= new point(blastbx3.location.x,blastbx3.location.y)  
    41 blastbx3.visible = false  
    42  
    43 'activate timer for computer controlled ship  
    44 timer2.enabled=true  
    45  
    46 end sub  
    47  
    48  
    49 Private sub Upbtn_Click(ByVal sender as object, byval e as System.Eventargs) handles Upbtn.click  
    50 goingup=true  
    51 'if up button is clicked go up ten spaces and move blast with ship2 invisibly  
    52 ship2box.location =new point(ship2box.location.x,ship2box.location.y-10)  
    53 blast3bx.location = new point(blastbx3.location.x,blastbx3.location.y-10)  
    54 blastbx3.location=new point(ship2box.location.x+44,ship2box.location.y+46)  
    55 timer1.enabled =false 'timer 1 controls the blast going to the left side of the screen  
    56 blastbx3.visible=false  
    57  
    58 end sub  
    59 private sub Downbtn_click(byval sender as object,byval e as system.eventargs)  
    60 handles downbtn.click  
    61 goingdown =true  
    62 'if down button is clicked take ship2 down ten spaces  
    63 ship2box.location=new point(ship2box.location.x,ship2box.location.y+10)  
    64 blastbx3.location=new point(blastbx3.location.x,blastbx3.location.y+10)  
    65 Blastbx3.location=new point(ship2.location.x+44,ship2box.location.y+46)  
    66 timer1.enabled=false  
    67 blastbx3.visible= false  
    68  
    69  
    70 'blast comes from user ship to computer controlled ship  
    71 ' it goes to the left part of the screen from any y-coordinate position   
    72 private sub timer1.tick(byval sender as object,byval e as system.eventargs)  
    73 handles timer1.tick  
    74 blastbx3.visible=true  
    75 blastbx3.location= new point(blastbx3.location.x-10,blastbx3.location.y)  
    76 end sub  
    77  
    78 'Here is where the errors occur and that you can make some suggestions  
    79 Private sub timer2_tick(byval senderas object,byval e as System.eventargs)  
    80 handles Timer2.tick  
    81 While (ship1box.location.y >Screen.Primaryscreen.bounds.top)  
    82 ship1box.location=new point(ship1box.location.x,ship1box.y-1)  
    83 end while  
    84  
    85 'the above while loop does move the computer controlled ship in one direction but when  
    86 doing the second while loop it is not a smooth ascending and descending motion  
    87  
    88 while (ship1box.y<screen.primaryScreen.bounds.bottom)  
    89 ship1box.location= new point(ship1box.location.x,ship1box.location.y+1)  
    90 end while  
    91 end sub  
    92 end class 
    To everybody interested in learning how to do a game for the first time I'm submitting this code it has some bugs which I hope some of you will be able to correct.  I feel that the only way for newbies to learn and for more experienced programmers to give feedback is through code.   This program has two ships. The first one is the computer controlled one but I'm having trouble setting up its movements in a steady motion.  The second ship is controlled by four buttons (up,down,left,right) the space bar is used to send a blast to the computer controlled ship.  My program is very simple it should not be complex and should be able to show someone just starting out how to do a simple game.  I myself am learning but if I can help others learn as well so be it!!! Please examine the code and give feedback if you want or suggestions.  By the way it uses three pictureboxes a fourth one hasn't been added yet for the computer controlled opponent ship.  You'll note that one of the pictureboxes is superimposed on the user's ship.  This was to make sure the blast moved with the main pictureboxu.

    Well here is the code hope you can add to it or make suggestions...This is a learning forum and for all the help people have given me I hope I can help someone else just starting out.  The ship2box(picturebox2) location is 1138,179(x,y)...its size is 274,77(width,height). The picturebox1(ship1box) location is 17,198(x,y)...its size is 249,81(width,height)
    1 'What is wrong with this code?  
    2  
    3  
    4 Private sub timer2_tick(byval sender as object,byval e as system.eventargs) handles timer2.tick  
    5 while (ship1box.location.y>screen.primaryscreen.bounds.top)   
    6 ship1box.location=new point(ship1box.location.x,ship1box.location.y-1)  
    7 end while  
    8 while(ship1box.location.y<screen.primaryscreen.bounds.bottom)  
    9 ship1box.location=new point(ship1box.location.x,ship1box.location.y+1)  
    10 end while  
    11 end sub  
    12 end class  
    13 'The motion of picturebox1(ship1box does not ascend or descend smoothly it either disappears  
    14 'or it skips to the bottom of the screen  
    15  




    ~turkey~
    • Edited by turkeygizzard Sunday, July 13, 2008 3:03 AM The code height wrong for vb program
    • Changed type turkeygizzard Sunday, July 13, 2008 3:04 AM code height was incorrect
    Sunday, July 13, 2008 3:00 AM