pygame 学习check_event,game_function,alien_invasion等程序
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                pygame 学习check_event,game_function,alien_invasion等程序
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                            #game.py
#主程序
import pygame
from settings import Settings
from game_function import update_screen
from game_function import check_events
from ship import Ship
def run_game():pygame.init()my_set = Settings()screen = pygame.display.set_mode((my_set.width,my_set.height))ship = Ship(screen)pygame.display.set_caption(my_set.caption)while True:check_events()update_screen(my_set,screen,ship)run_game() 
#settings.py
import pygame
import sys
class Settings():def __init__(self):self.color = (0,0,255)self.width = 1000self.height = 800self.caption = 'alien invasion' 
#ship.py
import pygame
class Ship():def __init__(self,screen):self.screen = screenself.screen_rect = self.screen.get_rect()self.image = pygame.image.load('ship.bmp')self.rect = self.image.get_rect()self.rect.centerx = self.screen_rect.centerxself.rect.bottom  = self.screen_rect.bottomdef blitme(self):self.screen.blit(self.image,self.rect)
 
#game_function.py
import pygame
import sys
from settings import Settings
def check_events():for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()def update_screen(settings,screen,ship):"""circle the ship in screen""""""fill the color """"""flip the screen"""screen.fill(settings.color)ship.blitme()pygame.display.flip() 
                        
                        
                        本文所用到的ship.bmp和alien.bmp的下載? ?鏈接: https://pan.baidu.com/s/1Dn5yaQGZLIsN0BohM8PzoA ?密碼: 38ie
滿足上述4個(gè)源代碼文件以及保證文件夾下放置ship.bmp情況下,運(yùn)行代碼:
ubuntu運(yùn)行命令:
python3 game.pywin系統(tǒng)運(yùn)行命令:
python game.py運(yùn)行結(jié)果:
?
總結(jié)
以上是生活随笔為你收集整理的pygame 学习check_event,game_function,alien_invasion等程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: c现代方法 13章程序设计题 自己编写答
 - 下一篇: (问题)c语言现代方法2th,自己编写的