pygame初体验——3D效果的雪景
来源:互联网 发布:河南省卫生网络直报 编辑:程序博客网 时间:2024/06/06 00:13
最近用pygame写了一个具有3D效果的雪景,截图为证:
雪花带有旋转效果
初学python,存在的问题很多, 路过的大神不吝赐教~~~代码献上,希望对一样热爱python的同学有帮助~~
献上代码:
#encoding:utf-8'''Created on 2013��9��8��@author: yangluo'''import pygameimport Imagefrom math import *from pygame.locals import *from random import randintfrom gameobjects.vector2 import Vector2snow_src_im = 'resource/snow.png'class Star(object): def __init__(self, x, y, speed_x=1, speed_y=1): self.x = x self.y = y self.speed = Vector2(speed_x, speed_y) self.pos = Vector2(x, y) def run(): SCREEN_SIZE = (1366, 768) snow_speed = 300. snow_rotation = 0 snow_rotation_speed = randint(100,360) rotation_direction = 0 speed_length = int(150*sqrt(2) - 10*sqrt(13)) #the rangge of the max and min speed screen = pygame.display.set_mode(SCREEN_SIZE,FULLSCREEN) stars = [] star_night_image = pygame.image.load(r'resource/star_night.png').convert() snow = pygame.image.load(snow_src_im).convert_alpha() #snow_im is to store the different snow images pygame.init() snow_im = [] for i in range(0,5): snow_im.append(pygame.image.load('resource/snow%d.png' %i).convert_alpha()) #create the first star for n in xrange(2): x = randint(-1, 320) y = randint(-100, 0) speed = Vector2(0,0) speed.x = randint(20, 100) speed.y = randint(30, 100) stars.append( Star(x, y, speed.x, speed.y) ) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == QUIT: return if event.type == KEYDOWN: return #init the stars's position x= randint(-1000, 400) y = randint(-200,0) speed = Vector2(0,0) speed.x = randint(20, 150) speed.y = randint(30, 150) star = Star(x, y, speed.x, speed.y ) stars.append(star) #add the new star to the stars #random the rotation_direction so that the snows will rotate in different speed rotation_direction = randint(0,20) time_passed = clock.tick() time_passed_seconds = time_passed /1000. #add the background image screen.blit(star_night_image,(0,0)) i = 0 #given the different snow image according to the speed of the star #delete the snows which will never on the screen for star in stars: real_speed = star.speed.get_magnitude() if real_speed > speed_length*4/5: rotated_snow = pygame.transform.rotate(snow_im[0], snow_rotation) if stars[i].pos.y >= SCREEN_SIZE[1]*4/5: del stars[i] elif real_speed > speed_length *3/5: rotated_snow = pygame.transform.rotate(snow_im[1], snow_rotation) if stars[i].pos.y >= SCREEN_SIZE[1]*4/5: del stars[i] elif real_speed > speed_length * 2/5: rotated_snow = pygame.transform.rotate(snow_im[2], snow_rotation) if stars[i].pos.y >= SCREEN_SIZE[1]*7/10: del stars[i] elif real_speed > speed_length /5 : rotated_snow = pygame.transform.rotate(snow_im[3], snow_rotation) if stars[i].pos.y >= SCREEN_SIZE[1]*3/5: del stars[i] else: rotated_snow = pygame.transform.rotate(snow_im[4], snow_rotation) if stars[i].pos.y >= SCREEN_SIZE[1]*3/5: del stars[i] i += 1 #reset the snows' position and the rotation w, h = rotated_snow.get_size() snow_draw_pos = Vector2(star.x - w/2, star.y - h/2) screen.blit(rotated_snow, snow_draw_pos) snow_rotation += rotation_direction * snow_rotation_speed * time_passed_seconds/10000 star.pos += star.speed * time_passed_seconds star.x = star.pos.x star.y = star.pos.y #def on_screen(star): # return star.x > 0 #stars = filter(on_screen, stars) pygame.display.update() if __name__ == "__main__": run()
- pygame初体验——3D效果的雪景
- windows真正体验3D效果桌面
- Shader实例 : 雪景效果
- WPF 3D初体验
- 上海难得一见的雪景
- 新年雪景新气象3
- 雪景
- 新的触摸体验——iOS9的3D Touch
- 新的触摸体验——iOS9的3D Touch
- HTML5的3D效果
- UITableView的3D效果
- 3D效果的ViewPager
- 3D效果的文字
- Lights:奇妙的WebGL 3D体验
- 3D Touch开发初体验.
- 3D Touch开发初体验
- 2014,难得一见的美丽雪景
- pygame-最易上手的2d游戏引擎
- linux-i2c驱动 之 i2c设备层的注册过程probe函数如何被调用分析
- mac os开发资料
- 五、代理模式
- 判断字符串是否为IP地址
- oracle 常用
- pygame初体验——3D效果的雪景
- 静态加载dll,动态加载dll及常见问题处理
- 12个有趣的C语言问答
- 命名管道文件的使用
- 将 ASP.NET MVC3 Razor 项目部署到虚拟主机中
- Linux下部署nginx
- 新导入项目可能会出现的错误
- IIS部署asp.net报404错误
- MULE学习笔记1