Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added GameCollection/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added GameCollection/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added GameCollection/2048.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added GameCollection/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added GameCollection/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added GameCollection/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added GameCollection/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file added GameCollection/game2048_score.npy
Binary file not shown.
259 changes: 259 additions & 0 deletions GameCollection/game_2048.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
import pygame
import numpy as np
import copy
import os
from pygame.locals import *

BOARD_SIZE = 4
TILE_SIZE = 100
GRID_SIZE = 10
WINDOW_WIDTH = BOARD_SIZE * TILE_SIZE + (BOARD_SIZE + 1) * GRID_SIZE
WINDOW_HEIGHT = WINDOW_WIDTH + 100

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (187, 173, 160)
LIGHT_GRAY = (205, 193, 180)

COLORS = {
0: (205, 193, 180),
2: (238, 228, 218),
4: (237, 224, 200),
8: (242, 177, 121),
16: (245, 149, 99),
32: (246, 124, 95),
64: (246, 94, 59),
128: (237, 207, 114),
256: (237, 204, 97),
512: (237, 200, 80),
1024: (237, 197, 63),
2048: (237, 194, 46),
}

score = 0
win = 0
FILENAME = 'game2048_score.npy'

def init():
if FILENAME not in os.listdir():
np.save(FILENAME, 0)
init_board = choice(np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=np.int64))
return init_board

def choice(board):
udict = {}
count = 0
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if not board[i, j]:
udict[count] = (i, j)
count += 1
if count == 0:
return board
random_number = np.random.randint(0, count)
two_or_four = np.random.choice([2, 2, 2, 4])
board[udict[random_number]] = two_or_four
return board

def basic(board):
global score
global win
for i in range(BOARD_SIZE):
flag = 1
while flag:
flag = 0
j = BOARD_SIZE - 2
while j >= 0:
if board[i, j] != 0:
if board[i, j + 1] == board[i, j]:
board[i, j + 1] = 2 * board[i, j]
if board[i, j + 1] == 2048:
win = 1
board[i, j] = 0
score += board[i, j + 1]
flag = 1
elif board[i, j + 1] == 0:
temp = board[i, j]
board[i, j] = board[i, j + 1]
board[i, j + 1] = temp
flag = 1
j -= 1
return board

def move_right(board):
return basic(board)

def move_up(board):
board = board[::-1, ::-1].T
board = basic(board)
board = board[::-1, ::-1].T
return board

def move_left(board):
board = board[::-1, ::-1]
board = basic(board)
board = board[::-1, ::-1]
return board

def move_down(board):
board = board.T
board = basic(board)
board = board.T
return board

def fail(board):
diff1 = board[:, 1:] - board[:, :-1]
diff2 = board[1:, :] - board[:-1, :]
inter = (diff1 != 0).all() and (diff2 != 0).all()
return inter

def load_score():
rank_score = np.load(FILENAME)
return rank_score

def save_score(score):
rscore = load_score()
if score > rscore:
np.save(FILENAME, score)

def draw_board(screen, board, rscore):
global score
screen.fill(GRAY)
font = pygame.font.SysFont('Arial', 36)
small_font = pygame.font.SysFont('Arial', 24)

score_text = small_font.render(f"Score: {score}", True, BLACK)
screen.blit(score_text, (20, 20))

high_text = small_font.render(f"Best: {max(score, rscore)}", True, BLACK)
screen.blit(high_text, (WINDOW_WIDTH - 120, 20))

for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
x = GRID_SIZE + j * (TILE_SIZE + GRID_SIZE)
y = 80 + GRID_SIZE + i * (TILE_SIZE + GRID_SIZE)

value = board[i, j]
color = COLORS.get(value, (60, 58, 50))
pygame.draw.rect(screen, color, (x, y, TILE_SIZE, TILE_SIZE))

if value != 0:
text_color = (119, 110, 101) if value <= 4 else (255, 255, 255)
text = font.render(str(value), True, text_color)
text_rect = text.get_rect(center=(x + TILE_SIZE // 2, y + TILE_SIZE // 2))
screen.blit(text, text_rect)

def main(return_to_menu=False):
global score, win
pygame.init()
os.chdir(os.path.dirname(os.path.abspath(__file__)))
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('2048')

board = init()
rscore = load_score()
score = 0
win = 0

running = True
is_fail = False
font2 = pygame.font.SysFont('Arial', 48)

while running:
draw_board(screen, board, rscore)

if win:
text = font2.render("You Win!", True, (255, 0, 0))
text_rect = text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2))
screen.blit(text, text_rect)
pygame.display.update()

if is_fail:
text = font2.render("Game Over!", True, (255, 0, 0))
text_rect = text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2))
screen.blit(text, text_rect)
pygame.display.update()

for event in pygame.event.get():
if event.type == QUIT:
save_score(score)
running = False
if return_to_menu:
return 'menu'
else:
pygame.quit()
return
elif event.type == KEYDOWN:
if win or is_fail:
if event.key == K_r:
save_score(score)
board = init()
score = 0
win = 0
is_fail = False
continue
elif event.key == K_q or event.key == K_ESCAPE:
save_score(score)
running = False
if return_to_menu:
return 'menu'
else:
pygame.quit()
return
elif event.key == K_TAB:
save_score(score)
if return_to_menu:
return 'snake'
continue

change = False
tempboard = copy.deepcopy(board)

if event.key == K_UP or event.key == K_w:
board = move_up(board)
elif event.key == K_DOWN or event.key == K_s:
board = move_down(board)
elif event.key == K_LEFT or event.key == K_a:
board = move_left(board)
elif event.key == K_RIGHT or event.key == K_d:
board = move_right(board)
elif event.key == K_r:
save_score(score)
board = init()
score = 0
win = 0
is_fail = False
continue
elif event.key == K_q or event.key == K_ESCAPE:
save_score(score)
running = False
if return_to_menu:
return 'menu'
else:
pygame.quit()
return
elif event.key == K_TAB:
save_score(score)
if return_to_menu:
return 'snake'
continue

if not (board == tempboard).all():
change = True

if change:
board = choice(board)

if (board != 0).all():
is_fail = fail(board)

pygame.display.update()

save_score(score)
if return_to_menu:
return 'menu'
else:
pygame.quit()

if __name__ == '__main__':
main()
120 changes: 120 additions & 0 deletions GameCollection/game_launcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import pygame
import sys
import os
from pygame.locals import *

os.chdir(os.path.dirname(os.path.abspath(__file__)))

from game_2048 import main as game_2048_main
from snack_pygame import main as snake_main

WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (100, 100, 100)
LIGHT_GRAY = (150, 150, 150)
BLUE = (0, 100, 200)
LIGHT_BLUE = (0, 150, 255)

pygame.init()

screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Game Collection')

def draw_text(text, font, color, x, y, center=True):
img = font.render(text, True, color)
rect = img.get_rect()
if center:
rect.center = (x, y)
else:
rect.topleft = (x, y)
screen.blit(img, rect)
return rect

def draw_button(text, x, y, width, height, color, hover_color, font, mouse_pos, mouse_click):
rect = pygame.Rect(x, y, width, height)
if rect.collidepoint(mouse_pos):
pygame.draw.rect(screen, hover_color, rect, border_radius=10)
if mouse_click:
return True
else:
pygame.draw.rect(screen, color, rect, border_radius=10)
draw_text(text, font, WHITE, x + width // 2, y + height // 2)
return False

def main_menu():
title_font = pygame.font.SysFont('Arial', 60, bold=True)
button_font = pygame.font.SysFont('Arial', 30)
small_font = pygame.font.SysFont('Arial', 20)

clock = pygame.time.Clock()

while True:
screen.fill((20, 20, 40))

mouse_pos = pygame.mouse.get_pos()
mouse_click = False

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
mouse_click = True

draw_text('Game Collection', title_font, WHITE, WINDOW_WIDTH // 2, 100)

if draw_button('2048', 300, 200, 200, 60, BLUE, LIGHT_BLUE, button_font, mouse_pos, mouse_click):
return '2048'

if draw_button('Snake', 300, 300, 200, 60, BLUE, LIGHT_BLUE, button_font, mouse_pos, mouse_click):
return 'snake'

if draw_button('Exit', 300, 400, 200, 60, GRAY, LIGHT_GRAY, button_font, mouse_pos, mouse_click):
pygame.quit()
sys.exit()

draw_text('Press TAB to switch games while playing', small_font, LIGHT_GRAY, WINDOW_WIDTH // 2, 550)

pygame.display.update()
clock.tick(30)

def run_game(game_name):
pygame.display.quit()
pygame.quit()
pygame.init()

while True:
if game_name == '2048':
result = game_2048_main(return_to_menu=True)
elif game_name == 'snake':
result = snake_main(return_menu=True)

if result == 'menu':
break
elif result == 'snake':
game_name = 'snake'
elif result == '2048':
game_name = '2048'

pygame.display.quit()
pygame.quit()
pygame.init()

pygame.display.quit()
pygame.quit()
pygame.init()
global screen
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Game Collection')

def main():
while True:
game = main_menu()
run_game(game)

if __name__ == '__main__':
main()
Loading