python-3.x - 俄罗斯方块 - 右侧碰撞闪烁问题
问题描述
我是一名高中生,正在做俄罗斯方块的总结性项目。最近我遇到了这个问题,对于某些位置,当我的方块与屏幕的右侧碰撞时,它有点“闪烁”。我似乎找不到它这样做的原因,而且我不知道形状是否超出网格并进入并再次被绘制,我不确定并且很困惑。
到目前为止,我已经能够在第一个块落地后随机生成块,我已经能够正确地对我的所有块与左侧进行碰撞,一些堆叠和旋转也可以。我已经包含了我如何编写代码的主要思想,因为其中很多都是重复的,所以我每个都包含了 2 个示例。(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
解决方案
事实证明,在检查其中一些矩形是否与墙壁发生碰撞之前,我在绘制循环中绘制了一些矩形。
推荐阅读
- c# - WPF 绑定到用户控件中的组合框
- python - Unity IronPython ImportException:没有名为 __future__ 的模块
- rest - 尝试 api 时 Swagger 集线器返回 504
- reactjs - 如何防止用户从 Django REST 中的多个设备登录?
- python - Python 服务器回复
- sentry - Sentry 集成未显示在指标警报中
- python - Python 键错误:1
- python - Django 在 InlineFormset_Factory 中访问外键表的数据
- android - 如果从家中删除小部件时应用程序被杀死+崩溃,则Android小部件监听系统时钟不会更新
- c++ - 从 C++ 派生类的构造函数修改基类的数据成员