pygame 写俄罗斯方块问题

自己写的一段 python pygame 俄罗斯方块代码,现在遇到个问题,左右移动时无法判断是否能移动,希望大家帮忙指导下,谢谢

import pygame
from pygame.locals import *
from sys import exit
from pygame.math import Vector2
import random


screen=pygame.display.set_mode((480,640))
n=0
x=120
part_list=[(-100,-100),(-200,-200)]
shape_list=[[(0,-40),(0,0),(-40,0),(-40,40)],
        [(0, -40), (0, 0), (40, 0), (40, 40)],
        [(0, -40), (0, 0), (0, 40), (0, 80)],
        [(-40, 0), (0, 0), (40, 0), (0, 40)],
        [(0, 0), (40, 0), (0, 40), (40, 40)],
        [(-40, -40), (0, -40), (0, 0), (0, 40)],
        [(40, -40), (0, -40), (0, 0), (0, 40)]]
newshape=[]
shape=[]

def run():
    screen.fill((255,255,255))
    clock = pygame.time.Clock()
    y=0
    global n,x,newshape,part_list,shape
    getnewshape()
    while True:
        clock.tick(24)
        for event in pygame.event.get():
            if event.type==QUIT:
                exit()
            if event.type==KEYDOWN:
                if event.key==K_UP:
                    screen.fill((255,255,255))
                    for (i,j) in shape[0:4]:
                        print(i,j)
                        a=i-x
                        i=j+x
                        j=-a
                        print(i,j)
                        shape.append((i,j))
                        j+=gety()
                        pygame.draw.rect(screen,(255,0,0),(i,j,40,40))
                    del shape[0:4]
                if event.key==K_DOWN:
                    while True:
                        n+=1
                        if inside_blank() == False or inside_part_list() == False:
                            for (i, j) in shape:
                                j += gety()
                                part_list.append((i, j))
                            getnewshape()
                            n = 0
                            break
                **if event.key==K_LEFT:
                    screen.fill((255,255,255))
                    for (i,j) in shape[0:4]:
                        i+=-40
                        a=j+gety()
                        if (i,a) in part_list:
                            del shape[5:10]
                            break
                        else:
                            shape.append((i,j))
                            j+=gety()
                            pygame.draw.rect(screen,(255,0,0),(i,j,40,40))
                    if len(shape)==8:
                        del shape[0:4]**
                if event.key==K_RIGHT:
                    screen.fill((255, 255, 255))
                    for (i, j) in shape[0:4]:
                        i += 40
                        shape.append((i, j))
                        j += gety()
                        pygame.draw.rect(screen, (255, 0, 0), (i, j, 40, 40))
                    print(shape)
                    del shape[0:4]
                pygame.time.delay(200)
                pygame.display.update()
        screen.fill((255,255,255))
        checktf()
        draw_part_list()
        pygame.time.delay(200)
        pygame.display.update()
def gety():
    return n*40

def inside_blank():
    for (i,j) in shape:
        j+=gety()+40
        if j>600:
            return False

def inside_part_list():
#    print(shape)
    for (i,j) in shape:
        j+=gety()+40
        if (i,j) in part_list:
            return False

def getnewshape():
    global shape,x
    newshape = random.choice(shape_list)
    x = random.randrange(80, 400, 40)
    del shape[0:10]
    for (i, j) in newshape:
        i += x
        pygame.draw.rect(screen, (255, 0, 0), (i, j, 40, 40))
        shape.append((i,j))
#    print()
def getshapedown():
#    print(shape)
    for (i,j) in shape:
        j+=gety()
        pygame.draw.rect(screen,(255,0,0),(i,j,40,40))
def draw_part_list():
#    print(part_list)
    for (i,j) in part_list:
        pygame.draw.rect(screen,(0,255,0),(i,j,40,40))
def checktf():
    global n,x,newshape,part_list,shape
    if inside_blank() == False or inside_part_list() == False:
        for (i, j) in shape:
            j += gety()
            part_list.append((i, j))
        getnewshape()
        n = 0
    else:
        n+=1
        getshapedown()
run()



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值