pygame 快速入门

  • pygame 就是一个 Python 模块,专为游戏设计
  • 官方网站:https://www.pygame.org/

第一步:
安装pygame
$ sudo pip3 install pygame
第二部:
$ python3 -m pygame.examples.aliens
今天我们要做的是飞机大战。

import random
import pygame
CREATE_ENEMY_EVENT =pygame.USEREVENT

HERO_FIRE_EVENT = pygame.USEREVENT +1

FEAM_PER_SEC = 60
SCREEN_RECT = pygame.Rect(0,0,700,700)
class GameSprite(pygame.sprite.Sprite):
def init(self,image_name,speed = 1,speend = 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
class background(GameSprite):
def init(self,is_alt=False):
self.image_name = "./images/asd.jpg"
super().init(self.image_name)
if is_alt:
self.rect.y -= self.rect.height
def update (self):
super().update()
self.rect.y += self.speed
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = -self.rect.height
class Enemy (GameSprite):
def init(self):
self.image_name = "./images/enemy-1.gif"
super().init(self.image_name)

    self.speed = random.randint(1,5)
    self.rect.bottom = 0
    max_x = SCREEN_RECT.width - self.rect.width
    self.rect.x = random.randint(0,max_x)
    
def update(self):
    super().update()
    if self.rect.y >= SCREEN_RECT.height:
        self.kill()
def __del__(self):
           print("清除德吉")

class Hero(GameSprite):
def init(self):
super().init("./images/plane.png",0)
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom-100
self.bullets = pygame.sprite.Group()
def update(self):
self.rect.x += self.speed
if self.rect.left <0:
self.rect.left = 0
if self.rect.right > SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right
def updateud(self):
self.rect.y += self.speend
def fire (self):
print("杀无赦")
for i in (1,2,3,4):
bullet = Bullet()
bullet.rect.bottom = self.rect.y - i*20
bullet.rect.centerx = self.rect.centerx
self.bullets.add(bullet)

class Bullet(GameSprite):
def init(self):
super().init("./images/enemy0_down print("清除德吉")

class Hero(GameSprite):
def init(self):
super().init("./images/plane.png",0)
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom-100
self.bullets = pygame.sprite.Group()
def update(self):
self.rect.x += self.speed
if self.rect.left <0:
self.rect.left = 0
if self.rect.right > SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right
def updateud(self):
self.rect.y += self.speend
def fire (self):
print("杀无赦")4.png",-1)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
def del(self):
print("珊瑚子弹")

import pygame
from PlanSprite import *
HERO_FIRE_EVENT = pygame.USEREVENT +1

class PlanGame(object):
#初始化
def init(self):
print("游戏初始化")
#创建窗口
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
#创建时钟对象
self.clock = pygame.time.Clock()
self.__create_sprite()

    pygame.time.set_timer( CREATE_ENEMY_EVENT,1000)
    pygame.time.set_timer(HERO_FIRE_EVENT,500)
    self.enemy_group = pygame.sprite.Group()
#开始游戏
def startGame(self):
    print("开始游戏")   
    while True:
        self.clock.tick(60)
        self.__event_handler()
        self.__check_collide()
        self.__update_sprites()
        pygame.display.update()



#创建精灵或精灵组
def __create_sprite(self):
    bg = background()
    bg1 = background(True)
    self.back_group = pygame.sprite.Group(bg,bg1)

    self.hero = Hero()
    self.hero_group = pygame.sprite.Group(self.hero)

    #self.bullets =  Bullet()
    #self.bullet_group = pygame.sprite.Group(self.bullets)



    #事件监听
def __event_handler(self):
    keys_pressed = pygame.key.get_pressed()
    if keys_pressed[pygame.K_RIGHT]:
        self.hero.speed = 6
    elif keys_pressed[pygame.K_LEFT]:
        self.hero.speed = -6
    elif keys_pressed[pygame.K_UP]:
        self.hero.speend = -6
    elif keys_pressed[pygame.K_DOWN]:
        self.hero.speend = 6
    else:
        self.hero.speed = 0

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            PlanGame.__game_over(self)
        elif event.type == CREATE_ENEMY_EVENT:
            enemy = Enemy()
            self.enemy_group.add(enemy)
        elif event.type == HERO_FIRE_EVENT:
            self.hero.fire()

碰撞检测

def __check_collide(self):
    pygame.sprite.groupcollide(self.hero.bullets,self.enemy_group,True,True)
    enemies = pygame.sprite.spritecollide(self.hero,self.enemy_group,True)
    if len(enemies) > 0:
        self.hero.kill()
        PlaneGame.__game_over()
    #更新精灵组
def __update_sprites(self):
    self.back_group.update()
    self.back_group.draw(self.screen)

    self.enemy_group.update()
    self.enemy_group.draw(self.screen)

    self.hero_group.update()
    self.hero_group.draw(self.screen)

    self.hero.bullets.update()
    self.hero.bullets.draw(self.screen)
def __game_over(self):
    print("游戏结束")   
    pygame.quit()
    exit()  

if name == 'main':
plangame = PlanGame()
plangame.startGame()

以上就是制作飞机大战的简单程序。

你可能感兴趣的:(pygame 快速入门)