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()
	
				
		
原创粉丝点击