Jump to content
  • Advertisement
Sign in to follow this  
xlordt

Help In Game Creation

This topic is 4431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sup all, Im creating an arkanoide clone game (sorta) but.. I have a problem with it, I was wondering if anyone can tell me how to make my ball hit only one object per frame.. For example.. I have 3 lines of bricks, now once in the blue.. my ball bounces and hit 2 of them at the same time.. but I really dont want this, Someone suggested that I make my collisions better, but I have no idea how to. The game is created using the pygame module, Now I have been learning pygame for only 3 week's and Im not to bright when It comes down to a few tutorials, I learn by example. So Im hoping that somene here can actually point me in the right direction. Here are the codes. Also I dont know if this is the right place to post this.. so if its not.. forgive me for my ignorance.
class Objects( pygame.sprite.Sprite ):
	def __init__( self, color, position, size = ( 17, 8 )):
		pygame.sprite.Sprite.__init__( self )
		
		self.image = pygame.Surface( size ).convert( )
		self.image.fill( color )
		self.rect    = self.image.get_rect( )
		self.rect.topleft   = position
		
	def GetRect( self ):
		return self.rect
		
################################################
# Here is were we creat the class to draw the ball          #
################################################

class DrawBall( pygame.sprite.Sprite ):
	def __init__ ( self, color, radius, velocity, _screensrect,  _objrect, _scoreRect ):
		pygame.sprite.Sprite.__init__( self )
		
		self.image = pygame.Surface(( 2 * radius, 2 * radius )).convert( )
		self.rect    = self.image.get_rect( )
		
		self.image.set_colorkey(( 0, 0, 0 ))
		self.rect.center = ( 100, 150 )
		
		pygame.draw.circle( self.image, color, ( radius, radius ), radius )
		
		self.velocity_x, self.velocity_y = velocity
		
		self._screenrect = _screensrect
		self._objrect_    = _objrect
		self._scoreRect  = _scoreRect
		
	def UpdateBall( self ):
		
		self.rect.move_ip(( self.velocity_x, self.velocity_y ))
		
		if self.rect.bottom >= self._screenrect.bottom - 1 or self.rect.top <= self._screenrect.top - 1:  self.velocity_y = - self.velocity_y
		#if self.rect.left <= self._screenrect.left - 2 or self.rect.right >= self._screenrect.right - 1: self.velocity_x = - self.velocity_x
		if self.rect.left <= 1: self.velocity_x = - self.velocity_x
		
	def _Colliding_( self, boolen ):
		if boolen == True:
			if self.rect.bottom >= self._objrect_.bottom -1 or self.rect.top <= self._objrect_.top: self.velocity_y = - self.velocity_y
			if self.rect.right >= self._objrect_.right -1 or self.rect.left <= self._objrect_.left -1: self.velocity_x = - self.velocity_x
		
	def _CollidingScore( self, boolen ):
		if boolen == True:
			if self.rect.left <= self._scoreRect.left - 1: self.velocity_x = - self.velocity_x

			
#########################################
# Here is were all the scoring will get created   #
#########################################

class _ScoreBoard( pygame.sprite.Sprite ):
	def __init__( self, scores ):
		pygame.sprite.Sprite.__init__( self )
		
		self.image = pygame.Surface(( 100, 300 ))
		self.rect    = self.image.get_rect( )
		pygame.draw.rect( self.image, ( 100, 5, 10 ), self.rect, 2 )
		self.rect.topright = ( 300, 0 )
		self.image.fill(( 100, 5, 10 ))
		
		self.scores = scores
		
		self.fonts =  pygame.font.Font( None, 15 )
		self.render = self.fonts.render( "Score: %s" % self.scores, 1, ( 255, 255, 100 ))
		self.trects   = self.render.get_rect( centerx = self.image.get_width( ) / 2 )
		self.image.blit( self.render, self.trects )
		
	def _AddScore_( self, points ):
		self.scores = points[0] - points[1] * 10
		
		self.render = self.fonts.render( "Score: %s" % self.scores, 1, ( 255, 255, 100 ))
		self.trects   = self.render.get_rect( centerx = self.image.get_width( ) / 2 )
		print "%s" % ( points[0] - points[1] )
		self.image.fill(( 100, 5, 10 ))
		self.image.blit( self.render, self.trects )
		 

##############################################
# Lets Create the Gens and extend it in to the list       #
##############################################

def GenRows( _color_, _posx_, _posy_, repeat ):
	_rows = []
	
	for x in range( repeat ):
		_gens = [( _color_, [ _posx_ * 18, _posy_ + 10] )]
		_rows.extend( _gens )
		_posx_ += 1
		
	return _rows
		
def TotalBricks( _total_, _totalLeft_ ):
	_all_ = ( _total_, _totalLeft_ )
	return _all_
	
#############################################
# Now we define the screen and call our classes      #
#############################################

start  = 0

_screen_ = pygame.display.set_mode( [ 300, 300 ] )
_scrRect  = _screen_.get_rect( )
_scoreb_ = _ScoreBoard( start )
_score_rect = _scoreb_.rect

velocityX, velocityY =  random.randint( 2, 5 ), random.randint( 2, 5 )

objgroup = pygame.sprite.RenderUpdates( )
scoresG   = pygame.sprite.RenderUpdates( )

count, remove  = 1, 0
pos_x, pos_y = 0, 11
rows   = []
clr = [([255, 0, 0]), ([0, 255, 0]), ([0, 0, 255])]

for x in range( len( clr )) :	
	
	rows =  GenRows( clr[x], pos_x, pos_y * count, 11 )
	count = count + 1; 
	
	for colors, pos in rows :
		objrects = Objects( colors, pos )
		objgroup.add( objrects )
		
scoresG.add( _scoreb_ )

total = len( objgroup )

_ball    = DrawBall( [ 255, 0, 0 ],  4, ( velocityX, velocityY ), _scrRect, objrects.GetRect( ), _score_rect )
_loop_ = True

while pygame.event.poll( ).type != pygame.KEYDOWN: 
	pygame.time.delay( 30 )
	
	total_bricks = len( objgroup )
	
	_screen_.fill(( 0, 0, 0 ))
	
	rectlist  = objgroup.draw( _screen_ )
	
	if pygame.sprite.spritecollide( _ball, objgroup, True ):
		_chall_ = TotalBricks( total, total_bricks )
		_ball._Colliding_( True ); _scoreb_._AddScore_( _chall_ )
	
	if pygame.sprite.spritecollide( _ball, scoresG, False ):
		_ball._CollidingScore( True )
	
	_ball.UpdateBall( )
	
	_screen_.blit( _scoreb_.image, _scoreb_.rect )
	_screen_.blit( _ball.image, _ball.rect )
	
	pygame.display.update( rectlist )
	pygame.display.flip( )

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!