Pygame

作者: Yehao_ | 来源:发表于2018-08-05 21:47 被阅读0次

类:

import pygame

SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640


class Hero(pygame.sprite.Sprite):
    """玩家"""

    def __init__(self, hero_surface, hero_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = hero_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = hero_init_pos
        self.speed = 6
        self.bullets1 = pygame.sprite.Group()

    def move(self, offset):
        # 左右移动 左减右加
        x = self.rect.left + offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]
        # 上下移动 下加上减
        y = self.rect.top + offset[pygame.K_DOWN] - offset[pygame.K_UP]
        # 放置左右移动时出界
        if x < 0:
            self.rect.left = 0
        elif x > SCREEN_WIDTH - self.rect.width:
            self.rect.left = SCREEN_WIDTH - self.rect.width
        else:
            self.rect.left = x

        # 防止上下移动时出界
        if y < 0:
            self.rect.top = 0
        elif y > SCREEN_HEIGHT - self.rect.height:
            self.rect.top = SCREEN_HEIGHT - self.rect.height
        else:
            self.rect.top = 0

    # 控制射击行为
    def single_shoot(self, bullet1_surface):
        bullet1 = Bullet(bullet1_surface, self.rect.midtop)
        self.bullets1.add(bullet1)


class Bullet(pygame.sprite.Sprite):
    """子弹"""
    def __init__(self, bullet_surface, bullet_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = bullet_init_pos
        self.speed = 8

    # 控制子弹移动
    def update(self):
        self.rect.top -= self.speed
        if self.rect.top < -self.rect.height:
            self.kill()

Main:

from gameRole import *


# 定义窗口的分辨率
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640

# 定义画面帧率
FRAME_RATE = 60

# 定义动画周期(帧数)
ANIMATE_CYCLE = 30

# 计数ticks
ticks = 0

offset = {pygame.K_LEFT: 0, pygame.K_RIGHT: 0, pygame.K_UP: 0, pygame.K_DOWN: 0}

# 初始化游戏
pygame.init()  # 初始化pygame
clock = pygame.time.Clock()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))  # 初始化一个用于显示的窗口

pygame.display.set_caption('飞机大战')  # 设置窗口标题

background = pygame.image.load('resources/image/background.png')  # 载入背景图

shoot_img = pygame.image.load('resources/image/shoot.png')  # 载入资源图片

# 用subsurface剪切读入的图片
hero_surface = []
# hero1_rect = pygame.Rect(0, 99, 102, 126)
hero_surface.append(shoot_img.subsurface(pygame.Rect(0, 99, 102, 126)))
# hero2_rect = pygame.Rect(165, 360, 105, 126)
hero_surface.append((shoot_img.subsurface(pygame.Rect(165, 360, 102, 126))))
# hero1 = shoot_img.subsurface(hero1_rect)
# hero2 = shoot_img.subsurface(hero2_rect)
hero_pos = [200, 600]

# 创建玩家
hero = Hero(hero_surface[0], hero_pos)

bullet1_surface = shoot_img.subsurface(pygame.Rect(1004, 987, 9, 21))

pygame.display.flip()

# 时间循环 (main loop)
while True:

    # 控制游戏最大帧率
    clock.tick(FRAME_RATE)

    screen.blit(background, (0, 0))  # 绘制背景

    # 改变飞机图片制造动画
    if ticks >= ANIMATE_CYCLE:
        ticks = 0
    hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]

    # 绘制飞机
    screen.blit(hero.image, hero.rect)
    ticks += 1

    # 射击
    if ticks % 10 == 0:
        hero.single_shoot(bullet1_surface)
    # 控制子弹
    hero.bullets1.update()
    # 绘制子弹
    hero.bullets1.draw(screen)

    pygame.display.update()  # 更新屏幕

    # 处理游戏退出
    # 从消息队列中循环取
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        # 控制方向
        if event.type == pygame.KEYDOWN:
            if event.key in offset:
                offset[event.key] = hero.speed
        elif event.type == pygame.KEYUP:
            if event.key in offset:
                offset[event.key] = 0

    # 移动飞机
    hero.move(offset)

相关文章

  • 2018-09-04-pygame

    一、pygame基本操作 import pygame——导入pygame模块 pygame.init()——初始化...

  • Pygame入门--飞机大战案例

    Pygame的快速入门 #导入pygame模块 import pygame #游戏初始化 pygame.init(...

  • Day_10 异常与pygame

    异常捕获 pygame操作流程 pygame显示文字 pygame显示图片与图片操作 pygame基本显示

  • Pygame-hello world

    使用pygame 模块名功能pygame.cdrom访问光驱pygame.cursors加载光标pygame.di...

  • pygame - alphabet

    pygame install pygame install[https://www.pygame.org/wiki...

  • Day12 pygame

    1.pygame基本操作: 1.导入pygame: import pygame.2.初始化:pygame init...

  • Day-18正则表达式2

    pygame游戏基本框架的创建 pygame中图片的显示 字体的显示 图形 Pygame Pygame有很多的模块...

  • day11-pygame笔记

    1pygame事件 import pygame pygame.display.set_caption('游戏事件'...

  • Python——Pygame模块

    学习资料: Pygame官网 pygame系列 PyGame - Python Wiki 用Python和Pyga...

  • pygame简介

    简介 关于Pygame的基本信息,pygame是什么,谁会被Pygame吸引,并且在哪里找到它。 Pygame是被...

网友评论

      本文标题:Pygame

      本文链接:https://www.haomeiwen.com/subject/vrjivftx.html