AI代碼創(chuàng)作
GPT生成的俄羅斯方塊代碼,稍微做了一些修改,可以運(yùn)行,但效果有些奇怪
import pygame
import random
# 初始化游戲
pygame.init()
# 游戲區(qū)域大小
screen_width = 800
screen_height = 600
# 顏色定義
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 創(chuàng)建游戲區(qū)域
screen = pygame.display.set_mode([screen_width, screen_height])
# 設(shè)置游戲標(biāo)題
pygame.display.set_caption("俄羅斯方塊")
# 方塊大小
block_size = 20
# 方塊形狀
shapes = [
? ? [[1, 1, 1], [0, 1, 0]],
? ? [[0, 2, 2], [2, 2, 0]],
? ? [[3, 3, 0], [0, 3, 3]],
? ? [[4, 0, 0], [4, 4, 4]],
? ? [[0, 0, 5], [5, 5, 5]],
? ? [[6, 6, 6, 6]],
? ? [[7, 7], [7, 7]]
]
# 方塊顏色
colors = [
? ? WHITE,
? ? RED,
? ? GREEN,
? ? BLUE,
? ? (255, 255, 0),
? ? (255, 0, 255),
? ? (0, 255, 255)
]
# 創(chuàng)建方塊類
class Block:
? ? def __init__(self,x,y,shape):
? ? ? ? self.x = x
? ? ? ? self.y = y
? ? ? ? self.shape = shape
? ? ? ? self.color = random.choice(colors)
? ? # 旋轉(zhuǎn)方塊
? ? def rotate(self):
? ? ? ? self.shape = list(zip(*self.shape[::-1]))
? ? # 繪制方塊
? ? def draw(self):
? ? ? ? for i in range(len(self.shape)):
? ? ? ? ? ? for j in range(len(self.shape[i])):
? ? ? ? ? ? ? ? if self.shape[i][j] > 0:
? ? ? ? ? ? ? ? ? ? pygame.draw.rect(screen,self.color,[self.x+j*block_size,self.y+i*block_size,block_size,block_size])
? ?
? ? #移動(dòng)方塊
? ? def move(self,dx,dy):
? ? ? ? self.x+=dx
? ? ? ? self.y+=dy
# 創(chuàng)建游戲類
class Game:
? ? def __init__(self):
? ? ? ? self.score = 0
? ? ? ? self.level = 1
? ? ? ? self.blocks = []
? ? ? ? self.current_block = None
? ? ? ? self.next_block = Block(screen_width//2-block_size,0,random.choice(shapes))
? ? # 創(chuàng)建新方塊
? ? def new_block(self):
? ? ? ? self.current_block = self.next_block
? ? ? ? self.next_block = Block(screen_width//2-block_size,0,random.choice(shapes))
? ? ? ? if self.check_collision(self.current_block):
? ? ? ? ? ? self.game_over()
? ? # 繪制游戲界面
? ? def draw(self):
? ? ? ? screen.fill(BLACK)
? ? ? ? self.draw_score()
? ? ? ? self.draw_level()
? ? ? ? self.current_block.draw()
? ? ? ? self.next_block.draw()
? ? ? ? for block in self.blocks:
? ? ? ? ? ? block.draw()
? ? # 繪制得分
? ? def draw_score(self):
? ? ? ? font = pygame.font.SysFont(None, 30)
? ? ? ? text = font.render("Score: " + str(self.score), True, WHITE)
? ? ? ? screen.blit(text, (10, 10))
? ? # 繪制等級(jí)
? ? def draw_level(self):
? ? ? ? font = pygame.font.SysFont(None, 30)
? ? ? ? text = font.render("Level: " + str(self.level), True, WHITE)
? ? ? ? screen.blit(text, (10, 40))
? ? # 檢查方塊是否與其他方塊碰撞
? ? def check_collision(self, block):
? ? ? ? for b in self.blocks:
? ? ? ? ? ? if block.x == b.x and block.y == b.y:
? ? ? ? ? ? ? ? return True
? ? ? ? ? ? for i in range(len(block.shape)):
? ? ? ? ? ? ? ? for j in range(len(block.shape[i])):
? ? ? ? ? ? ? ? ? ? if block.shape[i][j] > 0 and b.x + j * block_size == block.x and b.y + i * block_size == block.y:
? ? ? ? ? ? ? ? ? ? ? ? return True
? ? ? ? if block.y > 300:
? ? ? ? ? ? return True
? ? ? ? return False
? ?
? ? # 消除滿行
? ? def remove_full_rows(self):
? ? ? ? full_rows = []
? ? ? ? for i in range(screen_height // block_size):
? ? ? ? ? ? row = [(b.x, b.y) for b in self.blocks if b.y == i * block_size]
? ? ? ? ? ? if len(row) == screen_width // block_size:
? ? ? ? ? ? ? ? full_rows.append(i * block_size)
? ? ? ? for row in full_rows:
? ? ? ? ? ? self.blocks = [b for b in self.blocks if b.y != row]
? ? ? ? ? ? for b in self.blocks:
? ? ? ? ? ? ? ? if b.y < row:
? ? ? ? ? ? ? ? ? ? b.move(0, block_size)
? ? ? ? ? ? self.score += 10
? ? ? ? ? ? if self.score % 100 == 0:
? ? ? ? ? ? ? ? self.level += 1
? ? # 游戲結(jié)束
? ? def game_over(self):
? ? ? ? font = pygame.font.SysFont(None, 50)
? ? ? ? text = font.render("Game Over", True, WHITE)
? ? ? ? screen.blit(text, (screen_width // 2 - text.get_width() // 2, screen_height // 2 - text.get_height() // 2))
? ? ? ? pygame.display.update()
? ? ? ? pygame.time.wait(3000)
? ? ? ? pygame.quit()
? ? ? ? quit()
? ? # 運(yùn)行游戲
? ? def run(self):
? ? ? ? clock = pygame.time.Clock()
? ? ? ? self.new_block()
? ? ? ? while True:
? ? ? ? ? ? for event in pygame.event.get():
? ? ? ? ? ? ? ? if event.type == pygame.QUIT:
? ? ? ? ? ? ? ? ? ? pygame.quit()
? ? ? ? ? ? ? ? ? ? quit()
? ? ? ? ? ? ? ? if event.type == pygame.KEYDOWN:
? ? ? ? ? ? ? ? ? ? if event.key == pygame.K_LEFT:
? ? ? ? ? ? ? ? ? ? ? ? self.current_block.move(-block_size, 0)
? ? ? ? ? ? ? ? ? ? ? ? if self.check_collision(self.current_block):
? ? ? ? ? ? ? ? ? ? ? ? ? ? self.current_block.move(block_size, 0)
? ? ? ? ? ? ? ? ? ? elif event.key == pygame.K_RIGHT:
? ? ? ? ? ? ? ? ? ? ? ? self.current_block.move(block_size, 0)
? ? ? ? ? ? ? ? ? ? ? ? if self.check_collision(self.current_block):
? ? ? ? ? ? ? ? ? ? ? ? ? ? self.current_block.move(-block_size, 0)
? ? ? ? ? ? ? ? ? ? elif event.key == pygame.K_DOWN:
? ? ? ? ? ? ? ? ? ? ? ? self.current_block.move(0, block_size)
? ? ? ? ? ? ? ? ? ? ? ? if self.check_collision(self.current_block):
? ? ? ? ? ? ? ? ? ? ? ? ? ? self.current_block.move(0, -block_size)
? ? ? ? ? ? ? ? ? ? elif event.key == pygame.K_UP:
? ? ? ? ? ? ? ? ? ? ? ? self.current_block.rotate()
? ? ? ? ? ? self.current_block.move(0, block_size)
? ? ? ? ? ? if self.check_collision(self.current_block):
? ? ? ? ? ? ? ? self.current_block.move(0, -block_size)
? ? ? ? ? ? ? ? self.blocks.append(self.current_block)
? ? ? ? ? ? ? ? self.new_block()
? ? ? ? ? ? ? ? self.remove_full_rows()
? ? ? ? ? ? self.draw()
? ? ? ? ? ? pygame.display.update()
? ? ? ? ? ? clock.tick(10 * self.level)
# 創(chuàng)建游戲?qū)ο蟛⑦\(yùn)行游戲
game = Game()
game.run()