Pygame是被設計用來寫遊戲的python模塊集合,Pygame是在優秀的SDL庫之上開發的功能性包。使用python能夠導入pygame來開發具備所有特性的遊戲和多媒體軟件,Pygame是極度輕便的而且能夠運行在幾乎全部的平臺和操做系統上。python
1.建立一個Pygame窗口windows
參考代碼測試
import pygame from pygame.locals import * import sys def hello_world(): pygame.init() pygame.display.set_mode((680,480)) pygame.display.set_caption('Hello World!') while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update() if __name__ == '__main__': hello_world()
效果截圖ui
2.建立一個能自由移動的角色操作系統
參考代碼設計
import pygame,sys from pygame.locals import * def play_guai(): pygame.init() window_size = (width,height)=(640,480) speed = [1,1] color_white = (255,255,255) screen = pygame.display.set_mode(window_size) pygame.display.set_caption("怪") girl_image = pygame.image.load('guai.png') girl_rect = girl_image.get_rect() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() girl_rect = girl_rect.move(speed) if(girl_rect.left<0) or (girl_rect.right>width): speed[0] = -speed[0] if(girl_rect.top<0) or (girl_rect.bottom>height): speed[1] = -speed[1] screen.fill(color_white) screen.blit(girl_image,girl_rect) pygame.display.update() if __name__ =='__main__': play_guai()
效果截圖code
3.建立一個用鍵盤操控的角色blog
參考代碼遊戲
import os,sys,pygame from pygame.locals import * def control_guai(event): speed = [x,y] = [0,0] speed_offset = 10 if event.type ==pygame.KEYDOWN: if event.key == pygame.K_a: speed[0] -= speed_offset if event.key == pygame.K_d: speed[0] = speed_offset if event.key == pygame.K_w: speed[1] -= speed_offset if event.key == pygame.K_s: speed[1] = speed_offset if event.type == pygame.KEYUP: if event.type in [pygame.K_a,pygame.K_d,pygame.K_w,pygame.K_s]: speed = [0,0] return speed def play_guai(): pygame.init() windows_size = Rect(0,0,960,587) speed = [1,1] color_whiht = (255,255,255) screen = pygame.display.set_mode(windows_size.size) pygame.display.set_caption('李白2.0') girl_image = pygame.image.load('guai.png') back_image = pygame.image.load('李白.jpg') girl_rect = girl_image.get_rect() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() cur_speed = control_guai(event) girl_rect = girl_rect.move(cur_speed).clamp(windows_size) screen.blit(back_image,(0,0)) screen.blit(girl_image,girl_rect) pygame.display.update() if __name__ == '__main__': play_guai()
效果截圖開發
4.建立一個精靈類測試
參考代碼
import pygame from pygame.locals import * class MySprite(pygame.sprite.Sprite): def __init__(self,target): pygame.sprite.Sprite.__init__(self) self.sprite_surface=target self.image=None self.master_image=None self.rect=None self.topleft=0,0 self.frame=0 self.old_fram=-1 self.fram_width=1 self.fram_height=1 self.first_fram=0 self.last_fram=0 self.columns=1 self.last_time=0 def load(self,filename,width,height,columns): self.master_image=pygame.image.load(filename).convert_alpha() self.fram_width=width self.fram_height=height self.rect=0,0,width,height self.columns=columns rect=self.master_image.get_rect() self.last_fram=(rect.width//width)*(rect.height//height)-1 def update(self,current_time,rate=60): if current_time>self.last_time+rate: self.frame+=1 if self.frame>self.last_fram: self.frame=self.first_fram self.last_time=current_time if self.frame!=self.old_fram: frame_x=(self.frame%self.columns)*self.fram_width frame_y=(self.frame//self.columns)*self.fram_height rect=(frame_x,frame_y,self.fram_width,self.fram_height) self.image=self.master_image.subsurface(rect) self.old_fram=self.frame pygame.init() screen=pygame.display.set_mode((400,300),0,32) pygame.display.set_caption("精靈類測試") font=pygame.font.Font(None,18) framerate=pygame.time.Clock() cat=MySprite(screen) cat.load("sprite2.png",92,95,4) group=pygame.sprite.Group() group.add(cat) while True: framerate.tick(10) ticks=pygame.time.get_ticks() for event in pygame.event.get(): if event.type==pygame.QUIT: pygame.quit() exit() key=pygame.key.get_pressed() if key[pygame.K_ESCAPE]: exit() screen.fill((0,0,255)) cat.update(ticks) screen.blit(cat.image,cat.rect) pygame.display.update()
效果截圖
好啦,今天的基礎就分享就到這啦