- 安装pygame包,把安装好的包copy一份到pycharm工程目录下,不然找不到pygame包
抄一遍书上的代码:
settings.py
class Settings():def __init__(self):self.screen_width = 1200self.screen_height = 800self.bg_color = (255, 255, 255)self.ship_speed_factor = 1.5
ship.py
import pygame
class Ship():def __init__(self, ai_settings, screen):self.screen = screenself.ai_settings = ai_settingsself.image = pygame.image.load("images/ship.bmp")self.rect = self.image.get_rect() #图像矩形self.screen_rect = screen.get_rect() #屏幕矩形,左上角原点,右下角如1200,800self.rect.centerx = self.screen_rect.centerx #图像x中心,在屏幕中心self.rect.bottom = self.screen_rect.bottom #图像底边,在屏幕底边self.center = float(self.rect.centerx) #图像x中心坐标转成浮点型,存在center中self.moving_right = Falseself.moving_left = Falsedef update(self):if self.moving_right and self.rect.right < self.screen_rect.right: #限制位置在屏幕内self.center += self.ai_settings.ship_speed_factorif self.moving_left and self.rect.left > 0:self.center -= self.ai_settings.ship_speed_factorself.rect.centerx = self.centerdef blitme(self):self.screen.blit(self.image, self.rect) #blit实现动画效果
game_functions.py
import sys
import pygame
def check_keydown_events(event, ship): #检测键盘按下事件if event.key == pygame.K_RIGHT:ship.moving_right = Trueelif event.key == pygame.K_LEFT:ship.moving_left = Truedef check_keyup_events(event, ship): #检测键盘松开事件if event.key == pygame.K_RIGHT:ship.moving_right = Falseelif event.key == pygame.K_LEFT:ship.moving_left = Falsedef check_events(ship):for event in pygame.event.get():if event.type == pygame.QUIT: #退出事件sys.exit()elif event.type == pygame.KEYDOWN: #键盘按下事件check_keydown_events(event, ship)elif event.type == pygame.KEYUP: #键盘松开事件check_keyup_events(event, ship)def update_screen(ai_settings, screen, ship):screen.fill(ai_settings.bg_color) #屏幕填充背景色ship.blitme() #ship的动画效果pygame.display.flip() #让动画尽可能流畅
alien_invasion.py
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
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") #设置标题ship = Ship(ai_settings, screen) #创建游戏对象while True:gf.check_events(ship) #检测事件ship.update() #更新ship位置gf.update_screen(ai_settings, screen, ship) #刷新屏幕run_game()
练习题:
1.火箭上下左右移动
2.创建一个程序,显示一个空屏幕。在事件循环中,每当检测到 pygame.KEYDOWN 事件时都打印属性event.key。运行这个程序,并按各种键,看看Pygame如何响应
import pygame
import sys
def check_events(screen):for event in pygame.event.get():if event.type == pygame.QUIT: #退出事件sys.exit()elif event.type == pygame.KEYDOWN: #键盘按下事件while True:font = pygame.font.SysFont("宋体", 80) #设置字体font_surface = font.render(str(event.key), True, (0, 0, 0), (255, 255, 255)) #渲染内容为面pygame.image.save(font_surface, "text.bmp") #面保存成图片image = pygame.image.load("text.bmp") #获取图片image_rect = image.get_rect() #获取图片的矩形image_rect.centerx = 200 #设置图片矩形位置image_rect.centery = 150screen.blit(image, image_rect) #屏幕呈现该图片pygame.display.flip() #屏幕刷新显示出来if pygame.event.poll().type == pygame.KEYUP: #键盘松开break #退出
def run_game():pygame.init()pygame.font.init()screen = pygame.display.set_mode((400, 300))pygame.display.set_caption("测试按键......") #设置标题while True:screen.fill((255, 255, 255)) # 屏幕填充背景色pygame.display.flip() #刷新屏幕check_events(screen) #检测事件run_game()
添加子弹模块后的代码
settings.py
class Settings():def __init__(self):self.screen_width = 1200self.screen_height = 800self.bg_color = (255, 255, 255)self.ship_speed_factor = 1.5 #飞船速度self.bullet_speed_factor = 1 #子弹速度self.bullet_width = 3 #子弹宽高self.bullet_height = 15self.bullet_color = 60, 60, 60 #子弹颜色self.bullets_allowed = 2 #屏幕中允许的子弹数量
ship.py 不变
bullet.py
import pygame
from pygame.sprite import Spriteclass Bullet(Sprite):def __init__(self, ai_settings, screen, ship):super().__init__()self.screen = screenself.rect = pygame.Rect(0, 0, ai_settings.bullet_width,ai_settings.bullet_height) #绘制子弹的矩形self.rect.centerx = ship.rect.centerx #子弹x与飞船相同self.rect.top = ship.rect.top #子弹从飞船顶部起飞self.y = float(self.rect.y) # y存一份浮点的,可以精调位置self.color = ai_settings.bullet_color #子弹颜色self.speed_factor = ai_settings.bullet_speed_factor #子弹速度def update(self):self.y -= self.speed_factor #位置更新self.rect.y = self.y #浮点y位置付给矩形图形def draw_bullet(self):pygame.draw.rect(self.screen, self.color, self.rect) #把子弹矩形画到屏幕上
game_functions.py
import sys
import pygame
from bullet import Bullet
def fire_bullet(ai_settings, screen, ship, bullets): #发射子弹if len(bullets) < ai_settings.bullets_allowed: #子弹没超数量new_bullet = Bullet(ai_settings, screen, ship) #生成新子弹bullets.add(new_bullet) #新子弹加入组
def check_keydown_events(event,ai_settings, screen, ship, bullets): #检测键盘按下事件if event.key == pygame.K_RIGHT:ship.moving_right = Trueelif event.key == pygame.K_LEFT:ship.moving_left = Trueelif event.key == pygame.K_SPACE: #按下空格键,发射子弹fire_bullet(ai_settings, screen, ship, bullets)def check_keyup_events(event, ship): #检测键盘松开事件if event.key == pygame.K_RIGHT:ship.moving_right = Falseelif event.key == pygame.K_LEFT:ship.moving_left = Falsedef check_events(ai_settings, screen, ship, bullets):for event in pygame.event.get():if event.type == pygame.QUIT: #退出事件sys.exit()elif event.type == pygame.KEYDOWN: #键盘按下事件check_keydown_events(event, ai_settings, screen, ship, bullets)elif event.type == pygame.KEYUP: #键盘松开事件check_keyup_events(event, ship)def update_screen(ai_settings, screen, ship, bullets):screen.fill(ai_settings.bg_color) #屏幕填充背景色for bullet in bullets.sprites(): #把每个子弹画在屏幕上bullet.draw_bullet()ship.blitme() #ship的动画效果pygame.display.flip() #刷新,让动画尽可能流畅def update_bullets(bullets):bullets.update() #更新子弹位置for bullet in bullets.copy(): #删除飞出屏幕的子弹if bullet.rect.bottom <= 0:bullets.remove(bullet)
alien_invasion.py
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
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") #设置标题ship = Ship(ai_settings, screen) #创建游戏飞船对象bullets = Group() #创建子弹组while True:gf.check_events(ai_settings, screen, ship, bullets) #检测事件ship.update() #更新ship位置gf.update_bullets(bullets) #更新子弹位置,删除飞出去的子弹# print(len(bullets)) #调试时,确认子弹飞出屏幕后被删除gf.update_screen(ai_settings, screen, ship, bullets) #刷新屏幕run_game()
作业3:更改上述代码,使之横向发射子弹
添加 pygame.transform.rotozoom(surface, angle, scale) (缩放和旋转)
代码连接 https://github.com/hitskyer/course/tree/master/python/chenmingming/pygame_project/alien_invasion