实战项目–>飞机大战·上
; pygame 快速入门
一、使用pygame创建图形窗口
; 例:
import pygame
pygame.init()
print("游戏代码...")
pygame.quit()
例:
import pygame
hero_rect = pygame.Rect(100, 500, 120, 125)
print("英雄的原点: %d %d" % (hero_rect.x, hero_rect.y))
print("英雄的尺寸: %d %d" % (hero_rect.width, hero_rect.height))
print("%d %d " % hero_rect.size)
例:
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 700))
pygame.quit()
二、理解 图像 并实现图像绘制
; 例:
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 700))
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
pygame.display.update()
while True:
pygame.event.get(1000)
pass
pygame.quit()
例:
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 700))
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
pygame.display.update()
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (180, 500))
pygame.display.update()
while True:
pygame.event.get(1000)
pass
pygame.quit()
例:
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 700))
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (180, 500))
pygame.display.update()
while True:
pygame.event.get(1000)
pass
pygame.quit()
三、理解 游戏循环 和 游戏时钟
每一次调用 update 产生的效果,叫做帧。
; 例:
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 700))
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (180, 500))
pygame.display.update()
clock = pygame.time.Clock()
while True:
clock.tick(60)
pygame.quit()
部分:
clock = pygame.time.Clock()
hero_rect = pygame.Rect(180, 500, 102, 126)
while True:
clock.tick(60)
hero_rect.y -= 1
screen.blit(bg, (0, 0))
screen.blit(hero, hero_rect)
pygame.display.update()
部分:
while True:
clock.tick(60)
event_list = pygame.event.get()
if len(event_list) > 0:
print(event_list)
hero_rect.y -= 3
if hero_rect.y -126:
hero_rect.y = 700
screen.blit(bg, (0, 0))
screen.blit(hero, hero_rect)
pygame.display.update()
部分:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("游戏退出...")
pygame.quit()
exit()
hero_rect.y -= 3
if hero_rect.y -126:
hero_rect.y = 700
screen.blit(bg, (0, 0))
screen.blit(hero, hero_rect)
pygame.display.update()
四、理解 精灵 和 精灵组
注意:仍然需要调用 pygame.display.update() 才能在屏幕上看到最终结果。
; 部分:
plane_sprites(在下篇会用于项目的python文件) 模块代码:
import pygame
class GameSprite(pygame.sprite.Sprite):
"""飞机大战游戏精灵"""
def __init__(self, image_name, speed=1):
super().__init__()
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
self.rect.y += self.speed
部分:
演练精灵代码:
import pygame
from plane_sprites import *
pygame.init()
screen = pygame.display.set_mode((480, 700))
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (180, 500))
pygame.display.update()
clock = pygame.time.Clock()
hero_rect = pygame.Rect(180, 500, 102, 126)
enemy = GameSprite("./images/enemy1.png")
enemy1 = GameSprite("./images/enemy1.png", 2)
enemy_group = pygame.sprite.Group(enemy, enemy1)
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("游戏退出...")
pygame.quit()
exit()
hero_rect.y -= 3
if hero_rect.y -126:
hero_rect.y = 700
screen.blit(bg, (0, 0))
screen.blit(hero, hero_rect)
enemy_group.update()
enemy_group.draw(screen)
pygame.display.update()
pygame.quit()
附带B站找到的”飞机大战素材”地址:
https://pan.baidu.com/s/1pMM0beb
Original: https://blog.csdn.net/qq_52773803/article/details/114269391
Author: Aquarius、33
Title: Python实战项目 –> 飞机大战(学习过程·上)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/781205/
转载文章受原作者版权保护。转载请注明原作者出处!