淘先锋技术网

首页 1 2 3 4 5 6 7

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

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)

在这里插入图片描述