初学者です。
Pythonでテトリスを作っています。
できていること
画面上にミノをランダムに表示させる
やりたいこと
キー入力を受け付けて、左右と下にミノを移動させたい
問題点
・左と下には自然に移動するが、右にはマスを飛び越えて移動する
・壁にぶつかった時、ミノの形が変わってしまう
以上の2点、特に上の問題について解決策を教えていただきたいです。
import pygame import sys import random BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) BLUE = (0, 0, 255) GREEN = (0, 128, 0) AQUA = (0, 255, 255) ORANGE = (255, 110, 0) PURPLE = (128, 0, 128) YELLOW = (255, 255, 0) SIZE = 30 #1マスの大きさ BOARD =[]#盤面を管理するリスト COLOR = [RED,GREEN,PURPLE,BLUE,ORANGE,AQUA, YELLOW] def board():#盤面を定義する関数 global BOARD BOARD = [[0 for i in range(11)] for j in range(21)] for i in range(20): BOARD[i][0] = 1 BOARD[i][10] = 1 BOARD[20] = [1,1,1,1,1,1,1,1,1,1,1,1] def set_mino():#ミノを配置する関数 global BOARD rand = random.randint(2,8) if rand == 2: BOARD[0][4] = 2 BOARD[0][5] = 2 BOARD[1][5] = 2 BOARD[1][6] = 2 elif rand == 3: BOARD[0][5] = 3 BOARD[0][6] = 3 BOARD[1][4] = 3 BOARD[1][5] = 3 elif rand == 4: BOARD[0][5] = 4 BOARD[1][4] = 4 BOARD[1][5] = 4 BOARD[1][6] = 4 elif rand == 5: BOARD[0][4] = 5 BOARD[1][4] = 5 BOARD[1][5] = 5 BOARD[1][6] = 5 elif rand == 6: BOARD[0][6] = 6 BOARD[1][4] = 6 BOARD[1][5] = 6 BOARD[1][6] = 6 elif rand == 7: BOARD[0][5] = 7 BOARD[1][5] = 7 BOARD[2][5] = 7 BOARD[3][5] = 7 elif rand == 8: BOARD[0][5] = 8 BOARD[0][6] = 8 BOARD[1][5] = 8 BOARD[1][6] = 8 def draw(sc):#盤面を描く関数 global BOARD,SIZE,COLOR for y in range(21): for x in range(11): if 2 <= BOARD[y][x] <= 8 : pygame.draw.rect(sc, COLOR[(BOARD[y][x])-2], [x * SIZE,y * SIZE,SIZE - 1,SIZE - 1]) def move_mino():#ミノを操作する関数 global BOARD key = pygame.key.get_pressed() for y in range(20, -1, -1): for x in range(11): if 2 <= BOARD[y][x] <= 8 : if key[pygame.K_DOWN] == 1: if BOARD[y + 1][x] == 0: BOARD[y + 1][x] = BOARD[y][x] BOARD[y][x] = 0 if key[pygame.K_RIGHT] == 1: if BOARD[y][x + 1] == 0: BOARD[y][x + 1] = BOARD[y][x] BOARD[y][x] = 0 if key[pygame.K_LEFT] == 1: if BOARD[y][x - 1] == 0: BOARD[y][x - 1] = BOARD[y][x] BOARD[y][x] = 0 def main(): pygame.init() pygame.display.set_caption("tetris") screen = pygame.display.set_mode((11 * SIZE, 20 * SIZE)) clock = pygame.time.Clock() board() set_mino() while True: screen.fill(BLACK) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() move_mino() draw(screen) pygame.display.update() clock.tick(20) if __name__ == '__main__': main()

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。