1
QB64 Discussion / Re: Blocking stuff D:
« on: April 07, 2020, 11:41:23 am »
Well, that was odd but glad you got that sorted out lol
Listen to QB64 Report!
Our podcast on all things QB64 is out now. Listen at
http://podcast.qb64.org
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Hi Prithak
your last post code is very incomplete!
So I add something to become it clearer than now....
but the glitch is logic and not about typing....
This your issue let me remember another in a graphic game engine in which the output of grafic doesn't recognize the position of objects of the scene in respect with the main character... in that engine issue the engine has not option to distinguish the position of the character towards the object that are able to be pass back.
In your collision engine you haven't thought what it happens to the OldY and OldX variable at the second cycle of loop...
here my code can show you what it happensCode: QB64: [Select]
TYPE vec1 TYPE vec2 player.speed = 5 player.x = 100 ' character need a point of start X and Y player.y = 200 map = "map1.txt" openMap j = 1 makeplayer playerMovement ' here graphic output ' here it tests for collision between character and obstacles block obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2 NEXT j _LIMIT 20 SUB openMap i = i + 1 INPUT #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i).y2, obstacle(i).r, obstacle(i).g, obstacle(i).b CLOSE #1 PRINT i SUB makeplayer SUB playerMovement ' if player X is in the X range of obstacle AND player Y is in the Y range of obstacle ' I restore the old X and Y to player player.x = oldx player.y = oldy LOCATE 10 ' if player is out of obstacle ' I store actual X and Y of player into OLD X and Y oldx = player.x oldy = player.y
as you can see when you go through the second obstacle using a FOR loop that tests before first obstacle and then the following second obstacle so it changes the value of OldX and OldY and then when it tests the second obstacle it restore the OldX and OldY calculated towards the first obstacle.
So you can imagine how to solve this issue... a STATIC flag into the collision engine? Or and array of OldY and OldY dor each obstacle to use...or whatelse?
screen _newimage(1280,720,32)
type vec1
x as integer
y as integer
speed as integer
end type
type vec2
x1 as integer
y1 as integer
x2 as integer
y2 as integer
r as integer
g as integer
b as integer
btype as string
oldx as integer
oldy as integer
wl as string
end type
dim shared player as vec1
dim shared map as string
dim shared obstacle(9) as vec2
player.speed = 5
dim shared i as integer
map = "map1.txt"
player.x = _width / 2
player.y = _height / 2
openMap
j=1
do
cls
makeplayer
playerMovement
for j = 1 to i
if obstacle(j).btype = "obs" then
line (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2),_rgb32(obstacle(j).r,obstacle(j).g,obstacle(j).b),BF
if player.x + 10 >= obstacle(j).x1 and player.x - 10 <= obstacle(j).x2 and player.y + 10 >= obstacle(j).y1 and player.y - 10 <= obstacle(j).y2 then
player.x = obstacle(j).oldx
player.y = obstacle(j).oldy
else
obstacle(j).oldx = player.x
obstacle(j).oldy = player.y
end if
elseif obstacle(j).btype = "warp" then
line (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2),_rgb32(obstacle(j).r,obstacle(j).g,obstacle(j).b),BF
if collision%(obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2) then
map = obstacle(i).wl
openMap
end if
end if
next j
_display
_limit 120
loop
sub openMap
i=0
open map for input as #1
while not eof(1)
i = i + 1
input #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i). y2, obstacle(i).r, obstacle(i).g, obstacle(i).b
if obstacle(i).btype = "warp" then input #1, obstacle(i).wl
wend
close #1
end sub
sub makeplayer
circle (player.x, player.y), 10, _rgb32(100,200,255)
paint (player.x, player.y), _rgb32(100,200,255), _rgb32(100,200,255)
end sub
sub playerMovement
if _keydown(18432) then player.y = player.y - player.speed
if _keydown(20480) then player.y = player.y + player.speed
if _keydown(19200) then player.x = player.x - player.speed
if _keydown(19712) then player.x = player.x + player.speed
end sub
function collision%(x1,y1,x2,y2)
if player.x >= x1 and player.x <= x2 and player.y >= y1 and player.y <= y2 then
collision% = 1
end if
end function
obs,600,50,1280,100,100,200,255
obs,0,0,700,50,100,200,255
warp,500,500,550,550,255,200,100,map2.txt
obs, 600,50,1280,100,242,242,242
obs, 500,0,700,50,242,242,242
warp, 100,100,150,150,255,200,100,map1.txt
screen _newimage(1280,720,32)
type vec1
x as integer
y as integer
speed as integer
end type
type vec2
x1 as integer
y1 as integer
x2 as integer
y2 as integer
r as integer
g as integer
b as integer
btype as string
end type
dim shared player as vec1
dim shared speed as integer
dim shared map as string
dim shared obstacle(9) as vec2
dim shared oldx, oldy as integer
player.speed = 5
dim shared i as integer
map = "map1.txt"
openMap
j=1
do
cls
makeplayer
playerMovement
for j = 1 to i
line (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2),_rgb32(obstacle(j).r,obstacle(j).g,obstacle(j).b),BF
block obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2
next j
_display
_limit 60
loop
sub openMap
open map for input as #1
while not eof(1)
i = i + 1
input #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i). y2, obstacle(i).r, obstacle(i).g, obstacle(i).b
wend
close #1
end sub
sub makeplayer
circle (player.x, player.y), 10, _rgb32(100,200,255)
paint (player.x, player.y), _rgb32(100,200,255), _rgb32(100,200,255)
end sub
sub playerMovement
if _keydown(18432) then player.y = player.y - player.speed
if _keydown(20480) then player.y = player.y + player.speed
if _keydown(19200) then player.x = player.x - player.speed
if _keydown(19712) then player.x = player.x + player.speed
end sub
sub block(x1,y1,x2,y2)
if player.x + 10 >= x1 and player.x - 10 <= x2 and player.y + 10 >= y1 and player.y - 10 <= y2 then
player.x = oldx
player.y = oldy
else
oldx = player.x
oldy = player.y
end if
end sub
screen _newimage(1280,720,32)
type vec1
x as integer
y as integer
end type
type vec2
x1 as integer
y1 as integer
x2 as integer
y2 as integer
r as integer
g as integer
b as integer
btype as string
end type
dim shared player as vec1
dim shared speed as integer
dim shared map as string
dim shared obstacle(9) as vec2
dim shared i as integer
map = "map1.txt"
openMap
do
cls
j = j + 1
if j > i then j = 0
_display
loop
sub openMap
open map for input as #1
while not eof(1)
input #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).x2, obstacle(i).y1, obstacle(i). y2, obstacle(i).r, obstacle(i).g, obstacle(i).b
i = i + 1
wend
close #1
end sub
"Input pas end of file!"Are you sure? I easily get 25-35 fps with my current programs and they are far more CPU/Memory heavy than what you've posted. I cant see that you'd notice any difference in speed if you used arrays aposed to individual variables to store each value.Ok. Would you mind sending me an example. Cuz, when I tried to do it. It just didn't work.
Good luck,
Unseen
Just to get the code to compile I had to make this change to explicitly specify that the map1 file is in the same directory as the exe:Uhh... No you don't need to do that...Code: QB64: [Select]
map = ".\map1.txt"
Hi,Ya. I figured. But, Using Arrays would SIGNIFICANTLY slow down the computer. So, I figured this plan out. If this works, I won't have to worry about performance. Soo... If there is a way out, PLEASE TEACH MEH!!!
Its cause your only storing one set of data for the obstacles. You need to use an array to store more than one.
Id suggest you define a TYPE for the obstacles and then use an array of that type to handle them
i.e
type Obstacle
X1 AS integer
X2 as integer
Y1 as integer
Y2 as integer
Color as _unsigned long
End type
Dim Obstacle(9) as obstacle '// Creates an array of ten Obstacle type variables.
Have fun and happy coding!
Unseen
screen _newimage(1280,720,32)
_fullscreen
'Declaring Variables
type vec
x as integer
y as integer
speed as integer
end type
dim shared player as vec
dim shared map as string
dim shared oldx, oldy as integer
player.speed = 3
map = "map1.txt"
player.x = _width / 2
player.y = _height / 2
'Main Loop
do
cls
makeplayer
playerMovement
'Test
'makeObstacle 100,100,200,200,255,200,100
makeMap
'For Experiments: The co-ordinates
_printstring (0,0), str$(player.x) + str$(player.y)
_display
loop until _keyhit = 27
system
'Subs and Functions
sub makeplayer
circle (player.x, player.y), 10, _rgb32(100,200,255)
paint (player.x, player.y), _rgb32(100,200,255), _rgb32(100,200,255)
end sub
sub playerMovement
if _keydown(18432) then player.y = player.y - player.speed
if _keydown(20480) then player.y = player.y + player.speed
if _keydown(19200) then player.x = player.x - player.speed
if _keydown(19712) then player.x = player.x + player.speed
end sub
sub makeObstacle(x1,y1,x2,y2,r,g,b)
line (x1,y1)-(x2,y2),_rgb32(r,g,b), BF
if player.x + 10 >= x1 and player.x - 10 <= x2 and player.y + 10 >= y1 and player.y - 10 <= y2 then
player.x = oldx
player.y = oldy
else
oldx = player.x
oldy = player.y
end if
end sub
sub makeMap
open map for input as #1
while not eof(1)
input #1, cm$,x1,y1,x2,y2,r,g,b
if cm$ = "obs" then
makeObstacle x1,y1,x2,y2,r,g,b
end if
wend
close #1
end sub
obs, 0,0,700,50,242,242,242
obs, 800,50,1280,100,242,242,242
screen _newimage(800,600,32)
dim shared x as integer
dim shared y as integer
dim shared oldx as integer
dim shared oldy as integer
x = 100
y = 200
color , _rgb32(50,200,0)
cls
do
cls
open "blocks.txt" for input as #1
while not eof(1)
input #1, x1,y1,x2,y2,r,g,b
makeobs x1,y1,x2,y2,r,g,b
wend
close #1
IF _KEYDOWN(CVI(CHR$(0) + "P")) THEN y = y - 5 '_KEYDOWN(20480)
IF _KEYDOWN(CVI(CHR$(0) + "H")) THEN y = y + 5 '_KEYDOWN(18432)
IF _KEYDOWN(CVI(CHR$(0) + "K")) THEN x = x + 5 '_KEYDOWN(19200)
IF _KEYDOWN(CVI(CHR$(0) + "M")) THEN x = x - 5 '_KEYDOWN(19712)
'The sprites will be 20x20 pixels, I believe...
line (_width/2,_height/2)-step(20,20),_rgb32(255,200,100),BF
_printstring(0,0),str$(x) + " " + str$(y)
_limit 100
_Display
loop
FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
collision% = 0
ELSE
collision% = 1
END IF
END FUNCTION
sub makeobs (x1,y1,w,h,r,g,b)
line(x + x1, y + y1)-step(w,h),_rgb32(r,g,b),BF
Line(x + x1, y + y1)-step(w,h),_Rgb32(255,255,255),B
'200, 200 - 220, 220
if collision%(_width/2,_height/2,20,20,x + x1, y + y1,w,h) then
y = oldy
x = oldx
else
oldy = y
oldx = x
end if
end sub
200,100,100,100,100,200,255
200,300,100,100,100,200,255
100,200,100,100,100,200,255
300,200,100,100,100,200,255
Fine! Well suited. Very fast!
Please add <Help> button with instructions for keys active and a little story or the goal.
Negative Feedback:
Aargh! There is a big big big mistake at line code 25...Code: QB64: [Select]you use LEN("play")! Aargh! you must use LEN("quit")!!!!
:-)
:-P what do you think of this joke?
It’s because the _KEYDOWN check is in an ELSEIF statement. Make all those become independent IF checks instead.