我的世界python俄罗斯方块_python和pygame实现简单俄罗斯方块游戏

本文详细介绍了如何使用Python的pygame库创建俄罗斯方块游戏,包括各种形状的生成、旋转、移动等核心功能的实现代码。通过示例代码帮助读者理解游戏逻辑和pygame的使用。
摘要由CSDN通过智能技术生成

本文为大家分享了python实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下

Github:Tetris

代码:

# -*- coding:utf-8 -*-

import pygame, sys, random, copy

from pygame.locals import *

pygame.init()

CubeWidth = 40

CubeHeight = 40

Column = 10

Row = 20

ScreenWidth = CubeWidth * (Column + 5)

ScreenHeight = CubeHeight * Row

ScreenSize = (ScreenWidth, ScreenHeight)

Screen = pygame.display.set_mode(ScreenSize, 0, 32)

pygame.display.set_caption("Ly's Tetris")

pygame.mixer.music.load('BackgroundMusic.ogg')

pygame.mixer.music.play(-1, 0.0)

ClickMusic = pygame.mixer.Sound('ClickMusic.wav')

ExplodeMusic = pygame.mixer.Sound('Explode.wav')

BackgroundImg = pygame.image.load('BackgroundImg.png').convert()

PreImg = pygame.image.load('PreImg.png').convert()

PStartImg = pygame.image.load('PStartImg.png').convert()

ResultPreImg = pygame.image.load('GameResultPreBgImg.png').convert()

RestartImg = pygame.image.load('GameResultRestBgImg.png').convert()

ScoreHintFont = pygame.font.SysFont('arial', 50)

ScoreFont = pygame.font.SysFont('arial', 40)

ResultFont = pygame.font.SysFont('arial', 200)

Aquamarine = (127, 255, 212)

LightGoldenrod = (255, 236, 139)

IndianRed = (255, 106, 106)

DarkOrchid = (153, 50, 204)

RoyalBlue = (72, 118, 255)

DarkOrange = (255, 165, 0)

Turquoise = (0, 245, 255)

IsRect = []

FPSClock = pygame.time.Clock()

class I():

def __init__(self):

self.Statu = ''

self.Color = Aquamarine

self.Body = []

x = random.randint(1, 2)

if x == 1:

self.Statu = 'upright'

for i in range(4):

InitBody = pygame.Rect(160, i * 40, 40, 40)

self.Body.append(InitBody)

elif x == 2:

self.Statu = 'horizon'

for i in range(4):

InitBody = pygame.Rect(120 + i * 40, 0, 40, 40)

self.Body.append(InitBody)

def Fall(self):

for rect in self.Body:

rect.top += 40

def IsFalled(self):

for rect in self.Body:

if rect.top == 760:

return True

if IsRect[int(rect.top / 40) + 1][int(rect.left / 40)]:

return True

def Move(self, Curkey):

CanMoveFlag = True

if Curkey == K_UP:

self.Rotate()

elif Curkey == K_LEFT:

for rect in self.Body:

if rect.left == 0:

CanMoveFlag = not CanMoveFlag

break

elif IsRect[int(rect.top / 40) + 1][int(rect.left / 40) - 1]:

CanMoveFlag = not CanMoveFlag

break

if CanMoveFlag:

for rect in self.Body:

rect.left -= 40

elif Curkey == K_RIGHT:

for rect in self.Body:

if rect.left == 360:

CanMoveFlag = not CanMoveFlag

break

if IsRect[int(rect.top / 40) + 1][int(rect.left / 40) + 1]:

CanMoveFlag = not CanMoveFlag

break

if CanMoveFlag:

for rect in self.Body:

rect.left += 40

def Rotate(self):

if self.Statu == 'upright':

TempRotate = copy.deepcopy(self.Body)

TempRotate[0].left -= 40

TempRotate[0].top += 40

TempRotate[2].left += 40

TempRotate[2].top -= 40

TempRotate[3].left += 40 * 2

TempRotate[3].top -= 40 * 2

IsRotate = True

if TempRotate[0].left < 0:

IsRotate = False

if TempRotate[3].left > 360:

IsRotate = False

if IsRotate:

for rect in TempRotate:

if IsRect[int(rect.top / 40) + 1][int(rect.left / 40)]:

IsRotate = False

break

if IsRotate:

self.Body = copy.deepcopy(TempRotate)

self.Statu = 'horizon'

else:

TempRotate = copy.deepcopy(self.Body)

TempRotate[0].left += 40

TempRotate[0].top -= 40

TempRotate[2].left -= 40

TempRotate[2].top += 40

TempRotate[3].left -= 40 * 2

TempRotate[3].top += 40 * 2

IsRotate = True

if TempRotate[0].top < 0:

IsRotate = False

if TempRotate[3].top > 760:

IsRotate = False

if IsRotate:

for rect in TempRotate:

if IsRect[int(rect.top / 40) + 1][int(rect.left / 40)]:

IsRotate = False

break

if IsRotate:

self.Body = copy.deepcopy(TempRotate)

self.Statu = 'upright'

class O():

def __init__(self):

self.Color = LightGoldenrod

self.Body = []

for i in range(2):

InitBody = pygame.Rect(160, i * 40, 40, 40)

self.Body.append(InitBody)

for i in range(2):

InitBody = pygame.Rect(200, i * 40, 40, 40)

self.Body.append(InitBody)

def Fall(self):

for rect in self.Body:

rect.top += 40

def IsFalled(self):

for rect in self.Body:

if rect.top == 760:

return True

if IsRect[int(rect.top / 40) + 1][int(rect.left / 40)]:

return True

def Move(self, Curkey):

CanMoveFlag = True

if Curkey == K_UP:

self.Rotate()

elif Curkey == K_LEFT:

for rect in self.Body:

if rect.left == 0:

CanMoveFlag = not CanMoveFlag

break

elif IsRect[int(rect.top / 40) + 1][int(rect.left / 40) - 1]:

CanMoveFlag = not CanMoveFlag

break

if CanMoveFlag:

for rect in self.Body:

rect.left -= 40

elif Curkey == K_RIGHT:

for rect in self.Body:

if rect.left == 360:

CanMoveFlag = not CanMoveFlag

break

if IsRect[int(rect.top / 40) + 1][int(rect.left / 40) + 1]:

CanMoveFlag = not CanMoveFlag

break

if CanMoveFlag:

for rect in self.Body:

rect.left += 40

def Rotate(self):

pass

class T():

def __init__(

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值