import pygame
import random
import math
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1720, 920))
clock = pygame.time.Clock()
running = True
t1 = pygame.Rect(0,0,600,600)
t1.center = (1720*(1/4),460)
t2 = pygame.Rect(150,160,600,600)
t2.center = (1720*(3/4),460)
p1 =[[0 for i in range(4)] for i in range(4)]
p2 =[[0 for i in range(4)] for i in range(4)]
sp = [2,2,2,2,2,2,2,2,2,4]
#sp = [16384]
p1att = []
p2att = []
normalFont = pygame.font.SysFont(None,50)
box_color=["#CCCC77","#DDDD88","#EEEE99","#FFFFAA","#FFFFBB","#FFFFCC","#FFFFDD","#FFFFEE","#FFFFFF","#777777","#666666","#555555","#444444","#333333","#222222","#111111","#000000","#FFEEFF","#FFFFFF"]
text_color=["#000000","#000000","#000000","#000000","#000000","#000000","#000000","#000000","#000000","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FF55FF","#999999"]
def randomSpawn(who):
y = random.randrange(0,4)
x = random.randrange(0,4)
if who[y][x]==0:
who[y][x] = random.choice(sp)
else:
return randomSpawn(who)
def fl(who):
v = False
for i in range(4):
for j in range(4):
if who[i][j]==0:
v = True
return v
def attm(who):
for i in range(4):
for j in range(4):
if who[i][j]<0:
who[i][j]+=1
def draw(who):
if who == p1:
p_rect = (140 + 146 * j, 100 + 70 + 146 * i, 142, 142)
else:
p_rect = (140 + 860 + 146 * j, 100 + 70 + 146 * i, 142, 142)
if who[i][j] == 0:
pygame.draw.rect(screen, "#EEEEAA", p_rect, 0, 5)
else:
if who[i][j] < 0:
pygame.draw.rect(screen, "#888888", p_rect, 0, 5)
text = normalFont.render("%d" % who[i][j], True, "#444444")
else:
if who[i][j] > 262144:
pygame.draw.rect(screen, box_color[19 - 1], p_rect, 0, 5)
else:
pygame.draw.rect(screen, box_color[int(math.log2(who[i][j]) - 1)], p_rect, 0, 5)
if who[i][j] > 262144:
text = normalFont.render("%d" % who[i][j], True, text_color[19 - 1])
elif who[i][j] == 262144:
text = normalFont.render("Max", True, text_color[int(math.log2(who[i][j]) - 1)])
else:
text = normalFont.render("%d" % who[i][j], True, text_color[int(math.log2(who[i][j]) - 1)])
text_r = text.get_rect()
cen = (211+146*j,242+146*i)
if who == p2:
cen = (211 + 860 + 146 * j, 242 + 146 * i)
text_r.center = cen
screen.blit(text, text_r)
def randomAttack(victim,att):
y = random.randrange(0, 4)
x = random.randrange(0, 4)
if victim[y][x] == 0:
victim[y][x] = -att[0]
att.pop(0)
else:
return randomAttack(victim,att)
def banghyang(bv,who,vt):
x = 6
bl = True
for i in range(4):
for j in range(3):
if bv == pygame.K_UP or bv == pygame.K_w:
if who[j][i]!=0:
for k in range(3-j):
if who[j+(k+1)][i] == who[j][i]:
who[j][i]*=2
who[j+(k+1)][i]=0
bl = False
if who[j][i] > 64:
vt.append(math.log2(who[j][i])+x)
break
elif who[j+(k+1)][i] != 0:
break
if bv == pygame.K_DOWN or bv == pygame.K_s:
if who[3-j][i] != 0:
for k in range(j+1,4):
if who[3-k][i] == who[3-j][i]:
who[3-j][i] *= 2
who[3-k][i] = 0
bl = False
if who[3-j][i] > 64:
vt.append(math.log2(who[3-j][i])+x)
break
elif who[3-k][i] != 0:
break
if bv == pygame.K_LEFT or bv == pygame.K_a:
if who[i][j] != 0:
for k in range(3 - j):
if who[i][j + (k + 1)] == who[i][j]:
who[i][j] *= 2
who[i][j + (k + 1)] = 0
bl = False
if who[i][j] > 64:
vt.append(math.log2(who[i][j])+x)
break
elif who[i][j + (k + 1)] != 0:
break
if bv == pygame.K_RIGHT or bv == pygame.K_d:
if who[i][3 - j] != 0:
for k in range(j + 1, 4):
if who[i][3 - k] == who[i][3 - j]:
who[i][3 - j] *= 2
who[i][3 - k] = 0
bl = False
if who[i][3-j] > 64:
vt.append(math.log2(who[i][3-j])+x)
break
elif who[i][3 - k] != 0:
break
if not bl:
attm(who)
for i in range(4):
for j in range(3):
if bv == pygame.K_UP or bv == pygame.K_w:
if who[j][i] == 0:
for k in range(j+1,4):
if who[k][i]!=0:
who[j][i]=who[k][i]
who[k][i]=0
bl = False
break
if bv == pygame.K_DOWN or bv == pygame.K_s:
if who[3-j][i] == 0:
for k in range(j+1, 4):
if who[3-k][i] != 0:
who[3-j][i] = who[3-k][i]
who[3-k][i] = 0
bl = False
break
if bv == pygame.K_LEFT or bv == pygame.K_a:
if who[i][j] == 0:
for k in range(j+1,4):
if who[i][k]!=0:
who[i][j]=who[i][k]
who[i][k]=0
bl = False
break
if bv == pygame.K_RIGHT or bv == pygame.K_d:
if who[i][3-j] == 0:
for k in range(j+1, 4):
if who[i][3-k] != 0:
who[i][3-j] = who[i][3-k]
who[i][3-k] = 0
bl = False
break
return bl
def Lose(who):
v = False
for i in range(4):
for j in range(4):
if j != 3 and (who[i][j] == who[i][j + 1] or who[i][j + 1] == 0):
v = True
if j != 0 and (who[i][j] == who[i][j - 1] or who[i][j - 1] == 0):
v = True
if i != 3 and (who[i][j] == who[i + 1][j] or who[i + 1][j] == 0):
v = True
if i != 0 and (who[i][j] == who[i - 1][j] or who[i - 1][j] == 0):
v = True
return v
for i in range(2):
randomSpawn(p1)
randomSpawn(p2)
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
w = banghyang(event.key,p2,p1att)
if not w:
randomSpawn(p2)
#attm(p2)
if len(p2att) > 0 and fl(p2):
randomAttack(p2, p2att)
if event.key == pygame.K_w or event.key == pygame.K_s or event.key == pygame.K_d or event.key == pygame.K_a:
w1 = banghyang(event.key, p1, p2att)
if not w1:
randomSpawn(p1)
#attm(p1)
if len(p1att) > 0 and fl(p1):
randomAttack(p1,p1att)
screen.fill("#AAAA99")
v = Lose(p1)
v1 = Lose(p2)
# fill the screen with a color to wipe away anything from last frame
pygame.draw.rect(screen,"#FFFFAA",t1,0,10)
pygame.draw.rect(screen,"#FFFFAA",t2,0,10)
for i in range(4):
for j in range(4):
draw(p1)
draw(p2)
# flip() the display to put your work on screen
if not v:
t = pygame.font.SysFont("Lucida Console",250).render("p2 win", True, (0, 0, 0))
t_r = t.get_rect()
t_r.center = (1720/2,920/2)
screen.blit(t, t_r)
if not v1:
t = pygame.font.SysFont("Lucida Console",250).render("p1 win", True, (0, 0, 0))
t_r = t.get_rect()
t_r.center = (1720/2,920/2)
screen.blit(t, t_r)
pygame.display.flip()
clock.tick(60) # limits FPS to 60
pygame.quit()top of page

실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
3y4h795nqub
3y4h795nqub
댓글 0개
좋아요
댓글(0)
더 이상 게시물에 대한 댓글 기능이 지원되지 않습니다. 자세한 사항은 사이트 소유자에게 문의하세요.
bottom of page


