欢迎光临散文网 会员登陆 & 注册

AI代码创作

2023-05-01 21:11 作者:Daylight_aspf  | 我要投稿

GPT生成的俄罗斯方块代码,稍微做了一些修改,可以运行,但效果有些奇怪


import pygame

import random


# 初始化游戏

pygame.init()


# 游戏区域大小

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)


# 创建游戏区域

screen = pygame.display.set_mode([screen_width, screen_height])


# 设置游戏标题

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)

]


# 创建方块类

class Block:

    def __init__(self,x,y,shape):

        self.x = x

        self.y = y

        self.shape = shape

        self.color = random.choice(colors)


    # 旋转方块

    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])

   

    #移动方块

    def move(self,dx,dy):

        self.x+=dx

        self.y+=dy


# 创建游戏类

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))


    # 创建新方块

    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))


    # 绘制等级

    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


    # 游戏结束

    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()


    # 运行游戏

    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)


# 创建游戏对象并运行游戏

game = Game()

game.run()




AI代码创作的评论 (共 条)

分享到微博请遵守国家法律