pygame 写俄罗斯方块问题

    技术2023-08-16  79

    自己写的一段 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()
    Processed: 0.011, SQL: 9