pygame实现简易的2048小游戏

欢迎关注我的公众号【次烦】
(这个程序没有注释,不要问为什么,问就是忘了写,当我想起来后又懒得写了)

2048小游戏

主循环

import pygame
import sys
import random
import game_functions as gf
from time import sleep
from square import Square
from pygame.sprite import Group
def run_game():
	pygame.init()
	screen = pygame.display.set_mode((700,600))
	pygame.display.set_caption("2048")

	pos_check = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
	pos_check[random.randint(0,3)][random.randint(0,3)] = 2 if random.randint(1,3) != 2 else 4

	square = Square(screen)

	global score
	score = 0

	print(pos_check)

	while True:
		gf.check_events(screen,pos_check)
		
		gf.update_screen(square,screen,pos_check,score)
		pass

run_game()

函数

import pygame
import sys
import random
from square import Square


def update_screen(square,screen,pos_check,score):

	screen.fill((230,230,230))

	draw_background(screen)
	draw_squares(square, pos_check)

	draw_score(score,screen,pos_check)
	pygame.display.flip()
	pass

def draw_squares(square,pos_check):
	for i in range(4):
		for j in range(4):
			square.rect.x = (i+1.5)*100
			square.rect.y = (j+1)*100
			if pos_check[j][i] != 0:
				square.image = pygame.image.load('images\\'+str(pos_check[j][i])+'.png')
				square.blitme()
	pass


def check_events(screen,pos_check):
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
		elif event.type == pygame.KEYDOWN:
			if event.key == pygame.K_LEFT:
				check_left(pos_check)
			elif event.key == pygame.K_RIGHT:
				check_right(pos_check)
			elif event.key == pygame.K_UP:
				check_up(pos_check)
			elif event.key == pygame.K_DOWN:
				check_down(pos_check)
			elif event.key == pygame.K_q:
				sys.exit()
			elif event.key == pygame.K_r:
				for i in range(4):
					for j in range(4):
						pos_check[i][j] = 0


			while True:
				pos_x = random.randint(0,3)
				pos_y = random.randint(0,3)
				if pos_check[pos_x][pos_y] == 0:
					pos_check[pos_x][pos_y] = 2 if random.randint(1,4) != 2 else 4
					break


				over = False
				for line in pos_check:
					for i in range(0,4):
						if line[i] == 0:
							over = True
				if not over:
					print('game over')
					break
				

	pass

def draw_background(screen):
	pygame.draw.rect(screen,(100,150,200),(150,100,400,400),0)
	pass

def check_left(pos_check):
	for i in range(0,4):
		ppp = []
		for j in range(0,4):
			if pos_check[i][j] != 0:
				ppp.append(pos_check[i][j])
		if len(ppp) == 4:
			if ppp[0] == ppp[1] and ppp[2] == ppp[3]:
				ppp[0] *= 2
				ppp[2] *= 2
				del ppp[3]
				del ppp[1]
			else:
				if ppp[0] == ppp[1]:
					ppp[0] *= 2
					del ppp[1]
				elif ppp[1] == ppp[2]:
					ppp[1] *= 2
					del ppp[2]
				elif ppp[2] == ppp[3]:
					ppp[2] *= 2
					del ppp[3]
				if ppp[-1] == ppp[-2] and len(ppp) == 4:
					ppp[-2] *= 2
					del ppp[-1]
		elif len(ppp) in (2,3):
			if ppp[0] == ppp[1]:
				ppp[0] *= 2
				del ppp[1]
			elif ppp[-1] == ppp[-2]:
				ppp[-2] *= 2
				del ppp[-1]
			

		while len(ppp) < 4:
			ppp.append(0)
			pass
		pos_check[i] = ppp

def check_right(pos_check):
	for i in range(0,4):
		ppp = []
		for j in range(0,4):
			#删除空位
			if pos_check[i][j] != 0:
				ppp.append(pos_check[i][j])
		if len(ppp) == 4:
			if ppp[0] == ppp[1] and ppp[2] == ppp[3]:
				ppp[0] *= 2
				ppp[2] *= 2
				del ppp[3]
				del ppp[1]
			else:
				if ppp[2] == ppp[3]:
					ppp[2] *= 2
					del ppp[3]
				elif ppp[1] == ppp[2]:
					ppp[1] *= 2
					del ppp[2]
				elif ppp[0] == ppp[1]:
					ppp[0] *= 2
					del ppp[1]
				if ppp[0] == ppp[1] and len(ppp) == 4:
					ppp[0] *= 2
					del ppp[1]
		elif len(ppp) in (2,3):
			if ppp[-1] == ppp[-2]:
				ppp[-1] *= 2
				del ppp[-2]
			elif ppp[0] == ppp[1]:
				ppp[0] *= 2
				del ppp[1]
		

		while len(ppp) < 4:
			ppp.insert(0,0)
		pos_check[i] = ppp

def check_up(pos_check):
	n_pos = []
	for i in range(4):
		n_ppp = []
		for line in pos_check:
			n_ppp.append(line[i])
		n_pos.append(n_ppp)
	#行列颠倒
	check_left(n_pos)
	for i in range(4):
		n_ppp = []
		for line in n_pos:
			n_ppp.append(line[i])
		pos_check[i] = n_ppp

def check_down(pos_check):
	n_pos = []
	for i in range(4):
		n_ppp = []
		for line in pos_check:
			n_ppp.append(line[i])
		n_pos.append(n_ppp)
	#行列颠倒
	check_right(n_pos)
	for i in range(4):
		n_ppp = []
		for line in n_pos:
			n_ppp.append(line[i])
		pos_check[i] = n_ppp
	pass

def draw_score(score,screen,pos_check):
	score = sum(pos_check[0])+sum(pos_check[1])+sum(pos_check[2])+sum(pos_check[3])
	textimage = pygame.font.Font(None,60).render(str(score),True,(55,33,77))
	screen.blit(textimage,(350,520))
	pass

方块

import pygame
from pygame.sprite import Sprite
import random

class Square(Sprite):
	"""docstring for Square"""
	def __init__(self, screen):
		super(Square, self).__init__()
		self.screen = screen


		self.image = pygame.image.load('images\\2.png')
		self.rect = self.image.get_rect()

		self.rect.x = 0
		self.rect.y = 0

	def blitme(self):
		self.screen.blit(self.image,self.rect)

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CI_FAN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值