반응형
Chat GPT로 만든 Python 테트리스 게임 코드
제대로 동작하진 않지만 실행은 잘됨
import pygame
import random
# Set up the game window
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Tetris')
# Define the colors of the tetrominoes
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255), (255, 255, 255)]
# Define the shapes of the tetrominoes
shapes = [
[
[1, 1],
[1, 1]
],
[
[1, 0],
[1, 0],
[1, 1]
],
[
[0, 1],
[0, 1],
[1, 1]
],
[
[1, 1, 0],
[0, 1, 1]
],
[
[0, 1, 1],
[1, 1, 0]
],
[
[1, 0, 0],
[1, 1, 1]
],
[
[0, 0, 1],
[1, 1, 1]
]
]
# Define the Tetromino class
class Tetromino:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
self.shape = random.choice(shapes)
def draw(self):
# Draw the tetromino
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] == 1:
pygame.draw.rect(screen, self.color, (self.x + j*20, self.y + i*20, 20, 20))
def move(self, dx, dy):
# Move the tetromino
self.x += dx
self.y += dy
def rotate(self):
# Rotate the tetromino
new_shape = []
for i in range(len(self.shape[0])):
new_row = []
for j in range(len(self.shape)):
new_row.append(self.shape[len(self.shape)-j-1][i])
new_shape.append(new_row)
self.shape = new_shape
# Define the main game loop
tetrominoes = []
score = 0
grid = [[0 for x in range(WINDOW_WIDTH // 20)] for y in range(WINDOW_HEIGHT // 20)]
game_over = False
clock = pygame.time.Clock()
while not game_over:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
game_over = True
if event.key == pygame.K_LEFT:
for tetromino in tetrominoes:
tetromino.move(-20, 0)
if event.key == pygame.K_RIGHT:
for tetromino in tetrominoes:
tetromino.move(20, 0)
if event.key == pygame.K_DOWN:
for tetromino in tetrominoes:
tetromino.move(0, 20)
if event.key == pygame.K_UP:
for tetromino in tetrominoes:
tetromino.rotate()
# Add a new tetromino
if len(tetrominoes) == 0:
tetrominoes.append(Tetromino(WINDOW_WIDTH // 2 - 20, 0, random.choice(colors)))
# Move the tetrominoes down
for tetromino in tetrominoes:
tetromino.move(0, 20)
if tetromino.y >= WINDOW_HEIGHT - 20 or grid[(tetromino.y + 20) // 20][tetromino.x // 20] != 0:
# The tetromino has landed
tetromino.move(0, -20)
for i in range(len(tetromino.shape)):
for j in range(len(tetromino.shape[i])):
if tetromino.shape[i][j] == 1:
grid[(tetromino.y + i * 20) // 20][tetromino.x // 20] = tetromino.color
tetrominoes.remove(tetromino)
tetrominoes.append(Tetromino(WINDOW_WIDTH // 2 - 20, 0, random.choice(colors)))
# Check for completed rows
for i in range(len(grid)):
if 0 not in grid[i]:
grid.pop(i)
grid.insert(0, [0 for x in range(WINDOW_WIDTH // 20)])
score += 10
# Draw the screen
screen.fill((0, 0, 0))
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] != 0:
pygame.draw.rect(screen, grid[i][j], (j*20, i*20, 20, 20))
for tetromino in tetrominoes:
tetromino.draw()
pygame.display.update()
# Set the game's clock
clock.tick(10)
# End the game
pygame.quit()
반응형
'[====== Development ======] > Python' 카테고리의 다른 글
Python과 AWS S3연동하기 (0) | 2023.10.31 |
---|---|
파이썬에서 설정값 관리하기 (0) | 2023.09.22 |
Python 으로 테슬라 주가 그래프 그리기 (0) | 2023.02.16 |
[Python] OpenCV로 할 수 있는 것들 (0) | 2022.12.20 |
Flask CORS(Cross Origin Resource Sharing) 설정 추가 (0) | 2022.08.12 |