如何解决下面给出的“俄罗斯方块”游戏问题中的问题是什么?
所以基本上当我启动程序时一切看起来都很好,直到我尝试移动图形。
当我尝试移动图形 left
或 right
时,出现以下错误:
"local variable 'j' referenced before assignment".
"'Tetris' object has no attribute 'width'".
代码:
import pygame
import random
colors = [
(0,0),(0,240,240),(240,160),(160,0)
]
class figure:
x = 0
y = 0
figures = [
[[1,5,9,13],[4,6,7]],[[1,2,9],[0,4,6],[1,8],10]],10],[5,7,[2,10,11],[3,6]],[[6,9]],[[4,9]]
]
def __init__(self,x_coord,y_coord):
self.x = x_coord
self.y = y_coord
self.type = random.randint(0,len(self.figures)-1)
self.color = colors[self.type+1]
self.rotation = 0
def image(self):
return self.figures[self.type][self.rotation]
def rotate(self):
self.rotation = (self.rotation + 1) % len(self.figures[self.type])
class Tetris:
height = 0
width = 0
field = []
score = 0
state = 'start'
figure = None
def __init__(self,_height,_width):
self.height = _height
self.width = _width
self.field = []
self.score = 0
self.state = 'start'
for i in range(_height):
new_line = []
for j in range(_width):
new_line.append(0)
self.field.append(new_line)
self.new_figure()
def new_figure(self):
self.figure = figure(3,0)
def go_down(self):
self.figure.y += 1
if self.intersects():
self.figure.y -= 1
self.freeze()
def side(self,dx):
old_x = self.figure.x
edge = False
for i in range(4):
for j in j in range(4):
p = i * 4 + j
if p in self.figure.image():
if j + self.figure.x + dx > self.width -1 or \
j + self.figure.x + dx < 0:
edge = True
if not edge:
self.figure.x += dx
if self.intersects():
self.figures = old_x
def left(self):
self.side(-1)
def right(self):
self.side(+1)
def down(self):
while not self.intersects():
self.figure.y += 1
self.figure.y -= 1
self.freeze()
def rotate(self):
old_rotation = self.figure.rotation
self.figure.rotate()
if self.intersects():
self.figure.rotation = old_rotation
def intersects(self):
intersection = False
for i in range(4):
for j in range(4):
p = i * 4 + j
if p in self.figure.image():
if i + self.figure.y > self.height - 1 or \
i + self.figure.y < 0 or \
self.field[i + self.figure.y][j + self.figure.x] > 0:
intersection = True
return intersection
def freeze(self):
for i in range(4):
for j in range(4):
p = i * 4 + j
if p in self.figure.image():
self.field[i + self.figure.y][j + self.figure.x] = self.figure.type
self.break_lines()
self.new_figure()
if self.intersects():
self.state == 'gameover'
def break_lines(self):
lines = 0
for i in range(1,self.height):
zeros = 0
for j in range(self.wifth):
if self.field[i][j] == 0:
zeros += 1
if zeros == 0:
lines += 1
for i2 in range(i,1,-1):
for j in range(self.width):
self.field[i2][j] = self.field[i2 - 1][j]
self.score += lines ** 2
pygame.init()
screen = pygame.display.set_mode((380,670))
pygame.display.set_caption('Tetris by Calvin')
done = False
fps = 2
clock = pygame.time.Clock()
counter = 0
zoom = 30
game = Tetris(20,10)
pressing_down = False
pressing_left = False
pressing_right = False
BLACK = (0,0)
WHITE = (255,255,255)
GRAY = (128,128,128)
while not done:
if game.state == 'start':
game.go_down()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
game.rotate()
if event.key == pygame.K_DOWN:
pressing_down = True
if event.key == pygame.K_LEFT:
pressing_left = True
if event.key == pygame.K_RIGHT:
pressing_right = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
pressing_down = False
if event.key == pygame.K_LEFT:
pressing_left = False
if event.key == pygame.K_RIGHT:
pressing_right = False
if pressing_down:
game.down()
if pressing_left:
game.left()
if pressing_right:
game.right()
screen.fill(color=WHITE)
for i in range(game.height):
for j in range(game.width):
if game.field[i][j] == 0:
color = GRAY
just_border = 1
else:
color = colors[game.field[i][j]]
just_border = 0
pygame.draw.rect(screen,color,[30+j*zoom,30+i*zoom,zoom,zoom],just_border)
if game.figure is not None:
for i in range(4):
for j in range(4):
p = i * 4 + j
if p in game.figure.image():
pygame.draw.rect(screen,game.figure.color,[30+(j + game.figure.x)*zoom,30+(i+ game.figure.y)*zoom,zoom])
gameover_font = pygame.font.SysFont('Calibri',65,True,False)
text_gameover = gameover_font.render("Game Over!\n Press Esc",(255,215,0))
if game.state == "gameover":
screen.blit(text_gameover,[30,250])
score_font = pygame.font.SysFont('Calibri',25,False)
text_score = score_font.render("score: " + str(game.score),0))
screen.blit(text_score,0])
pygame.display.flip()
clock.tick(fps)
pygame.quit
解决方法
在第 77 行你写了
for j in j in range(4):
代替
for j in range(4):
在第 134 行
self.wifth
应该换成
self.width
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。