<commit_before>import pygame, os, sys, random, math

def getDeg(dx, dy):
	'Helper function that returns degree based on slope'
	hyp = math.hypot(dx, dy)
	if hyp == 0:
		return 0.0
	deg = math.degrees(math.acos(dx / hyp))
	if dy < 0:
		deg = 360 - deg
	return deg


class Movable(pygame.Rect):
	'Super class for movable game objects'

	def __init__(self, x, y, w, h, vel=0.0, direction=0.0):
		# init super class
		pygame.Rect.__init__(self, x, y, w, h)
		
		# precision (float) x and y 
		self.px = float(x)
		self.py = float(y)
		self.vel = vel
		self.direction = direction
		self.fixDxDy();

	def update(self):
		self.px += self.dx
		self.py += self.dy
		self.x = int(self.px)
		self.y = int(self.py)

	def applyForce(self, vel, direction):
		rad = math.radians(direction)
		new_dx = math.cos(rad) * vel
		new_dy = math.sin(rad) * vel

		self.dx = self.dx + new_dx
		self.dy = self.dy + new_dy
		self.fixVelDir()

	def stop(self):
		self.vel = 0
		self.direction = 0
		self.dx = 0
		self.dy = 0

	def fixDxDy(self):
		rad = math.radians(self.direction)
		self.dx = math.cos(rad) * self.vel
		self.dy = math.sin(rad) * self.vel

	def fixVelDir(self):
		self.vel = math.hypot(self.dx, self.dy)
		self.direction = getDeg(self.dx, self.dy)

		

class Ball(Movable):
	"""Ball class for use in Pong and Breakout"""
	img = pygame.image.load('gfx/ball.png')

	def __init__(self, x=0.0, y=0.0):
		"""Constructor: loads image, initializes position and speed 
		optional x and y parameters set initial position"""

		# run super class constructor first
		rect = Ball.img.get_rect()
		Movable.__init__(self, x, y, rect.w, rect.h)

	def update(self):
		Movable.update(self)

		if self.x < 0 or self.x > 640 - self.w: 
			self.flipDx()
			Movable.update(self)

		if self.y < 0:
			self.flipDy()
			Movable.update(self)

	def display(self, surface):
		'Blits ball image to the give surface'
		surface.blit(Ball.img, self)

	def flipDx(self):
		"Flips the ball's horizontal speed from pos to neg or visa versa"
		#self.applyFddorce(self.dx*2, 180)#always 180 because dx can be min!
		self.dx = -self.dx
		self.fixVelDir()

	def flipDy(self):
		"Flips the ball's vertical speed from pos to neg or visa versa"
		#self.applyForce(self.dy*2, 270) # always 270 because dy can be min!
		self.dy = -self.dy
		self.fixVelDir()

	def goUp(self):
		"Ensures that dy is set to make ball go up"
		if self.dy > 0:
			self.dy = -self.dy
			self.fixVelDir()

class Paddle(Movable):
	paddles = pygame.image.load('gfx/Paddle Rotations.png')
	tracks = pygame.image.load('gfx/Track Rotations.png')

	def __init__(self, x=0.0, y=0.0):
		"""Constructor: loads images, initializes position  
		optional x and y parameters set initial position"""

		# run super class constructor first
		#rect = Paddle.img.get_rect()
		Movable.__init__(self, x, y, 32, 8)

		# acceleration
		self.accel = 2.0

		# Track rotation
		self.trot = 0.0

		# Paddle rotation
		self.prot = 0.0

	def moveLeft(self):
		'Moves paddle left'
		self.applyForce(self.accel, 180 + self.trot)

	def moveRight(self):
		'Moves paddle right'
		self.applyForce(self.accel, self.trot)

#	def rotateLeft(self):
#		self.trot = (self.trot - 1) % 360
#
#	def rotateRight(self):
#		self.trot = (self.trot + 1) % 360

	def update(self, terrain):
		# apply gravity
		self.applyForce(1.0, 90)

		 #slow down due to friction
		if self.vel < 0.15:
			self.applyForce(self.vel, self.direction - 180)
		else:
			self.applyForce(self.vel * 0.20, self.direction - 180)

		# do the actual move
		Movable.update(self)

		# make sure we don't fall off the screen
		if self.x > 640 - self.w:
			self.stop()
			self.x = self.px = 640 - self.w
		elif self.x < 0:
			self.stop()
			self.x = self.px = 0

		if self.y > 480 - self.h:
			self.stop()
			self.y = self.py = 480 - self.h
		elif self.y < 0:
			self.stop()
			self.y = self.py = 0

		# make sure we're on top of the terrain
		pxarray = pygame.PixelArray(terrain)
		dug_in = False
		x, y = self.x + self.w/2, self.y - 240 + 14
		while pxarray[x][y] >> 24 != 0:
			dug_in = True
			self.py -= 1
			y = self.y = int(self.py)
		if dug_in:
			self.dy = 0
			self.fixVelDir()

		# make sure we're rotated correctly
		del pxarray

	
	def display(self, surface):
		'Blits paddle image to the given surface'
		# determine rotation frame on spritesheet
		frame = int((self.trot % 180) / 11.25)
		area = pygame.Rect(frame * 32,0,32,32)
		# determine track location relative to paddle
		rad = math.radians(self.trot)
		dx = -6*math.sin(rad)
		dy = +6*math.cos(rad)
		# blit the tracks and the paddle
		surface.blit(Paddle.tracks, (self.x + dx, self.y - 12 + dy), area)
		surface.blit(Paddle.paddles, (self.x, self.y - 12), area)

class Brick(Movable):
	img = pygame.image.load('gfx/brick.png')

	def __init__(self, x, y, w, h):
		'constructor, rect stuff plus dx'
		Movable.__init__(self, x, y, w, h)<commit_msg>fixed terrain index out of bounds bug<commit_after>import pygame, os, sys, random, math

def getDeg(dx, dy):
	'Helper function that returns degree based on slope'
	hyp = math.hypot(dx, dy)
	if hyp == 0:
		return 0.0
	deg = math.degrees(math.acos(dx / hyp))
	if dy < 0:
		deg = 360 - deg
	return deg


class Movable(pygame.Rect):
	'Super class for movable game objects'

	def __init__(self, x, y, w, h, vel=0.0, direction=0.0):
		# init super class
		pygame.Rect.__init__(self, x, y, w, h)
		
		# precision (float) x and y 
		self.px = float(x)
		self.py = float(y)
		self.vel = vel
		self.direction = direction
		self.fixDxDy();

	def update(self):
		self.px += self.dx
		self.py += self.dy
		self.x = int(self.px)
		self.y = int(self.py)

	def applyForce(self, vel, direction):
		rad = math.radians(direction)
		new_dx = math.cos(rad) * vel
		new_dy = math.sin(rad) * vel

		self.dx = self.dx + new_dx
		self.dy = self.dy + new_dy
		self.fixVelDir()

	def stop(self):
		self.vel = 0
		self.direction = 0
		self.dx = 0
		self.dy = 0

	def fixDxDy(self):
		rad = math.radians(self.direction)
		self.dx = math.cos(rad) * self.vel
		self.dy = math.sin(rad) * self.vel

	def fixVelDir(self):
		self.vel = math.hypot(self.dx, self.dy)
		self.direction = getDeg(self.dx, self.dy)

		

class Ball(Movable):
	"""Ball class for use in Pong and Breakout"""
	img = pygame.image.load('gfx/ball.png')

	def __init__(self, x=0.0, y=0.0):
		"""Constructor: loads image, initializes position and speed 
		optional x and y parameters set initial position"""

		# run super class constructor first
		rect = Ball.img.get_rect()
		Movable.__init__(self, x, y, rect.w, rect.h)

	def update(self):
		Movable.update(self)

		if self.x < 0 or self.x > 640 - self.w: 
			self.flipDx()
			Movable.update(self)

		if self.y < 0:
			self.flipDy()
			Movable.update(self)

	def display(self, surface):
		'Blits ball image to the give surface'
		surface.blit(Ball.img, self)

	def flipDx(self):
		"Flips the ball's horizontal speed from pos to neg or visa versa"
		#self.applyFddorce(self.dx*2, 180)#always 180 because dx can be min!
		self.dx = -self.dx
		self.fixVelDir()

	def flipDy(self):
		"Flips the ball's vertical speed from pos to neg or visa versa"
		#self.applyForce(self.dy*2, 270) # always 270 because dy can be min!
		self.dy = -self.dy
		self.fixVelDir()

	def goUp(self):
		"Ensures that dy is set to make ball go up"
		if self.dy > 0:
			self.dy = -self.dy
			self.fixVelDir()

class Paddle(Movable):
	paddles = pygame.image.load('gfx/Paddle Rotations.png')
	tracks = pygame.image.load('gfx/Track Rotations.png')

	def __init__(self, x=0.0, y=0.0):
		"""Constructor: loads images, initializes position  
		optional x and y parameters set initial position"""

		# run super class constructor first
		#rect = Paddle.img.get_rect()
		Movable.__init__(self, x, y, 32, 8)

		# acceleration
		self.accel = 2.0

		# Track rotation
		self.trot = 0.0

		# Paddle rotation
		self.prot = 0.0

	def moveLeft(self):
		'Moves paddle left'
		self.applyForce(self.accel, 180 + self.trot)

	def moveRight(self):
		'Moves paddle right'
		self.applyForce(self.accel, self.trot)

#	def rotateLeft(self):
#		self.trot = (self.trot - 1) % 360
#
#	def rotateRight(self):
#		self.trot = (self.trot + 1) % 360

	def update(self, terrain):
		# apply gravity
		self.applyForce(1.0, 90)

		 #slow down due to friction
		if self.vel < 0.15:
			self.stop()
		else:
			self.applyForce(self.vel * 0.20, self.direction - 180)

		# do the actual move
		Movable.update(self)

		# make sure we don't fall off the screen
		if self.x > 640 - self.w:
			self.stop()
			self.x = self.px = 640 - self.w
		elif self.x < 0:
			self.stop()
			self.x = self.px = 0

		if self.y > 480 - self.h:
			self.stop()
			self.y = self.py = 480 - self.h
		elif self.y < 0:
			self.stop()
			self.y = self.py = 0

		# make sure we're on top of the terrain
		pxarray = pygame.PixelArray(terrain)
		dug_in = False
		x, y = self.x + self.w/2, self.y - 240 + 14
		while pxarray[x][y] >> 24 != 0:
			dug_in = True
			self.py -= 1
			self.y = int(self.py)
			y = self.y - 240 + 14

		if dug_in:
			self.dy = 0
			self.fixVelDir()

		# make sure we're rotated correctly
		del pxarray

	
	def display(self, surface):
		'Blits paddle image to the given surface'
		# determine rotation frame on spritesheet
		frame = int((self.trot % 180) / 11.25)
		area = pygame.Rect(frame * 32,0,32,32)
		# determine track location relative to paddle
		rad = math.radians(self.trot)
		dx = -6*math.sin(rad)
		dy = +6*math.cos(rad)
		# blit the tracks and the paddle
		surface.blit(Paddle.tracks, (self.x + dx, self.y - 12 + dy), area)
		surface.blit(Paddle.paddles, (self.x, self.y - 12), area)

class Brick(Movable):
	img = pygame.image.load('gfx/brick.png')

	def __init__(self, x, y, w, h):
		'constructor, rect stuff plus dx'
		Movable.__init__(self, x, y, w, h)
{'fraction_non_alphanumeric': 0.1377079482439926, 'fraction_numerical': 0.026083384678578764, 'mean_word_length': 3.0680868838763575, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1616078', 'n_tokens_mistral': 4372, 'n_tokens_neox': 3733, 'n_words': 1064}