Slowing down ??
Blitz3D Forums/Blitz3D Beginners Area/Slowing down ??
| ||
Hi all, Having trouble with my attempt at a game slowing down ? even if I just sit and move the ship left and right without firing a shot it suddenly slows right down ?...The other thing I was scratching my head about was the fact that when I put my Setbuffer backbuffer() command at the top of the code the gamescreen flickered terribly...Moved it to the main game loop and its fine ?...Scuse the code in general..its my first attempt at something a bit bigger than "Hello World " !! Any constructive critisism will be very welcome as it will of course help me learn !! Code as follows: ;Space invaders....First attempt!! ;By Dave Simmonite ;21st Dec 2005 ;------------ ;Stuff to do- ;------------ ;Sort out the alien movement !!............... ;Draw the images..............................DONE ;draw the titlescreen.........................DONE ;Sort out ther types and their fields.........DONE ;Define the correct functions ................ONGOING ;Sort out the Music...........................SOON ;Sort out constants - variables etc...........ADDING AS REQ ;Do a screen for the keymapping...............DONE ;Functions: ;drawtitlescreen().........Draws the titlescreen and play music intro ;drawgamescreen()..........Draws the game ;Checkinput()..............Check for movement and collisions ;update()..................Move the missile upwards ;Drawenemy()...............Draw the aliens ;Movealien()...............Move the aliens accros and down ;Check_Collision().........Check for missile hitting alien ;---------------------------------------------------------------------------------------------------------------| ;------------------------ ;Variables and constants- ;------------------------ Const width = 1024 ;Graphics height Const height = 768 ;Graphics width Const quit =1 ;Escape key scancode Const leftkey = 203 ;left arrow key scancode Const rightkey = 205 ;right arrow key scancode Const firekey = 57 ;spacebar scancode Const playerspeed = 7 ;Speed that player moves Const enemyspeed = 5 ;Speed that alien moves Const missilespeed = 20 ;Speed of bullet Global xorigin = 24 ;Origin of first alien x position Global yorigin = 12 ;Origin of first alien y position Const score = 0 ;Starting score Const lives = 3 ;Starting lives ;---------------------------------------------------------------------------------------------------------------| ;-------------- ;SOUND EFFECTS- ;-------------- Global clip=LoadSound ("C:\blitzsounds/assholes.wav") Global gunshot=LoadSound ("C:/blitzsounds/gunshot.wav") ;---------------------------------------------------------------------------------------------------------------| Graphics width,height,32,1 ; set graphics to 1024 x 768 and run at 32 bit colour in fullscreen mode ;---------------------------------------------------------------------------------------------------------------| ;------ ;TYPES- ;------ Type ship Field xpos Field ypos Field hits Field image End Type Type alien Field x,y Field hits End Type Type bullet Field x Field y End Type Global player.ship = New ship player\xpos=512 player\ypos =650 player\hits = 0 player\image = LoadImage("spaceship001.jpg") ;---------------------------------------------------------------------------------------------------------------| ;--------------- ;Main game loop- ;--------------- drawtitlescreen() ; call the drawtitlescreen function SetBuffer BackBuffer() ; Make sure that this is put in the correct place or it screws the game up !!!! ; It causes the gamescreen to flicker ...Dont know why yet ?? While Not KeyHit(quit) drawgamescreen() ; call the drawgamescreen function checkInput() update() drawenemy() movealien() Flip Wend ;------------------- ;End main game loop- ;------------------- ;---------------------------------------------------------------------------------------------------------------| ;---------- ;FUNCTIONS- ;---------- Function drawtitlescreen() ; Define function for the titlescreen titlescreen = LoadImage("titlescreen.jpg") DrawImage titlescreen,0,0 PlaySound clip Delay 5000 Text 80,60,"RIGHT ARROW KEY TO MOVE SHIP RIGHT" Delay 2000 Text 80,80,"LEFT ARROW KEY TO MOVE SHIP LEFT " Delay 2000 Text 80,100,"SPACE TO FIRE" Delay 2000 Text 80,140,"ESCAPE KEY WILL QUIT IF YOU CANT TAKE IT!!!" Delay 2000 Text 80,180,"PRESS ANY KEY TO CONTINUE....." WaitKey End Function ;---------------------------------------------------------------------------------------------------------------| Function drawgamescreen() ; Draw the starting screen Cls DrawImage player\image,player\xpos,player\ypos Text 50,730,"SCORE..." + score Text 850,730, "LIVES LEFT..." + lives End Function ;---------------------------------------------------------------------------------------------------------------| Function checkinput() ; Check for keys pressed... If KeyHit(firekey) PlaySound gunshot bullet.bullet= New bullet bullet\x =player\xpos -5 bullet\y = player\ypos-15 EndIf If KeyDown (rightkey) ;Move ship to the right player\xpos = player\xpos + playerspeed If player\xpos > width - 80 ; 80 is the player image width...this stops it going offscreen player\xpos= width -80 EndIf EndIf If KeyDown (leftkey) ; Move ship left player\xpos = player\xpos - playerspeed If player\xpos <0 player\xpos=0 EndIf EndIf End Function ;---------------------------------------------------------------------------------------------------------------| Function update() ; This moves the bullet upwards missile = LoadImage ("c:\blitzpics\missile.jpg") For bullet.bullet=Each bullet bullet\y = bullet\y -10 ; -10 is the speed it will travel...this can be altered to suit If bullet\y < 0 Delete bullet Else DrawImage missile,bullet\x,bullet\y EndIf Next End Function Function drawenemy() enemyship = LoadImage ("C:\blitzpics\alien.jpg") enemy.alien = New alien enemy\x = xorigin enemy\y = yorigin For numenemy = 0 To 59 DrawImage enemyship,enemy\x,enemy\y enemy\x = enemy\x + 80 If enemy\x >width - 64 enemy\x = xorigin enemy\y = enemy\y + 64 EndIf Next End Function ;---------------------------------------------------------------------------------------------------------------| Function movealien() ; Need to sort this out not finished yet ! For enemy.alien = Each alien enemy\x = enemy\x + 16 If enemy\x >width - 32 enemy\y = enemy\y + 64 enemy\x = enemy\x - 16 EndIf Next End Function ;--------------------------------------------------------------------------------------------------------------| Function check_collision () ;This the next step to work on End Function ;--------------------------------------------------------------------------------------------------------------| |
| ||
For being one of your first programs I think your on the right track!! At a glance, your game could be slowing down because you are loading things in your functions and not in your initialization section before the main loop. for example in this function you loaded enemyship in your function, which I have learned is a bad thing ;======================= Function drawenemy() enemyship = LoadImage ("C:\blitzpics\alien.jpg") ;========================= as for the flickering, I myself have always set my buffer right after the initial graphicsmode call. I have encountered screen flickering like you described and it was when I had used the flip command too much or in an inconvenient place. -Soo hope any of this helps some |
| ||
It's OK to load your your titlescreen in the function like you do, because it only occurs once during the game. You should however, free the image from memory (with FreeImage) at the end of your titlescreen function, because you are done using it. However, as W pointed out, loading images (or any external files) in a function that is repeatedly called (like your drawenemy() and update() functions) is an extremely bad idea. You are calling those functions at the speed that your monitor updates (at least 50 times a second) and you are just filling up all the ram in your computer with useless images. Move the loading routines to the beginning of the program and make their variable Global, so that you can use them in your functions. Like so: Global enemyship = LoadImage ("C:\blitzpics\alien.jpg") Also, where you place SetBuffer BackBuffer() is not of any real concern as long as it's after the Graphics call, but it doesn't need to be in your main game loop. Just move it to just before your game loop and everything will be fine. Just so you realize it, you've already placed that command BEFORE your game loop so there's no need to move it (Your ;Main game loop- comment is not in the correct place). By the way, when posting code, use the forum tags: What are the forum codes? |
| ||
I noticed that you are intermixing forward-slash and backward-slash characters in your load commands. Maybe Blitz doesn't care which one you use, but you might want to be at least consistent:Global clip=LoadSound ("C:\blitzsounds/assholes.wav") Global gunshot=LoadSound ("C:/blitzsounds/gunshot.wav") |
| ||
You flickering problem is really strange since it seems you really use FLIP only one time in the whole program, as well as SETBUFFER. you may try to replace FLIP by VWAIT:FLIP 0 and see it it helps |
| ||
1) use Setbuffer Backbuffer() Immediately AFTER calling the Graphics command, and nowhere else. (to get the above working) 2) Inside your Drawtitlescreen, use FLIP before the DELAY 5000 line. Because you've set the current buffer to BackBuffer, all images will have been drawn "behind he scenes" and will need to be bought forward with FLIP. 3) Load ALL media outside of functions and loops- In an INIT section of code. This will fix the slowdown issue. and what the others said :) |