一、下载插件
在setting设置中找到Plugins,下载插件,要点apply应用按钮。
二、相关设置
重启idea,在右边可以看到chatGPT图标
此时还不能使用,输入问题后显示下图右下角的内容,提示缺少token,
下面打开设置中找到Tools,找到chatGPT
点击右侧的get token按钮
这是OpenAI 官方提供的源。反正需要一个openai账号,所有的记录和官网是会同步的,但是使用官网需要科学上网,直接在软件里使用就不用科学上网,方便了很多。
点击第一个链接跳转到chatgpt官网,
点击第二个链接跳转到获取token的网页
将下面的
token复制到输入框点击apply即可
接下来就可以用了!
下面是让他写的一个飞机大战的例子:很流畅
import pygameimport random# 初始化游戏pygame.init()# 窗口大小screen_width = 400screen_height = 800# 创建窗口screen = pygame.display.set_mode((screen_width, screen_height))pygame.display.set_caption("飞机大战")# 颜色white = (255, 255, 255)# 飞机图片player_img = pygame.image.load("player.png")player_width = 50player_height = 50# 敌机图片enemy_img = pygame.image.load("enemy.png")enemy_width = 50enemy_height = 50# 子弹图片bullet_img = pygame.image.load("bullet.png")bullet_width = 10bullet_height = 30# 音效# bullet_sound = pygame.mixer.Sound("bullet.wav")# explosion_sound = pygame.mixer.Sound("explosion.wav")# 飞机类class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.transform.scale(player_img, (player_width, player_height)) self.rect = self.image.get_rect() self.rect.centerx = screen_width // 2 self.rect.bottom = screen_height - 10 self.speed_x = 0 def update(self): self.rect.x += self.speed_x if self.rect.left < 0: self.rect.left = 0 if self.rect.right > screen_width: self.rect.right = screen_width def shoot(self): bullet = Bullet(self.rect.centerx, self.rect.top) all_sprites.add(bullet) bullets.add(bullet) # bullet_sound.play()# 敌机类class Enemy(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.transform.scale(enemy_img, (enemy_width, enemy_height)) self.rect = self.image.get_rect() self.rect.x = random.randint(0, screen_width - enemy_width) self.rect.y = random.randint(-100, -enemy_height) self.speed_y = random.randint(1, 3) def update(self): self.rect.y += self.speed_y if self.rect.top > screen_height: self.rect.x = random.randint(0, screen_width - enemy_width) self.rect.y = random.randint(-100, -enemy_height) self.speed_y = random.randint(1, 3)# 子弹类class Bullet(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) self.image = pygame.transform.scale(bullet_img, (bullet_width, bullet_height)) self.rect = self.image.get_rect() self.rect.centerx = x self.rect.bottom = y self.speed_y = -10 def update(self): self.rect.y += self.speed_y if self.rect.bottom < 0: self.kill()# 创建精灵组all_sprites = pygame.sprite.Group()enemies = pygame.sprite.Group()bullets = pygame.sprite.Group()# 创建玩家飞机player = Player()all_sprites.add(player)# 创建敌机for _ in range(10): enemy = Enemy() all_sprites.add(enemy) enemies.add(enemy)# 游戏循环running = Trueclock = pygame.time.Clock()while running: clock.tick(60) # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player.shoot() # 获取键盘按键状态 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player.speed_x = -5 elif keys[pygame.K_RIGHT]: player.speed_x = 5 else: player.speed_x = 0 # 更新游戏状态 all_sprites.update() # 检测子弹是否击中敌机 hits = pygame.sprite.groupcollide(enemies, bullets, True, True) for hit in hits: # explosion_sound.play() enemy = Enemy() all_sprites.add(enemy) enemies.add(enemy) # 检测敌机是否碰撞玩家飞机 hits = pygame.sprite.spritecollide(player, enemies, False) if hits: running = False # 绘制游戏界面 screen.fill(white) all_sprites.draw(screen) pygame.display.flip()# 退出游戏pygame.quit()
来源地址:https://blog.csdn.net/weixin_45897172/article/details/131037920