For...Each freezes game. A newbie needs help

BlitzPlus Forums/BlitzPlus Beginners Area/For...Each freezes game. A newbie needs help

mbrenner(Posted 2016) [#1]
Hey everyone! Thanks for helping :). I'm a newbie at this and just need some help. My code is below.

Graphics 800,600,0,2
SetBuffer BackBuffer()

Dim array(10,10)

Data 1,1,1,1,1,1,1,1,1,1
Data 1,1,1,1,1,1,1,1,1,1
Data 1,1,1,1,1,1,1,1,1,1
Data 1,1,1,0,0,0,0,1,1,1
Data 1,1,1,0,0,0,0,1,1,1
Data 1,1,1,0,0,0,0,1,1,1
Data 1,1,1,0,0,0,0,1,1,1
Data 1,1,1,1,1,1,1,1,1,1
Data 1,1,1,1,1,1,1,1,1,1
Data 1,1,1,1,1,1,1,1,1,1


;###########################################
;#            Type Definitions             #
;###########################################
Type brick
	Field x,y
	Field image
	Field hitpoints
End Type

Type player
	Field lives
	Field x,y
	Field image
End Type

Type ball
	Field x,y
	Field vX, vY
	Field image
End Type

;###########################################
;#        Variable and New Types           #
;###########################################
Global weakbrick.brick = New brick
weakbrick\x = 150
weakbrick\y = 150
weakbrick\image = LoadImage("brick.bmp")
weakbrick\hitpoints = 1

Global ball.ball = New ball
ball\x = 400
ball\y = 300
ball\vX = 4
ball\vY = 4
ball\image = LoadImage("ball.bmp")

Global player.player = New player
player\lives = 3
player\x = 350
player\y = 580
player\image = LoadImage("player.bmp")

Global background1 = LoadImage("background1.bmp")
Global readinfo = 1

;###########################################
;#               Main Loop                 #
;###########################################
While Not KeyHit(1)
	Cls
	drawImages()
	drawlevel()
	movement()
	collisions()
	Flip
Wend


;###########################################
;#    Performs all Collision Detection     #
;###########################################
Function collisions()
	;Tests to see whether ball collides with sides
	If ball\y > 595 Then ball\vY = -ball\vY
	If ball\x > 796 Then ball\vX = -ball\vX
	If ball\y < 0 Then ball\vY = -ball\vY
	If ball\x < 0 Then ball\vX = -ball\vX
	
	;If the ball hits the player paddle	
	If ImagesCollide(player\image, player\x, player\y, 0, ball\image, ball\x,ball\y, 0) Then
		ball\vY = -ball\vY
	EndIf
	
	If ImagesCollide(weakbrick\image, weakbrick\x, weakbrick\y, 0, ball\image, ball\x,ball\y, 0) Then
		ball\vY = -ball\vY
	EndIf
	
	;If the player paddle hits the edges
	If player\x < 0 Then player\x = 0
	If player\x > 721 Then player\x = 721		
	
End Function


;###########################################
;#           Draws all images              #
;###########################################
Function drawImages()
	DrawImage(background1,0,0)
	DrawImage(player\image,player\x,player\y)
	DrawImage(ball\image,ball\x,ball\y)
End Function

Function drawlevel()
	For y = 1 To 10
		For x = 1 To 10
			If readinfo = 1 Then Read array(x,y)
			If array(x,y) = 1 Then
				DrawImage(weakbrick\image, x * 50, y * 20)
			EndIf
		Next
	Next
	readinfo = 0
End Function


;###########################################
;#            Movement Inputs              #
;###########################################
Function movement()
	;ball movement
	ball\x = ball\x + ball\vX
	ball\y = ball\y + ball\vY
	
	;player movement
	If KeyDown(203) Then
		player\x = player\x - 5
	Else If KeyDown(205) Then
		player\x = player\x + 5
	EndIf
End Function

End



I'm trying to create a breakout game and I have multiple bricks drawn to the screen. However, each time I use the FOR EACH method to try to add behaviors to the bricks it freezes the game. Why does it do this?

Everyone in advance.

-- Matt


okee(Posted 2016) [#2]
i would sort out the array sizes first

Dim array(10,10)
creates an array 11 x 11 as arrays start at 0
so an array with 10 elements would be
Dim array(9,9)

then use
for x = 0 to 9

At the moment it's just creating an empty element at the beginning
What was the for each code that was being used ?


Dan(Posted 2016) [#3]
In your code, you have defined only 1 instance of the Brick type.

here is how i would do it:



Then you can use collision detection like this:




Dan(Posted 2016) [#4]
A small game, for Blitz3d - click on maximize box ;)



(edit #xxl)