感谢分享原文-http://bjbsair.com/2020-04-03/tech-info/30000.htmlhtml
1.说明:python
1.1 推荐指数:★★★★★app
1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。dom
1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。编辑器
1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。函数
1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。动画
1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。设计
人类很眇小,宇宙很美3d
2.本次高级一点点的效果图:code
3.代码分析和注释:
3.1 第1步:
#---第1步---导出模块--- import pygame from pygame.locals import* from sys import exit from random import randint import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置 pygame.init() pygame.display.set_caption("太阳-地球-月亮模拟3d星空") #这个是打开的窗口的大小 width,height = 1800,1400 #设置屏幕分辨率 screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义--- #颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下 #列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法 white = (255,255,255) black=0,0,0 planse=[65,105,225] #定义x和y的屏幕坐标,用于地球和月球的位置设置 screenCenterx = width//2 -1 screenCentery = height//2 -1 #设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可 pygame.mixer.music.load("123.mp3") pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义--- class Star(object): #类 def __init__(self,x,y,speed): #定义坐标和速度 self.x = x self.y = y self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon--- class Ball(): #球的半径,颜色和每次转的角速度 def __init__(self,r,color,speed): self.image = pygame.Surface((2*r,2*r)) pygame.draw.circle(self.image,color,(r,r),r) self.image.set_colorkey((0,0,0)) self.rect = self.image.get_rect() self.speed= speed self.angle = 0 #绕着x,y点以radius为半径旋转 def move(self,x,y,radius): #乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适 self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3) self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3) self.angle = self.angle + self.speed #定义画图函数 def draw(self): screen.blit(self.image,self.rect)
===========================================
第6步:请注意,两种方法,简单游戏不须要name和main
===========================================
3.6 第6步:
#---第6步---运行定义 #def run(): #这是注释掉def run的代码,总体向左移动一格 #地球和月亮的球颜色和大小的设置 #65,105,225=品蓝色;0,0,255=蓝色 #50是地球大小,10是月球大小 #1和5是对应的速度 #earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉 earth = Ball(50,planse,1) moon = Ball(10,(255,255,200),5) #画星星,随机按x和y的大小生成,存入stars的列表中 stars=[] for n in range(2000): #在第一帧画上一些星星 #x和y不可是坐标,仍是画布大小的设置 #初始化生成的星星个数2000,可本身调节 x = float(randint(0,width)) y = float(randint(0,height)) speed = float(randint(10,300)) stars.append(Star(x,y,speed)) clock = pygame.time.Clock() #Clock对象 #while循环 while True: #退出设置,建议固定 for event in pygame.event.get(): if event.type == QUIT: #return exit() #在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update() #---单文件可要可不要,可是也是有道理的,我之前讲过 # 若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试 #这是注释掉def run的代码 #if __name__ == "__main__": #run()
-
简单动画或游戏,是不须要用if name == "main":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。
-
简洁的完整代码以下:
import pygame from pygame.locals import* from sys import exit from random import randint import math pygame.init() pygame.display.set_caption("太阳-地球-月亮模拟3d星空") width,height = 1800,1400 #设置屏幕分辨率 screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起 white = (255,255,255) black=0,0,0 planse=[65,105,225] screenCenterx = width//2 -1 screenCentery = height//2 -1 pygame.mixer.music.load("123.mp3") pygame.mixer.music.play(-1,0.0) class Star(object): #类 def __init__(self,x,y,speed): #定义坐标和速度 self.x = x self.y = y self.speed = speed class Ball(): #球的半径,颜色和每次转的角速度 def __init__(self,r,color,speed): self.image = pygame.Surface((2*r,2*r)) pygame.draw.circle(self.image,color,(r,r),r) self.image.set_colorkey((0,0,0)) self.rect = self.image.get_rect() self.speed= speed self.angle = 0 #绕着x,y点以radius为半径旋转 def move(self,x,y,radius): #乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适 self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3) self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3) self.angle = self.angle + self.speed #定义画图函数 def draw(self): screen.blit(self.image,self.rect) earth = Ball(50,planse,1) moon = Ball(10,(255,255,200),5) #画星星,随机按x和y的大小生成,存入stars的列表中 stars=[] for n in range(2000): #在第一帧画上一些星星 #x和y不可是坐标,仍是画布大小的设置 #初始化生成的星星个数2000,可本身调节 x = float(randint(0,width)) y = float(randint(0,height)) speed = float(randint(10,300)) stars.append(Star(x,y,speed)) clock = pygame.time.Clock() #Clock对象 #while循环 while True: #退出设置,建议固定 for event in pygame.event.get(): if event.type == QUIT: #return exit() #在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update() ```感谢分享原文-http://bjbsair.com/2020-04-03/tech-info/30000.html 1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
1.说明: 1.1 推荐指数:★★★★★ **1.2 理由:上次matplotlib制做太阳-地球-月亮模拟你们很喜欢,此次比上次高级一些,加入3d星空运动,还能够加入背景音乐,因此推荐指数5个星。** 1.3 利用python的相关知识和pygame的动画设计,逐步代码分析,最后有简洁的完整代码。慢慢看,一看就会,通俗易懂。 1.4 推荐:python3(python3.8)、pygame和微软vscode编辑器。 1.5 咱们生活在宇宙中,人类是很眇小,病毒才是人类的共同敌人。好好热爱生活,注意身体,爱护和珍惜身边的人,为祖国目前已经打败疫情而骄傲,也祝世界其余国家早日打败疫情。最后继续遵从终南山、李兰娟、张文宏等教授的建议,作好防御,防止输入性疫情复发,人人有责。 1.6 有点长,慢慢品读,适合收藏和转发,谢谢你们喜欢。  人类很眇小,宇宙很美 2.本次高级一点点的效果图:  3.代码分析和注释: 3.1 第1步:
#---第1步---导出模块---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和标题设置
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
#这个是打开的窗口的大小
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
3.3 第3步:
#---第3步---相关定义---
#颜色定义,颜色能够的单独定义,在pygame中颜色定义能够使以下
#列表法、元组法、直接法,还有引入rgb法,就是不能十六进制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
#定义x和y的屏幕坐标,用于地球和月球的位置设置
screenCenterx = width//2 -1
screenCentery = height//2 -1
#设置背景音效---可要可不要,本身弄一个音乐,放在根目录下或者指定位置引出便可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
3.4 第4步:
#---第4步---星星类的初始化定义---
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定义:sun,earth,moon---
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
=========================================== 第6步:请注意,两种方法,简单游戏不须要name和main =========================================== 3.6 第6步:
#---第6步---运行定义
#def run(): #这是注释掉def run的代码,总体向左移动一格
#地球和月亮的球颜色和大小的设置
#65,105,225=品蓝色;0,0,255=蓝色
#50是地球大小,10是月球大小
#1和5是对应的速度
#earth = Ball(50,(65,105,225),1) #两种颜色法,故意注释掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() #pygame.display.flip() #可要可不要,因为双缓冲的缘由,须要将整个display的surface对象更新到屏幕上去 #刷新速度,60~600,数值越大刷新越快,速度也越快 clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()
#---单文件可要可不要,可是也是有道理的,我之前讲过
若是不要,注释掉,那么def run就能够删除,其下面的代码块总体向左相应的定格试试
#这是注释掉def run的代码
#if name == "main":
#run()
4. 简单动画或游戏,是不须要用if __name__ == "__main__":,可是推荐使用,我为了讲解代码的区别和分析采用去掉的,并且简单动画或游戏不要紧,复杂一点的或者大型动画或游戏,是须要的。 5. 简洁的完整代码以下:
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
pygame.init()
pygame.display.set_caption("太阳-地球-月亮模拟3d星空")
width,height = 1800,1400 #设置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕挂起
white = (255,255,255)
black=0,0,0
planse=[65,105,225]
screenCenterx = width//2 -1
screenCentery = height//2 -1
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)
class Star(object): #类
def init(self,x,y,speed): #定义坐标和速度
self.x = x
self.y = y
self.speed = speed
class Ball():
#球的半径,颜色和每次转的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#绕着x,y点以radius为半径旋转
def move(self,x,y,radius):
#乘以多少1~5,那么地球离太阳的距离就远些,相应的月球离地球也远些,3比较合适
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定义画图函数
def draw(self):
screen.blit(self.image,self.rect)
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)
#画星星,随机按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一帧画上一些星星
#x和y不可是坐标,仍是画布大小的设置
#初始化生成的星星个数2000,可本身调节
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))
clock = pygame.time.Clock() #Clock对象
#while循环
while True:
#退出设置,建议固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()
#在循环启动后仍能随机生成小星星,不要的画也能够,就是不能继续生成从右到左的后续小星星 y = float(randint(0,height)) x = float(randint(0,width)) speed = float(randint(10,300)) star = Star(x,y,speed) stars.append(star) time_passed = clock.tick() time_passed_seconds = time_passed/1000 #单位毫秒,需转换 #屏幕背景颜色 #screen.fill((0,0,0)) screen.fill(black) for star in stars: #绘制全部星星 new_x = star.x - time_passed_seconds*star.speed #画星星 pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y)) star.x = new_x #画个太阳在中间,255,125,64=肉色,大小100 pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100) #150是地球与太阳的距离,也是半径 earth.move(screenCenterx,screenCentery,150) earth.draw() #50是月球与地球的距离,也是半径 moon.move(earth.rect.centerx,earth.rect.centery,50) moon.draw() clock.tick(60) #屏幕更新,必需要,不然无画面 pygame.display.update()