首页 > 热点 > > 正文

天天快消息!AI代码创作

时间:2023-05-01 20:57:34 来源:哔哩哔哩

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

关键词:
x 广告
共同抗疫!郑州71岁党员志愿者每天爬6栋楼,提醒800多户居民做核酸

1月4日,郑州市新冠肺炎疫情防控指挥部办公室发布通告对部分区域实施管控措施。这是近半年来,新冠疫情第3次造访郑州。截止至1月10日24时,

安阳疫情最新消息|1月11日8时至12日8时安阳市新增本土确诊病例39例,累计123例

安阳疫情最新消息2022年1月11日8时至12日8时安阳市新型冠状病毒肺炎疫情最新情况1月11日8时至12日8时,全市新增本土确诊病例39例(汤阴县38

迅速高效!“河南抗疫互助通道”网上网下联动 获群众点赞

核酸检测24小时了还没查到结果,怎么办?现在高速下站口的管控政策到底是什么样子的?从洛阳老城区自驾走高速回郑州,收费站还劝返吗?苏八

信阳市疫情防控工作视频调度会召开 陈志伟出席会议

1月11日,市疫情防控工作视频调度会召开。代市长陈志伟出席会议并讲话。听取有关疫情防控重点工作和各县区(管理区、开发区)疫情防控工作

全国最新疫情消息:11日新增本土确诊病例166例 天津市今日启动第二轮全员核酸检测

11日0—24时,31个省(自治区、直辖市)和新疆生产建设兵团报告新增新冠肺炎本土确诊病例166例(河南118例,其中安阳市65例、许昌市41例、

1月12日天津市放假半天 开启第二轮全员核酸检测

1月12日,天津市人民政府发布重要通告,为做好天津市第二轮全员核酸检测工作,全市机关和企事业单位于1月12日(星期三)下午放假半天。广大

x 广告

Copyright ©  2015-2023 港澳评测网版权所有  备案号:京ICP备2023022245号-31   联系邮箱:435 226 40 @qq.com