当前位置:   article > 正文

Python小例子——使用pygame应用:飞机大战(五 发射子弹)_pygame库的使用: 案例:飞机大战任务五实现效 卑展示

pygame库的使用: 案例:飞机大战任务五实现效 卑展示
#coding=utf-8
import pygame
from pygame.locals import *

'''
    5. 实现玩家飞机发射子弹

    接下来要做的任务:
    1. 实现飞机在你想要的位置显示
    2. 实现按键控制飞机移动
    3. 实现按下空格键的时候,显示一颗子弹
'''

class HeroPlane(object):

    def __init__(self,screen):

        #设置飞机默认的位置
        self.x = 150
        self.y = 480

        #设置要显示内容的窗口
        self.screen = screen

        self.imageName = "hero0.png"
        self.image = pygame.image.load(self.imageName).convert()

        #用来存储英雄飞机发射的所有子弹
        self.bulletList = []

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

        for bullet in self.bulletList:
            bullet.display()#显示一个子弹的位置
            bullet.move()#让这个子弹进行移动,下次再显示的时候就会看到子弹在修改后的位置

    def moveLeft(self):
        self.x -= 10

    def moveRight(self):
        self.x += 10

    def moveUp(self):
        self.y -= 10

    def moveDown(self):
        self.y += 10

    def sheBullet(self):
        newBullet = Bullet(self.x, self.y, self.screen)
        self.bulletList.append(newBullet)

class Bullet(object):
    def __init__(self,x,y,screen):
        self.x = x+20
        self.y = y-20
        self.screen = screen
        self.image = pygame.image.load("bullet.png").convert()

    def move(self):
        self.y -= 5

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

def key_control(heroPlane):
    #判断是否是点击了退出按钮
    for event in pygame.event.get():
        # print(event.type)
        if event.type == QUIT:
            print("exit")
            exit()
        elif event.type == KEYDOWN:
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                heroPlane.moveLeft()
                #控制飞机让其向左移动
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                heroPlane.moveRight()
            elif event.key == K_w or event.key == K_UP:
                print('up')
                # 控制飞机让其向上移动
                heroPlane.moveUp()
            elif event.key == K_s or event.key == K_DOWN:
                print('down')
                # 控制飞机让其向下移动
                heroPlane.moveDown()
            elif event.key == K_SPACE:
                print('space')
                heroPlane.sheBullet()

def main():
    #1. 创建一个窗口,用来显示内容
    screen = pygame.display.set_mode((400,654),0,32)

    #2. 创建一个和窗口大小的图片,用来充当背景
    background = pygame.image.load("background.png").convert()

    #3. 创建一个飞机对象
    heroPlane = HeroPlane(screen)

    #3. 把背景图片放到窗口中显示
    while True:
        screen.blit(background,(0,0))

        heroPlane.display()

        key_control(heroPlane)

        pygame.display.update()

if __name__ == "__main__":
    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

运行程序,结果如下:
在这里插入图片描述

space
left
space
right
space
space
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/123163
推荐阅读
相关标签
  

闽ICP备14008679号