<commit_before>#!/usr/bin/python
from random import randint
from math import fabs
import pygame
from button import Button

#GUI
button_restart = Button("restart.png", 342, 400)
button_help = Button("help.png", 371, 400)

done = False
animating = False
frame = 0
score = 0
message = ""
pygame.init()
clock = pygame.time.Clock()
size = (400, 430)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("2048")
GRAY = (150, 150, 150)

tiles = []

IMAGES = [
		pygame.image.load("0.png"),
		pygame.image.load("2.png"),
		pygame.image.load("4.png"),
		pygame.image.load("8.png"),
		pygame.image.load("16.png"),
		pygame.image.load("32.png"),
		pygame.image.load("64.png"),
		pygame.image.load("128.png"),
		pygame.image.load("256.png"),
		pygame.image.load("512.png"),
		pygame.image.load("1024.png"),
		pygame.image.load("2048.png")
		]

def newTile():
	n = randint(1, 10)
	if n == 10:
		value = 2
	else:
		value = 1

	grid = []
	empty = []
	for i in range(16):
		grid.append(0)
	for i in tiles:
		grid[(i['grid_x'] * 4) + i['grid_y']] = 1
	for i in range(16):
		if grid[i] == 0:
			empty.append(i)

	pos = empty[randint(0, len(empty)-1)]
	xpos = pos % 4
	ypos = (pos - xpos) / 4

	tiles.append({
			'grid_x' : xpos,
			'grid_y' : ypos,
			'value' : value,
			'draw_x' : xpos * 100 + 5,
			'draw_y' : ypos * 100 + 5,
			'change_x' : 0,
			'change_y' : 0
			})

def hasLost():
	for tile in tiles:
		if ( movable(tile, 0,-1) or combinable(tile, 0,-1) or
		     movable(tile, 0, 1) or combinable(tile, 0, 1) or
			   movable(tile,-1, 0) or combinable(tile,-1, 0) or
			   movable(tile, 1, 0) or combinable(tile, 1, 0)  ):
			return False
	return True

def isEdge(tile, xmove, ymove):
	if xmove != 0:
		if tile['grid_x'] + xmove > 3 or tile['grid_x'] + xmove < 0:
			return True
	elif ymove != 0:
		if tile['grid_y'] + ymove > 3 or tile['grid_y'] + ymove < 0:
			return True
	return False

def movable(tile, xmove, ymove):
	if not isEdge(tile, xmove, ymove):
		for i in tiles:
			if i['grid_x'] == tile['grid_x'] + xmove and i['grid_y'] == tile['grid_y'] + ymove:
				return False
		return True

def combinable(tile, xmove, ymove):
	#if not isEdge(tile, xmove, ymove):
		for i in tiles:
			if i['grid_x'] == tile['grid_x'] + xmove and i['grid_y'] == tile['grid_y'] + ymove and i['value'] == tile['value']:
				return True
def combineTile(tile, xmove, ymove):
	if combinable(tile, xmove, ymove):
		for i in tiles:
			if i['grid_x'] == tile['grid_x'] + xmove and i['grid_y'] == tile['grid_y'] + ymove:
				tiles.remove(tile)
				i['value'] += 1
				global score, message, changed
				changed = True
				score += 2 ** i['value']
				if i['value'] >= 11:
					message = "You win!"
def moveTile(tile, xmove, ymove):
	while True:
		if movable(tile, xmove, ymove):
			tile['grid_x'] += xmove
			tile['grid_y'] += ymove
			global changed
			changed = True
		else:
			break

def initAnimation(tile, xmove, ymove):
	global animating
	animating = True
	if tile['grid_x'] * 100 + 5 == tile['draw_x']:
		tile['change_x'] = 0
	else:
		tile['change_x'] = fabs(tile['draw_x'] - (tile['grid_x'] * 100 + 5)) / 10 * xmove
	if tile['grid_y'] * 100 + 5 == tile['draw_y']:
		tile['change_y'] = 0
	else:
		tile['change_y'] = fabs(tile['draw_y'] - (tile['grid_y'] * 100 + 5)) / 10 * ymove
def animate():
	global animating, frame
	frame += 1
	if frame <= 10:
		for tile in tiles:
			tile['draw_x'] += tile['change_x']
			tile['draw_y'] += tile['change_y']
	else:
		animating = False
		frame = 0
<commit_msg>added animation for combining tiles<commit_after>#!/usr/bin/python
from random import randint
from math import fabs
import pygame
from button import Button

#GUI
button_restart = Button("restart.png", 342, 400)
button_help = Button("help.png", 371, 400)

done = False
animating = False
frame = 0
score = 0
message = ""
pygame.init()
clock = pygame.time.Clock()
size = (400, 430)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("2048")
GRAY = (150, 150, 150)

tiles = []

IMAGES = [
		pygame.image.load("0.png"),
		pygame.image.load("2.png"),
		pygame.image.load("4.png"),
		pygame.image.load("8.png"),
		pygame.image.load("16.png"),
		pygame.image.load("32.png"),
		pygame.image.load("64.png"),
		pygame.image.load("128.png"),
		pygame.image.load("256.png"),
		pygame.image.load("512.png"),
		pygame.image.load("1024.png"),
		pygame.image.load("2048.png")
		]

def newTile():
	n = randint(1, 10)
	if n == 10:
		value = 2
	else:
		value = 1

	grid = []
	empty = []
	for i in range(16):
		grid.append(0)
	for i in tiles:
		grid[(i['grid_x'] * 4) + i['grid_y']] = 1
	for i in range(16):
		if grid[i] == 0:
			empty.append(i)

	pos = empty[randint(0, len(empty)-1)]
	xpos = pos % 4
	ypos = (pos - xpos) / 4

	tiles.append({
			'grid_x' : xpos,
			'grid_y' : ypos,
			'value' : value,
			'target' : value,
			'draw_x' : xpos * 100 + 5,
			'draw_y' : ypos * 100 + 5,
			'change_x' : 0,
			'change_y' : 0,
			'remove' : False
			})

def hasLost():
	for tile in tiles:
		if ( movable(tile, 0,-1) or combinable(tile, 0,-1) or
		     movable(tile, 0, 1) or combinable(tile, 0, 1) or
			   movable(tile,-1, 0) or combinable(tile,-1, 0) or
			   movable(tile, 1, 0) or combinable(tile, 1, 0)  ):
			return False
	return True

def isEdge(tile, xmove, ymove):
	if xmove != 0:
		if tile['grid_x'] + xmove > 3 or tile['grid_x'] + xmove < 0:
			return True
	elif ymove != 0:
		if tile['grid_y'] + ymove > 3 or tile['grid_y'] + ymove < 0:
			return True
	return False

def movable(tile, xmove, ymove):
	if not isEdge(tile, xmove, ymove):
		for i in tiles:
			if i['grid_x'] == tile['grid_x'] + xmove and i['grid_y'] == tile['grid_y'] + ymove:
				return False
		return True

def combinable(tile, xmove, ymove):
	#if not isEdge(tile, xmove, ymove):
		for i in tiles:
			if i['grid_x'] == tile['grid_x'] + xmove and i['grid_y'] == tile['grid_y'] + ymove and i['value'] == tile['value']:
				return True

def combineTile(tile, xmove, ymove):
	if combinable(tile, xmove, ymove):
		for i in tiles:
			if i['grid_x'] == tile['grid_x'] + xmove and i['grid_y'] == tile['grid_y'] + ymove:
				#tiles.remove(tile)
				tile['remove'] = True
				tile['grid_x'] += xmove
				tile['grid_y'] += ymove
				i['target'] += 1
				global score, message, changed
				changed = True
				score += 2 ** i['target']
				if i['target'] >= 11:
					message = "You win!"

def moveTile(tile, xmove, ymove):
	while True:
		if movable(tile, xmove, ymove):
			tile['grid_x'] += xmove
			tile['grid_y'] += ymove
			global changed
			changed = True
		else:
			break

def initAnimation(tile, xmove, ymove):
	global animating
	animating = True
	if tile['grid_x'] * 100 + 5 == tile['draw_x']:
		tile['change_x'] = 0
	else:
		tile['change_x'] = fabs(tile['draw_x'] - (tile['grid_x'] * 100 + 5)) / 10 * xmove
	if tile['grid_y'] * 100 + 5 == tile['draw_y']:
		tile['change_y'] = 0
	else:
		tile['change_y'] = fabs(tile['draw_y'] - (tile['grid_y'] * 100 + 5)) / 10 * ymove

def animate():
	global animating, frame
	frame += 1
	if frame <= 10:
		for tile in tiles:
			tile['draw_x'] += tile['change_x']
			tile['draw_y'] += tile['change_y']
	else:
		animating = False
		frame = 0
		for tile in tiles:
			tile['value'] = tile['target']
			if tile['remove'] == True:
				tiles.remove(tile)

{'fraction_non_alphanumeric': 0.1819941916747338, 'fraction_numerical': 0.038998755358871526, 'mean_word_length': 3.4975124378109452, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '837964', 'n_tokens_mistral': 3385, 'n_tokens_neox': 2889, 'n_words': 754}