|
Post by lordtim on Sept 3, 2006 21:14:50 GMT -5
hey everybody i'm making a 2d game and i need to how to change screens please help
|
|
|
Post by davy on Sept 3, 2006 21:21:14 GMT -5
Change screens? You mean swap the buffer or put opengl in a 2d perspective?
|
|
|
Post by lordtim on Sept 3, 2006 21:23:32 GMT -5
no, like legend of zelda or metroid 1 screen = 1 room
|
|
|
Post by davy on Sept 3, 2006 21:26:03 GMT -5
Well, that would really depend on what kind of game your writing and how you are drawing the rooms. It's important for games like this that you write everything in a sortof engine instead of individually coding the levels (this was its alot easier to change everything)
Do you have any code or anything?
|
|
|
Post by lordtim on Sept 3, 2006 21:49:13 GMT -5
dim x, y, sx, sy, sOnScreen, ssx, ssy, ssOnScreen dim i x = 2 y = 22 sx = x + 2 sy = 22 ssx = x - 1 ssy = 22 ssOnScreen = false sOnScreen = false while true cls textmode(text_buffered) color (250,200,23) for i = 0 to 40 locate i,23:print"-" next color (0,255,2) locate x,y-2:print"_" locate x-1,y-1:print"/" locate x,y:print"|" locate x,y+1:print"^" color (215, 203, 121) locate x,y-1:print"o" locate x+1,y:print"-" locate x-1,y:print"-" if scankeydown(vk_left) and x > 1 and ssx > 0 then color (0,255,2) locate x+1,y-1:print"\" locate x-1,y-1:print" " x = x - 1 ssx = ssx - 1 if ssOnScreen then color (255,255,255) locate ssx+3,ssy:print"0" else color (215,203,121) locate x,y:print"-" endif endif if scankeydown(vk_right) and x < 39 and ssx < 37 then color (0,255,2) locate x-1,y-1:print"/" x = x + 1 ssx = ssx + 1 if ssOnScreen = true then color (255,255,255) locate ssx-1,ssy:print"0" else color (215,203,121) locate x-2,y:print"-" endif endif if scankeydown(vk_control) then ssOnScreen = true endif if scankeydown(vk_numpad0) then ssOnScreen = false endif if sOnScreen then sx = x sy = y else if scankeydown(vk_space) then sOnScreen = true color (255,255,255) sy = y:print"--" color (0,0,255) sx = x:print"<" endif endif if ScanKeyDown (VK_up) and ssy = ssy and y = y then y = y - 1 ssy = ssy - 1 else y = y + 1 ssy = ssy + 1 if y = y then y = 22 endif if ssy = ssy then ssy = y endif endif if y+1 = 18 then y = 22 endif if ssy+1 = 18 then ssy = y endif drawtext() sleep(75) wend
'' thats all i have for now i'm still working on getting the sword to slash '' i'l keep you posted
|
|
|
Post by davy on Sept 3, 2006 22:33:10 GMT -5
Ok, it seems like you need a method of storing/loading/drawing your levels. For a platform style game you dont ever want to just write out the code to draw one level, you have to make a level "format"
The best way to do this is to use either a data table, or load your level from a file. Do you know how to read from a data table or from a file? Which would you prefer? Files will offer you the ability to make large numbers of levels and even a level editor for your users to make their own, while data tables tend to be simpler and more geared towards a few internal levels.
Here is an example of a data-table based RPG tile engine...
This is a little complex, but could be adapted to a textbased engine as well. It all depends which route you want to take. Personally, I believe files offer alot more freedom.
Press 1, 2, or 3 to change the level...
'/// Enter 2d mode /// glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() glOrtho(0,WindowWidth(),WindowHeight(),0,-1,1) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glDisable(GL_DEPTH_TEST) glEnable(GL_TEXTURE_2D) TextMode(TEXT_OVERLAID)
'/// Engine constants const Levels=3 const Width=10 const Height=10
'/// Level structure /// struc SLevel dim Name$ dim Map(Width-1)(Height-1) endstruc dim SLevel Level(Levels-1) dim SLevel &pLevel dim dLevel dim CurrentLevel
data "Level 1 (By the lake)" data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,7,7,7,7,7,0,0 data 0,0,7,7,6,6,6,7,7,0 data 0,7,7,6,6,6,6,6,7,7 data 0,7,6,6,6,6,6,6,6,6 data 0,7,6,6,6,6,6,6,6,6
data "Level 2 (The rug room)" data 1,1,1,1,1,1,1,1,1,1 data 1,1,1,1,1,1,1,1,1,1 data 4,4,9,4,4,1,1,1,1,1 data 9,9,9,9,4,1,1,1,1,1 data 9,8,8,9,4,1,1,1,1,1 data 9,8,8,9,4,1,1,1,1,1 data 9,8,8,9,4,4,4,4,4,1 data 9,8,8,9,9,9,9,9,4,1 data 9,8,8,9,9,9,9,9,4,1 data 9,9,9,9,9,9,9,9,4,1
data "Level 3 (The dungeon)" data 2,2,2,2,2,2,2,2,2,2 data 2,2,2,5,3,5,2,2,2,2 data 2,5,5,5,3,5,5,5,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,5,5,5,5,5,5,5,2 data 2,2,2,2,2,2,2,2,2,2
'/// Commonly used variables /// dim i, Key$ dim x, y
'/// Load all levels /// for i=0 to Levels-1 &pLevel = &Level(i) Read pLevel.Name$ for y=0 to Height-1 for x=0 to Width-1 Read pLevel.Map(x)(y) next next next
'/// Load all textures /// dim Texture(9) Texture(0)=LoadTexture("textures\00005.jpg") Texture(1)=LoadTexture("textures\00006.jpg") Texture(2)=LoadTexture("textures\00007.jpg") Texture(3)=LoadTexture("textures\wall01.jpg") Texture(4)=LoadTexture("textures\00009.jpg") Texture(5)=LoadTexture("textures\pavement.jpg") Texture(6)=LoadTexture("textures\water.bmp") Texture(7)=LoadTexture("textures\00001.jpg") Texture(8)=LoadTexture("textures\Ceil01.jpg") Texture(9)=LoadTexture("textures\floor01.jpg")
'/// Set and load the first level /// CurrentLevel=1 Gosub LoadLevel
'################################################### 'Main Loop '################################################### do '/// Call the sub to draw the level /// Gosub DrawLevel '/// These keypresses are for changing the level /// Key$=Inkey$() if Key$="1" then CurrentLevel=1: Gosub LoadLevel: endif if Key$="2" then CurrentLevel=2: Gosub LoadLevel: endif if Key$="3" then CurrentLevel=3: Gosub LoadLevel: endif loop
'################################################### 'Sub routines '###################################################
'/// Sub to load the current level /// LoadLevel: &pLevel=&Level(CurrentLevel-1) dLevel=glGenLists(1) glNewList(dLevel, GL_COMPILE) for y=0 to Height-1 for x=0 to Width-1 glBindTexture(GL_TEXTURE_2D, texture(pLevel.Map(x)(y))) glBegin(GL_QUADS) glTexCoord2f(0,0): glVertex2f(x*32, y*32 ) glTexCoord2f(1,0): glVertex2f(x*32+32,y*32 ) glTexCoord2f(1,1): glVertex2f(x*32+32,y*32+32) glTexCoord2f(0,1): glVertex2f(x*32, y*32+32) glEnd() next next glEndList() return
'/// Sub to draw the loaded level /// DrawLevel: cls Locate 0,0: Print pLevel.Name$ glClear(GL_COLOR_BUFFER_BIT) glCallList(dLevel) DrawText() SwapBuffers() return
|
|
|
Post by lordtim on Sept 3, 2006 22:44:14 GMT -5
hey davy that was cool i'll try and see what i can do
|
|
|
Post by davy on Sept 3, 2006 23:40:43 GMT -5
Heres a small update, this one has a "player" that can be walked around using the arrow keys, the left and right sides of the screen will change the current level or "map section"
This could be an untidy start for an RPG engine if anyone wants to use it. A map editor and an external system of level loading wouldnt be too hard to add on. Then, of course... Collision maps or objects with a collision property for walls and such, and probably multi-layers to compete with the other 3d RPG engines out there.
Oh... And a main character that isnt an asteroid, lol.
'/// Enter 2d mode /// glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() glOrtho(0,WindowWidth(),WindowHeight(),0,-1,1) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glDisable(GL_DEPTH_TEST) glEnable(GL_TEXTURE_2D) TextMode(TEXT_OVERLAID) glEnable(GL_ALPHA_TEST) glAlphaFunc(GL_GREATER, .5)
'/// Engine constants const Levels=3 const Width=10 const Height=10
'/// Level structure /// struc SLevel dim Name$ dim Map(Width-1)(Height-1) endstruc dim SLevel Level(Levels-1) dim SLevel &pLevel dim dLevel dim CurrentLevel
data "Level 1 (By the lake)" data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,0,0,0,0,0,0,0 data 0,0,0,7,7,7,7,7,0,0 data 0,0,7,7,6,6,6,7,7,0 data 0,7,7,6,6,6,6,6,7,7 data 0,7,6,6,6,6,6,6,6,6 data 0,7,6,6,6,6,6,6,6,6
data "Level 2 (The rug room)" data 1,1,1,1,1,1,1,1,1,1 data 1,1,1,1,1,1,1,1,1,1 data 5,5,4,5,5,1,1,1,1,1 data 4,4,4,4,5,1,1,1,1,1 data 4,8,8,4,5,1,1,1,1,1 data 4,8,8,4,5,1,1,1,1,1 data 4,8,8,4,5,5,5,5,5,1 data 4,8,8,4,4,4,4,4,5,1 data 4,8,8,4,4,4,4,4,5,1 data 4,4,4,4,4,4,4,4,5,1
data "Level 3 (The dungeon)" data 2,2,2,2,2,2,2,2,2,2 data 2,2,2,5,3,5,2,2,2,2 data 2,5,5,5,3,5,5,5,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,3,3,3,3,3,3,5,2 data 2,5,5,5,5,5,5,5,5,2 data 2,2,2,2,2,2,2,2,2,2
'/// Commonly used variables /// dim i, Key dim x, y
'/// Load all levels /// for i=0 to Levels-1 &pLevel = &Level(i) Read pLevel.Name$ for y=0 to Height-1 for x=0 to Width-1 Read pLevel.Map(x)(y) next next next
'/// Load all textures /// dim Texture(9) Texture(0)=LoadTexture("textures\00005.jpg") Texture(1)=LoadTexture("textures\00006.jpg") Texture(2)=LoadTexture("textures\00007.jpg") Texture(3)=LoadTexture("textures\wall01.jpg") Texture(4)=LoadTexture("textures\00009.jpg") Texture(5)=LoadTexture("textures\pavement.jpg") Texture(6)=LoadTexture("textures\water.bmp") Texture(7)=LoadTexture("textures\00001.jpg") Texture(8)=LoadTexture("textures\Ceil01.jpg") Texture(9)=LoadTexture("textures\floor01.jpg")
'/// Set and load the first level /// CurrentLevel=1 Gosub LoadLevel
'/// Player structure /// struc SPlayer dim Pos#(1) dim Ang# dim Texture dim nPos#(1) dim Walking endstruc dim SPlayer Player
'/// Setup player /// Player.Pos#=vec2(0, 0) Player.Ang#=0 Player.Texture=LoadTexture("Data\asteroid.png") Player.nPos#=Player.Pos# Player.Walking=False
do '/// Call the sub to draw the level /// Gosub DrawLevel
'/// Do player walking and keypresses /// if Player.Walking=true then if Player.Pos#(0)<Player.nPos#(0) then Player.Pos#(0)=Player.Pos#(0)+.5 endif if Player.Pos#(0)>Player.nPos#(0) then Player.Pos#(0)=Player.Pos#(0)-.5 endif if Player.Pos#(1)<Player.nPos#(1) then Player.Pos#(1)=Player.Pos#(1)+.5 endif if Player.Pos#(1)>Player.nPos#(1) then Player.Pos#(1)=Player.Pos#(1)-.5 endif if Player.Pos#(0)=Player.nPos#(0) and Player.Pos#(1)=Player.nPos#(1) then Player.Walking=false endif else if ScanKeyDown(VK_RIGHT) then Player.Ang#=270: Player.Walking=true: Player.nPos#=Player.Pos#+vec2(32,0) endif if ScanKeyDown(VK_LEFT) then Player.Ang#=090: Player.Walking=true: Player.nPos#=Player.Pos#-vec2(32,0) endif if ScanKeyDown(VK_DOWN) then Player.Ang#=000: Player.Walking=true: Player.nPos#=Player.Pos#+vec2(0,32) endif if ScanKeyDown(VK_UP) then Player.Ang#=180: Player.Walking=true: Player.nPos#=Player.Pos#-vec2(0,32) endif Gosub Collision endif loop
'/// Sub to load the current level /// LoadLevel: &pLevel=&Level(CurrentLevel-1) dLevel=glGenLists(1) glNewList(dLevel, GL_COMPILE) for y=0 to Height-1 for x=0 to Width-1 glBindTexture(GL_TEXTURE_2D, texture(pLevel.Map(x)(y))) glBegin(GL_QUADS) glTexCoord2f(0,0): glVertex2f(x*32, y*32 ) glTexCoord2f(1,0): glVertex2f(x*32+32,y*32 ) glTexCoord2f(1,1): glVertex2f(x*32+32,y*32+32) glTexCoord2f(0,1): glVertex2f(x*32, y*32+32) glEnd() next next glEndList() return
'/// Sub to draw the loaded level /// DrawLevel: cls Locate 0,0: Print pLevel.Name$ glLoadIdentity() glClear(GL_COLOR_BUFFER_BIT) glCallList(dLevel) Gosub DrawPlayer DrawText() SwapBuffers() return
'/// Draw the player /// DrawPlayer: glTranslatef(Player.Pos#(0)+16, Player.Pos#(1)+16, 0) glRotatef(Player.Ang#, 0, 0, 1) glBindTexture(GL_TEXTURE_2D, Player.texture) glBegin(GL_QUADS) glTexCoord2f(0,0): glVertex2f(-16,-16) glTexCoord2f(1,0): glVertex2f( 16,-16) glTexCoord2f(1,1): glVertex2f( 16, 16) glTexCoord2f(0,1): glVertex2f(-16, 16) glEnd() return
'/// Do collision checks /// Collision: '/// Increase level when right side of screen is hit /// if Player.nPos#(0)>(Width-1)*32 then Player.Pos#(0)=0 Player.nPos#(0)=0 CurrentLevel=CurrentLevel+1 if CurrentLevel=4 then CurrentLevel=1 endif Gosub LoadLevel endif '/// Decrease level when left side of screen is hit /// if Player.nPos#(0)<0 then Player.Pos#(0)=(Width-1)*32 Player.nPos#(0)=(Width-1)*32 CurrentLevel=CurrentLevel-1 if CurrentLevel=0 then CurrentLevel=3 endif Gosub LoadLevel endif '/// Stop when top or bottom of screen are hit /// if Player.nPos#(1)<0 then Player.nPos#(1)=0 endif if Player.nPos#(1)>(Height-1)*32 then Player.nPos#(1)=(Height-1)*32 endif return
|
|
|
Post by crazynate on Mar 13, 2009 17:16:37 GMT -5
could it be made to work with tilemaps of different sizes?
|
|
|
Post by Darkjester on Mar 13, 2009 17:55:11 GMT -5
yes could
|
|
|
Post by crazynate on Mar 15, 2009 12:36:42 GMT -5
how?
|
|