方块无法下滑,怎么解决啊??

程序代码:

var tire_rows=20;
var tire_cols=15;
var cell_size =100;
function   test(rows,cols,cellwidth,cellheight){
	
	var table =document.createElement("table");
	for(var i=0;i<rows;i++)
	{
		var row=table.insertRow(i);
		for(var j=0;j<cols;j++)
			{
			   var cell =row.insertCell(j);
			   cell.style.cellwidth+"px";
			   cell.style.cellheight+"px";
			}
	}

	return table;
	
}


var tire_table;
var currentfall =[
                  {x:tire_cols/2-1,y:0},
                  {x:tire_cols/2,y:0},
                  {x:tire_cols/2,y:1},
                  {x:tire_cols/2+1,y:1},
                  
                  ];


 window.onload =function()
 {
	var tire_table= test(tire_rows,tire_cols,800,600);
	 tire_table.border=1;
	 tire_table.style.borderCollapse="collapse"; 
	document.body.appendChild(tire_table);
	
	for(var i=0;i<currentfall.length;i++)
	{
	
	  var cur =currentfall[i];
	  tire_table.rows[cur.y].cells[cur.x].style.backgroundColor ="blue";
	
	}
		
	setInterval(fall,1000);
	
 };



function fall()
{
	
	for(var i=0;i<currentfall.length;i++)
	{	  
	  var cur =currentfall[i];	  
	  tire_table.rows[cur.y].cells[cur.x].style.backgroundColor ="white";
	  
	}
	
	
	var canfall=false;
	
	for(var i=0;i<currentfall.length;i++)
	{
	  var cur =currentfall[i];
	  if(cur.y>=tire_rows-1)
	  {
		  canfall =false;
		  
		  break;
	  }
	}
	

	if (canfall) {
		for ( var i = 0; i < currentfall.length; i++) {
			var cur = currentfall[i];
			cur.y++;
			
		}
	}
	

	
	for(var i=0;i<currentfall.length;i++)
	{
	
	  var cur =currentfall[i];
	  
	  tire_table.rows[cur.y].cells[cur.x].style.backgroundColor ="blue";
	  
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,以下是一个简单的俄罗斯方块的 Python 代码示例: ``` python import pygame import random # 初始化Pygame pygame.init() # 创建游戏窗口 screen_width = 640 screen_height = 480 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("俄罗斯方块") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) # 定义方块大小和格子数 block_size = 20 grid_width = screen_width // block_size grid_height = screen_height // block_size # 定义方块形状 shapes = [ [[1, 1], [1, 1]], [[0, 2, 0], [2, 2, 2]], [[0, 3, 3], [3, 3, 0]], [[4, 4, 0], [0, 4, 4]], [[5, 5, 5, 5]], [[6, 6, 6], [0, 6, 0], [0, 6, 0]], [[0, 7, 0], [7, 7, 7], [0, 7, 0]] ] # 定义方块颜色 colors = [ BLACK, RED, GREEN, BLUE, YELLOW ] # 定义游戏变量 score = 0 grid = [[0] * grid_width for _ in range(grid_height)] cur_shape = random.choice(shapes) cur_color = random.choice(colors) cur_x = grid_width // 2 - len(cur_shape[0]) // 2 cur_y = 0 # 定义游戏函数 def draw_block(x, y, color): pygame.draw.rect(screen, color, (x * block_size, y * block_size, block_size, block_size)) def draw_shape(shape, x, y, color): for i in range(len(shape)): for j in range(len(shape[i])): if shape[i][j] != 0: draw_block(x + j, y + i, color) def check_collision(shape, x, y): for i in range(len(shape)): for j in range(len(shape[i])): if shape[i][j] != 0 and (y + i >= grid_height or x + j < 0 or x + j >= grid_width or grid[y + i][x + j] != 0): return True return False def place_shape(shape, x, y, color): for i in range(len(shape)): for j in range(len(shape[i])): if shape[i][j] != 0: grid[y + i][x + j] = color def remove_lines(): global score lines_removed = 0 for i in range(grid_height): if all(grid[i]): grid.pop(i) grid.insert(0, [0] * grid_width) lines_removed += 1 score += lines_removed ** 2 # 游戏循环 clock = pygame.time.Clock() while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: cur_x -= 1 if check_collision(cur_shape, cur_x, cur_y): cur_x += 1 elif event.key == pygame.K_RIGHT: cur_x += 1 if check_collision(cur_shape, cur_x, cur_y): cur_x -= 1 elif event.key == pygame.K_UP: old_shape = cur_shape cur_shape = [[cur_shape[j][i] for j in range(len(cur_shape))] for i in range(len(cur_shape[0]) - 1, -1, -1)] if check_collision(cur_shape, cur_x, cur_y): cur_shape = old_shape elif event.key == pygame.K_DOWN: cur_y += 1 if check_collision(cur_shape, cur_x, cur_y): cur_y -= 1 # 更新游戏状态 cur_y += 1 if check_collision(cur_shape, cur_x, cur_y): cur_y -= 1 place_shape(cur_shape, cur_x, cur_y, cur_color) remove_lines() cur_shape = random.choice(shapes) cur_color = random.choice(colors) cur_x = grid_width // 2 - len(cur_shape[0]) // 2 cur_y = 0 if check_collision(cur_shape, cur_x, cur_y): pygame.quit() exit() # 绘制游戏界面 screen.fill(WHITE) for i in range(grid_height): for j in range(grid_width): if grid[i][j] != 0: draw_block(j, i, colors[grid[i][j]]) draw_shape(cur_shape, cur_x, cur_y, cur_color) pygame.display.set_caption("俄罗斯方块 - 得分: {}".format(score)) pygame.display.flip() # 控制游戏速度 clock.tick(10) ``` 这是一个非常简单的俄罗斯方块示例,只有基本的游戏逻辑和图形界面。你可以根据自己的需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值