37,721
社区成员
发帖
与我相关
我的任务
分享
#这样清零后再赋值就有问题
for i in range(RowNum):
ActiveList[i]=ZeroList
ActiveList[0][5]=1
#结果:
"""
[[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0]]
"""
#只有这样才可以
for i in range(RowNum):
for j in range(ColumnNum):
ActiveList[i][j]=0
ActiveList[0][5]=1
#结果:
"""
[[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
"""
# -*- coding: utf-8 -*-
import pygame
import sys
import random
import time
#游戏框行列数
RowNum=18
ColumnNum=10
GridSize=20
#整个窗口大小
ScreenWidth=(ColumnNum+4)*GridSize+20
ScreenHeight=RowNum*GridSize
#游戏,提示窗口大小,位置,背景
LeftBox=pygame.Rect(0,0,GridSize*ColumnNum,GridSize*RowNum)
LeftbgColor=(255,255,0)
LeftfgColor=(255,0,0)
NextBox=pygame.Rect(GridSize*ColumnNum+10,10,GridSize*4,GridSize*4)
NextbgColor=(0,0,0)
#方块存储
SquareList=[[0 for a in range(ColumnNum)] for b in range(RowNum)]
ActiveList=[[0 for a in range(ColumnNum)] for b in range(RowNum)]
CheckList=[1 for a in range(ColumnNum)]
ZeroList=[0 for a in range(ColumnNum)]
print CheckList,'\n',ZeroList
TimeSleep=0.5
class Game():
def __init__(self):
#主窗口,标题
self.screen=pygame.display.set_mode((ScreenWidth,ScreenHeight))
pygame.display.set_caption('俄罗斯方块(pygame版)')
self.background=pygame.Surface(self.screen.get_size()).convert()
#主循环
def mainloop(self):
#背景
self.screen.fill((255,255,255))
#画主游戏框
pygame.draw.rect(self.screen,LeftbgColor,LeftBox)
#画下一个游戏框
pygame.draw.rect(self.screen,NextbgColor,NextBox)
pygame.display.flip()
self.GameMoveable=True
self.RndFirst()
self.DisplayGame()
#判断循环退出
isRunning=True
while isRunning:
for event in pygame.event.get():
if event.type==pygame.QUIT:
isRunning=False
if event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE:
isRunning=False
if event.type==pygame.KEYDOWN and event.key==pygame.K_UP:
self.Up()
if event.type==pygame.KEYDOWN and event.key==pygame.K_DOWN:
self.Down()
if event.type==pygame.KEYDOWN and event.key==pygame.K_LEFT:
self.Left()
if event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT:
self.Right()
time.sleep(TimeSleep)
self.Down()
#退出循环
pygame.quit()
#显示
def DisplayGame(self):
pygame.draw.rect(self.screen,LeftbgColor,LeftBox)
pygame.display.flip()
for i in range(RowNum):
for j in range(ColumnNum):
if SquareList[i][j]==1:
pygame.draw.rect(self.screen,LeftfgColor,(j*GridSize,i*GridSize,GridSize-2,GridSize-2))
pygame.display.flip()
#检查消行
def CheckFull(self):
for i in range(RowNum):
if SquareList[i]==CheckList:
for j in range(i,0,-1):
SquareList[j]=SquareList[j-1]
SquareList[0]=ZeroList
self.DisplayGame()
#变形
def Up(self):
#print 'Up'
pass
#下落
def Down(self):
Moveable=True
for i in range(RowNum-2,-1,-1):
for j in range(ColumnNum):
if ActiveList[i][j]==1 and ActiveList[i+1][j]==0 and SquareList[i+1][j]==1:
Moveable=False
if self.GameMoveable:
for i in range(RowNum-2,-1,-1):
for j in range(ColumnNum):
if ActiveList[i][j]==1:
ActiveList[i+1][j]=ActiveList[i][j]
ActiveList[i][j]=0
SquareList[i+1][j]=SquareList[i][j]
SquareList[i][j]=0
self.DisplayGame()
print 'True\nSquareList\n',SquareList,'\nActiveList\n',ActiveList
for j in range(ColumnNum):
if ActiveList[RowNum-1][j]==1:
Moveable=False
if Moveable==False:
for i in range(RowNum):
ActiveList[i]=ZeroList
print 'ZeroActiveList\n',ActiveList
self.CheckFull()
self.RndFirst()
self.DisplayGame()
print 'False\nSquareList\n',SquareList,'\nActiveList\n',ActiveList
#左移
def Left(self):
Moveable=True
for i in range(RowNum):
for j in range(1,ColumnNum):
if ActiveList[i][j]==1 and ActiveList[i][j-1]==0 and SquareList[i][j-1]==1:
Moveable=False
for i in range(RowNum):
if ActiveList[i][0]==1:
Moveable=False
if Moveable:
for i in range(RowNum):
for j in range(1,ColumnNum):
if ActiveList[i][j]==1:
ActiveList[i][j-1]=ActiveList[i][j]
ActiveList[i][j]=0
SquareList[i][j-1]=SquareList[i][j]
SquareList[i][j]=0
self.DisplayGame()
#右移
def Right(self):
Moveable=True
for i in range(RowNum):
for j in range(ColumnNum-2,-1,-1):
if ActiveList[i][j]==1 and ActiveList[i][j+1]==0 and SquareList[i][j+1]==1:
Moveable=False
for i in range(RowNum):
if ActiveList[i][ColumnNum-1]==1:
Moveable=False
if Moveable:
for i in range(RowNum):
for j in range(ColumnNum-2,-1,-1):
if ActiveList[i][j]==1:
ActiveList[i][j+1]=ActiveList[i][j]
ActiveList[i][j]=0
SquareList[i][j+1]=SquareList[i][j]
SquareList[i][j]=0
self.DisplayGame()
def RndFirst(self):
print 'Rnd'
self.DisplayGame()
self.SetActive(0,5)
def SetActive(self,i,j):
print 'SetActive\n',ActiveList
ActiveList[i][j]=1#不正常
print SquareList,'\n',ActiveList
SquareList[i][j]=1#正常
print 'SetActive OK\n',ActiveList,'\nSquare\n',SquareList
def SetSqure(self,i,j):
pass
def main():
g=Game()
g.mainloop()
if __name__=='__main__':
main()