python 学习之尝试飞机大战游戏编程

大概思路

  1. 开始界面
    鼠标点击特定区域,开始游戏

  2. 游戏界面
    其中分为:
    计分,玩家生命值,玩家飞机,敌方飞机,发射子弹
    其中需要存储玩家飞机位置,判定按键输入(上下左右,a键发射子弹),随机函数,生成敌机位置,子弹命中判定,击中音效以及敌机,子弹运动轨迹函数。

  3. 结束界面
    显示game over,并按键重新开始游戏

以下为代码

import pygame
import sys
from pygame.locals import *
import random
from sys import exit
from pygame.color import THECOLORS
width = 640
height = 480
cellsize = 20
speed = 20
right = 'right'
left = 'left'
up = 'up'
down = 'down'
self = 'plane.jpg'
bullet = 'attack.jpg'
type1 = 'type1.jpg'
boom1 = 'boom1.jpg'
start1 = 'start.jpg'
cellw = int(width/cellsize)
cellsh = int(height/cellsize)
direction = 'test'
rctwidth = 0
color = 255, 153, 18
color1 = 255, 255, 0
planeplace = {'x': 280, 'y': 420}
attack = []
n = 0
enemyplace = []
number = 0
pygame.mixer.init()
music=pygame.mixer.Sound("1872.wav")
kill = 0
health = 100

def main():
    global planeplace,speedCLOCK,screen,n,count,health
    health = 100
    pygame.init()
    screen = pygame.display.set_mode((width, height))
    plane = pygame.image.load(self).convert()
    pygame.display.update()
    speedCLOCK = pygame.time.Clock()
    count = 0
    start()
    while True:
        for event in pygame.event.get():
            keys = pygame.key.get_pressed()
            if keys[K_BACKSPACE]:
                pygame.quit()
                sys.exit()
        screen.blit(plane, (290, 430))
        keytest()
        speedCLOCK.tick(speed)
        screen = pygame.display.set_mode((width, height))
        screen.blit(plane, (planeplace['x'], planeplace['y']))
        runroutine(planeplace)
        pygame.display.update()
        n = n+1

def keytest():
    global direction
    keys = pygame.key.get_pressed()
    if keys[K_UP]:
        direction = up
    if keys[K_DOWN]:
        direction = down
    if keys[K_RIGHT]:
        direction = right
    if keys[K_LEFT]:
        direction = left
    if direction == up:
        planeplace['y'] = planeplace['y'] - 1*cellsize
    if direction == down:
        planeplace['y'] = planeplace['y'] + 1*cellsize
    if direction == right:
        planeplace['x'] = planeplace['x'] + 1 * cellsize
    if direction == left:
        planeplace['x'] = planeplace['x'] - 1*cellsize
    direction = 'test'

def runroutine(planeplace):
    global n,count,number
    keys = pygame.key.get_pressed()
    if keys[K_a]:
        a = planeplace['x']
        b = planeplace['y']
        attack.insert( 0,{ 'x': a, 'y': b} )
    c = 0
    bullete = pygame.image.load(bullet).convert()
    enemy1  = pygame.image.load(type1).convert()
    if randomplace() <= 40:
        d = randomplace()
        if number < 5:
            enemyplace.insert(0, {'x': d, 'y': 0})
            number += 1
    h = 0
    for coord in enemyplace:
        if enemyplace[h]['y'] >420:
            number -= 1
            enemyplace.pop(h)
            continue
        screen.blit(enemy1, ( coord['x'] , enemyplace[h]['y'] ))
        enemyplace[h]['y'] = enemyplace[h]['y'] + cellsize/5
        h += 1
    over = pygame.font.SysFont('arial', 30)
    textImage = over.render("socre %.d" % (kill), True, color)
    screen.blit(textImage, (550, 30))
    over1 = pygame.font.SysFont('arial', 10)
    textImage = over1.render("HEALTH piont %.d" % (health), True, color)
    screen.blit(textImage, (30, 440))
    for coord in attack:
        if (coord['y']) < 0:
            attack.pop(c)
            if c == 0:
                return
            if c != 0:
                c -= 1
        screen.blit(bullete, (coord['x'] - 10, attack[c]['y'] - 20))
        screen.blit(bullete, (coord['x'] + 50,  attack[c]['y'] - 20))
        attack[c]['y'] = attack[c]['y'] - cellsize
        judgement(attack, enemyplace, c)
        c += 1
        count += 1
    healthcheck(enemyplace, planeplace)
def randomplace():
    return random.randint( 0, 580)

def judgement(attack,enemyplace,c):
    global number,kill
    b = 0
    boom2 = pygame.image.load(boom1).convert()
    for coord in enemyplace:
        if (enemyplace[b]['y'] / 20) <= ((attack[c]['y'] - 20) / 20) <= (((enemyplace[b]['y'] ) / 20) + 3):
            if ((enemyplace[b]['x']/20)<=((attack[c]['x']-10)/20)<=(((enemyplace[b]['x'])/20)+3)) or ((enemyplace[b]['x']/20)<=((attack[c]['x'] + 50)/20)<=(((enemyplace[b]['x'])/20)+3)):
                screen.blit(boom2, (enemyplace[b]['x'], enemyplace[b]['y']))
                enemyplace.pop(b)
                attack.pop(c)
                music.play()
                number -= 1
                kill += 1
                return
        b += 1

def start():
    start2 = pygame.image.load(start1).convert()
    screen.blit(start2, ( 480, 400))
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and 480 <= event.pos[0] <= 640 and 400 <= event.pos[1] <= 480:
                return
def healthcheck( enemyplace,planeplace):
    global health
    ct = 0
    for coord in enemyplace:
        if enemyplace[ct]['x'] <= planeplace['x'] <= (enemyplace[ct]['x'] + 60) and enemyplace[ct]['y'] <= planeplace['y'] <= (enemyplace[ct]['y'] + 60):
            health -= 50
            enemyplace.pop(ct)
        if health <= 0:
            gameover()
        ct += 1

def gameover():
    screen = pygame.display.set_mode((width, height))
    over = pygame.font.SysFont('arial', 60)
    over2 = pygame.font.SysFont( 'arial', 30)
    textImage = over.render("game over", True, color)
    screen.blit(textImage, (200, 200))
    textImage2 = over2.render("press L-CTRL to restar", True, color)
    screen.blit(textImage2, (200, 300))
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            keys = pygame.key.get_pressed()
            if keys[K_BACKSPACE]:
                pygame.quit()
            if keys[K_LCTRL]:
                main()
main()

其中需要有敌机,玩家飞机,子弹,爆炸特效,开始方块按钮的贴图,以及爆炸音效! 我直接采用的网上搜索,ps处理的方法,但是在处理图片时,jpg格式一般会自带背景图,如果采用png格式读取,仍然会带有背景颜色,这里的问题尚未完全解决,需要更深层次的学习。

你可能感兴趣的:(python 学习之尝试飞机大战游戏编程)