本文實(shí)例為大家分享了pygame實(shí)現(xiàn)雷電游戲開發(fā)代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)公司專注于萬安網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供萬安營(yíng)銷型網(wǎng)站建設(shè),萬安網(wǎng)站制作、萬安網(wǎng)頁設(shè)計(jì)、萬安網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造萬安網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供萬安網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。源代碼:
stars.py
#-*- coding=utf-8 -*- #!/usr/bin/python import pygame from pygame.locals import * from random import randint import math class Star(object): def __init__(self, x, y, speed, color=(255,255,255)): self.x = x self.y = y self.speed = speed self.color = color class Stars(object): ''' 用于繪制星星背景 ''' def __init__(self, num = 0, SCREEN_SIZE=(800,600), color=(255,255,255)): self.stars = [] self.MIN_SPEED = 10 self.MAX_SPEED = 300 self.SCREEN_SIZE = SCREEN_SIZE if num > 0: self.create_star(num, color) def set_min_speed(self,speed): self.MIN_SPEED = speed def set_max_speed(self,speed): self.MAX_SPEED = speed def create_star(self,num = 1, color = (255,255,255)): '''創(chuàng)建一個(gè)或多個(gè)星星,顏色可選''' for i in xrange(0,num): x = float(randint(0, self.SCREEN_SIZE[0])) y = float(randint(0, self.SCREEN_SIZE[1])) speed = float(randint(self.MIN_SPEED, self.MAX_SPEED)) self.stars.append( Star(x, y, speed, color) ) def move(self,time_passed_seconds): '''移動(dòng)星星并過濾''' for star in self.stars: star.y = star.y + time_passed_seconds * star.speed #過濾跑出畫面的星星 self.stars = filter(lambda one: one.y<=self.SCREEN_SIZE[1], self.stars) def draw(self, surface): '''將星星畫到指定圖像對(duì)象''' for star in self.stars: #pygame.draw.aaline(surface, star.color,\ # (star.x, star.y), (star.x+1., star.y)) surface.set_at((int(star.x),int(star.y)),star.color) def test(): pygame.init() screen = pygame.display.set_mode((800, 600)) #, FULLSCREEN) stars = Stars() #stars.set_max_speed(1000) #stars.set_min_speed(300) # 在第一幀,畫上一些星星 stars.create_star(200) clock = pygame.time.Clock() white = (255, 255, 255) while True: for event in pygame.event.get(): if event.type == QUIT: return if event.type == KEYDOWN: return time_passed = clock.tick(30) time_passed_seconds = time_passed / 1000. #update_background(stars, screen, time_passed_seconds) # 增加一顆新的星星 stars.create_star(1) stars.move(time_passed_seconds) screen.fill((0, 0, 0)) # 繪制所有的星 stars.draw(screen) pygame.display.update() if __name__ == "__main__": test()
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。