俄罗斯方块 - 右侧碰撞闪烁问题

时间:2018-06-03 00:09:54

标签: python-3.x pygame

我是高中的学生,在俄罗斯方块进行总结性项目。最近我遇到了这个问题,对于某些位置,当我的块与屏幕右侧发生碰撞时,它会发生一些“闪烁”。我似乎无法找到它为什么会这样做的原因,而且我不知道这个形状是否已经走出网格然后进入并再次被吸引,我不确定很困惑。

到目前为止,我已经能够在第一个撞击地面后产生随机块,我已经能够正确地与左侧碰撞所有块,一些堆叠和旋转工作同样。我已经包含了我如何编写代码的主要思想,因为很多内容都重复了,所以我包含了两个例子。 (2种不同的旋转,2种方式显示我如何绘制块等)

我真的陷入困境,如果有人可以提供帮助那就太棒了,谢谢你。

    import pygame
    import colors
    import random

    class Shape:

        def __init__ (self, x, y, shape1, shape2, shape3, shape4, shapeType):
            self.shape1 = shape1
            self.shape2 = shape2
            self.shape3 = shape3
            self.shape4 = shape4
            self.x = x
            self.y = y
            self.lasty = 0
            self.direction = 0
            self.collided = False
            self.fc = 0
            self.shapeType = shapeType

        def draw (self, screen):
            self.lasty = self.y
            self.fc += 1
            if pygame.key.get_pressed()[pygame.K_DOWN] and self.fc >= 5:
                self.fc = 0
                self.y += 39
            elif self.fc >= 90:
                self.fc = 0
                self.y += 39

            if pygame.key.get_pressed()[pygame.K_RIGHT] and self.fc >= 5:
                self.fc = 0
                self.x += 39


            elif self.fc >= 90:
                self.fc = 0
                self.x += 39

            ## When the block collides with the bottom of the grid
            if self.y >= 778:
                self.y = 0

## I USE 'DIRECTION' TO REPRESENT THE DIFFERENT ROTATED POSITIONS -- ONLY 1 IS SHOWN BELOW BUT I HAVE (0-3 DIRECTIONS) = 4 DIFFERENT ROTATIONS
            if self.direction == 0: # if the shape is in the first position
                for y in range(len(self.shape1)): # RUNS A LOOP THAT GETS THE LENGTH OF THE SHAPE IN THE FIRST POSITION
                    for x in range(len(self.shape1[y])):
                        if self.shape1[y][x] == 1 and not self.collided:
                            if (self.y + 39*y) + 39 > 780: # CHECKS THAT THE SHAPE IS NOT GOING PASSED THE RIGHT SIDE OF THE GRID
                                self.collided = True
                            if (self.x + (39*x)) >= 739:
                                self.x = 740-(39*x)-39
                            for blok in blocks: ## stacking the blocks and checking if the block collides with the other blocks
                                if self.x + (39*x) == blok.x and self.y + (39*y) == blok.y:
                                    self.collided = True
                            pygame.draw.rect(screen, colors.lightBlue,(self.x + (39*x), self.y + (39*y), 39,39))

            elif self.direction == 1: # WHEN THE SHAPE IS IN THE SECOND ROTATION
                for y in range(len(self.shape2)):
                    for x in range(len(self.shape2[y])):
                        if self.shape2[y][x] == 1 and not self.collided:
                            if(self.y + 39*y) + 39 > 780:
                                self.collided = True
                            if (self.x + (39*x)) >= 739:
                                self.x = 740-(39*x)-39
                            for blok in blocks:
                                if self.x + (39*x) == blok.x and self.y + (39*y) == blok.y:
                                    self.collided = True
                            pygame.draw.rect(screen, colors.lightBlue,(self.x + (39*x), self.y + (39*y), 39,39))

            if self.collided == True:
                self.y = self.lasty
                if self.direction == 0:
                    for y in range(len(self.shape1)):
                        for x in range(len(self.shape1[y])):
                            if self.shape1[y][x] == 1:
                                blocks.append(Block(self.x + (39 * x), self.y + (39 * y)))

                elif self.direction == 1:
                    for y in range(len(self.shape2)):
                        for x in range(len(self.shape2[y])):
                            if self.shape2[y][x] == 1:
                                blocks.append(Block(self.x + (39 * x), self.y + (39 * y)))

    class Block:
        def __init__(self, x, y):
            self.x = x
            self.y = y

        def draw(self, screen):
            pygame.draw.rect(screen, colors.lightBlue, (self.x, self.y, 39, 39))

    blocks = []
    ## EXAMPLE OF HOW EACH SHAPE IS DRAWN

    ## L SHAPE
    b = Shape(350,0, [[1,1],[0,1],[0,1]], [[0,0,1],[1,1,1]], [[1,0,],[1,0], [1,1]], [[1,1,1],[1,0,0]],"L SHAPE")

    ## Z SHAPE
    ##b = Shape(300,300, [[0,1],[1,1],[1,0]], [[1,1],[0,1,1]], [[0,1],[1,1],[1,0]], [[1,1],[0,1,1]])

# FUNCTION FOR THE GRID
    def drawGrid(_x, _y, screen,width,height, columns,rows ):
        for y in range(0, rows+1):
            for x in range(0, columns+1):
                screen.fill(colors.grey, [(x*(width/columns))+_x, _y, 1, height])
            screen.fill(colors.grey, [_x, (y*(height/rows))+_y, width, 1])

    def drawGame(screen, scene): # UPDATING DRAW FUNCTION
        global b
        for evn in pygame.event.get():
            if evn.type == pygame.QUIT:
                quit()
                return scene, True

            elif evn.type == pygame.KEYDOWN: 
                if evn.key == pygame.K_UP: # IF THE UP ARROW KEY IS PRESSED, CHANGE THE ROTATION OF THE SHAPE
                    b.direction += 1
                    if b.direction == 4: # IF THE DIRECTION EQUALS 4 RESET TO THE FIRST POSITION
                        b.direction = 0
        screen.fill(colors.black)

    ## ACCELERATES THE BLOCK DOWNWARDS

        drawGrid(350, 0, screen, 390, 780, 10, 20)
        if not b.collided:
            b.draw(screen)
        else:  # CHECKS WHICH VALUE OUT OF (0-6) IS RECEIVED TO CHOOSE THE RANDOM SHAPE
            i = random.randint(0,6)  
            if i == 0: 
                ## L shape
                b = Shape(350,0, [[1,1],[0,1],[0,1]], [[0,0,1],[1,1,1]], [[1,0,],[1,0], [1,1]], [[1,1,1],[1,0,0]],"L SHAPE")

            elif i == 5:
                ## Z Shape
                b = Shape(350, 0, [[0, 1], [1, 1], [1, 0]], [[1, 1], [0, 1, 1]], [[0, 1], [1, 1], [1, 0]],
                      [[1, 1], [0, 1, 1]],"Z SHAPE")

        for i in blocks: # RUNS THE LOOP TO DRAW THE SHAPE
            i.draw(screen) #  DRAWS THE VALUE OF 'i' ONTO THE SCREEN

        pygame.display.flip()
        return scene, False

1 个答案:

答案 0 :(得分:0)

事实证明,我正在绘制一些绘制循环中的矩形,然后检查其中一些是否与墙壁发生碰撞。

相关问题