python--pygame01
1.编写外星人入侵地球的游戏。
import sys import pygame from setting import Settings #第一步: ##游戏设置及游戏屏幕 def run_game(): pygame.init() ai_settings=Settings() screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) pygame.display.set_caption("alien invasion") #设置背景色 # bg_color=ai_settings.bg_color #利用event事件检测用户键盘鼠标的操作 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() #每次重绘屏幕会调用 screen.fill(ai_settings.bg_color) pygame.display.flip() run_game() #第二步: #1.创建类 #1.a.设置的类
另一个文件setting.py
##根据玩家的喜好制定设置,包括屏幕的大小,颜色等。
class Settings(): def __init__(self,screen_width=1200,screen_height=800,bg_color=(249,123,189)): screen_width=input("please input what you like the screen height,default 800") self.screen_width=int(screen_width) screen_height=input("please input what you like the screen height,default 1200") self.screen_height=int(screen_height) self.bg_color=bg_color