赞
踩
(因为之前下载过PyCharm就没从下载开始)
首先新建项目,解释器选择已有的、Conda 环境。
不知道为啥显示的是问号文件(不过并不影响),找到Anaconda中的_conda.exe文件。
(没有这个文件的话好像是找在Scripts文件夹里的conda.exe文件。)
这里注意一定要点Load Environments的按钮!我傻了吧唧选了路径没点这里,看左下角标红还以为哪里错了,耽搁了好久。
然后点击确认就可以啦~配置成功~
中间跟着这个教程搞了,能够在pycharm 终端激活conda环境了,虽然还不知道有啥用。
【已解决】pycharm 终端无法激活conda环境_pycharm控制台如何激活环境-CSDN博客
1.在PyCharm终端直接使用pip安装,爆红;
2.在cmd中激活虚拟环境,pip安装,一直time out下载失败。
3.看别的教程里去pygame网站下载,结果那个网站貌似没了,显示not found。
4.在pycharm中点击设置- python interpreter-点一下那个小绿图标-点加号-搜索pygame-下载,成功!(注意假如点➕后搜索一直为空,返回来在点点绿圈圈,然后再试试)
网上找了个烟花代码用来测试:
- #!/usr/bin/python
- # -*- coding: UTF-8 -*-
- import pygame
- import random
- import time # 导入time模块
-
- # 设置窗口尺寸
- WIDTH, HEIGHT = 800, 600
- # 颜色定义
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- YELLOW = (255, 255, 0)
-
- # 初始化pygame
- pygame.init()
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Fireworks")
-
-
- # 定义Firework(烟花)类
- class Firework:
- def __init__(self):
- self.x = random.randint(50, WIDTH - 50)
- self.y = HEIGHT
- self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
- self.speed = random.randint(5, 12)
-
- def move(self):
- self.y -= self.speed
-
- def explode(self, sparks):
- for _ in range(50):
- spark = Spark(self.x, self.y, self.color)
- sparks.append(spark)
-
- def draw(self, screen):
- pygame.draw.circle(screen, self.color, (self.x, self.y), 3)
-
-
- # 定义Spark(火花)类
- class Spark:
- def __init__(self, x, y, color):
- self.x = x
- self.y = y
- self.color = color
- self.speedx = random.uniform(-1, 1) * 8
- self.speedy = random.uniform(-1, 1) * 8
- self.age = 0
-
- def move(self):
- self.x += self.speedx
- self.y += self.speedy
- self.speedy += 0.2
- self.age += 1
-
- def draw(self, screen):
- pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 2)
-
-
- # 主循环
- clock = pygame.time.Clock()
-
- fireworks = []
- sparks = []
-
- running = True
- start_time = pygame.time.get_ticks() # 获取程序开始的时间
- total_run_time = 60 * 3 # 设置程序总运行时间为3分钟
- current_run_time = 0 # 当前程序运行时间
-
- while current_run_time < total_run_time: # 循环条件改为判断当前运行时间是否小于总运行时间
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
-
- screen.fill(BLACK)
-
- # 创建新烟花
- if random.random() < 0.02:
- fireworks.append(Firework())
-
- # 更新和绘制烟花
- for firework in fireworks:
- firework.move()
- firework.draw(screen)
-
- if firework.y < random.randint(50, HEIGHT // 2):
- firework.explode(sparks)
- fireworks.remove(firework)
-
- # 移动和绘制火花
- for spark in sparks:
- spark.move()
- spark.draw(screen)
-
- if spark.age > 100:
- sparks.remove(spark)
-
- pygame.display.flip()
- clock.tick(60)
-
- # 更新程序当前运行时间
- current_run_time = (pygame.time.get_ticks() - start_time) / 1000 # 转换为秒
- time.sleep(0.1) # 添加时间延迟,让程序暂停0.1秒
-
- pygame.quit()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
运行成功!
打开已有项目时,右下角有标记的说明该文件夹被用作PyCharm的项目地址:
查看解释器是不是自己想要的那个:
编辑运行配置:
右上角edit configuration-加号-python,选一下运行的文件就行。
(这个不好用,切换到下个文件之后还是会运行之前的)
然后就是这个样:
或者直接在文件里点击右键,点击运行。(这个能保证运行当前文件)
【老实讲还没搞懂这一步的意思,不配置直接运行也能运行啊,在current file里头。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。