您的位置:首页 > 编程语言 > Python开发

python-小游戏(打飞机)v1.2(三发子弹)

2017-02-28 10:22 337 查看
---------------------------------备注-----------------------------------

运行环境 :

python --v3.2.5

pygame -- pygame-1.9.2a0.win32-py3.2

-------------------------------------------------------------------------

# -*- coding: utf-8 -*-
import pygame
import random
from pygame.locals import *
from sys import exit
pygame.init()

class FeiJi:

def __init__(self,x,y):
self.x = x
self.y = y
self.image = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\feiji.png')

def restart(self):
self.x = 200
self.y = 500
#加载 飞机 图像
screen.blit(feiji.image, (feiji.x,feiji.y))

def move(self):
#-----------------------------------
#判断 按键 状态
if event.type==pygame.KEYDOWN:
if event.key==K_w:
keys[0]=True
elif event.key==K_a:
keys[1]=True
elif event.key==K_s:
keys[2]=True
elif event.key==K_d:
keys[3]=True

if event.type==pygame.KEYUP:
if event.key==K_w:
keys[0]=False
elif event.key==K_a:
keys[1]=False
elif event.key==K_s:
keys[2]=False
elif event.key==K_d:
keys[3]=False
#-----------------------------------
# 按键 的操作
if keys[0]==True:
if self.y > 0:
self.y-=0.3
if keys[1]==True:
if self.x > 0:
self.x-=0.3
if keys[2]==True:
if self.y < 600 - feiji.image.get_height():
self.y+=0.3
if keys[3]==True:
if self.x < 300 - feiji.image.get_width():
self.x+=0.3

#-----------------------------------
#---------------END-----------------

class ShiTou:

def __init__(self):
self.x = random.randint(0, 250)
self.y = random.randint(-150, 0)
self.image = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\shitou.png')
self.active = False
self.speed = random.random() + 0.2

#重置石头
def restart(self):
self.x=random.randint(0, 250)
self.y=random.randint(-200, 0)
self.active = True

#石头的下落
def down(self):
if self.active:
if self.y < 600:
self.y+=self.speed			#设置石头下落速度
if self.y > 600:
self.active = False
else:
self.restart()
#-----------------------------------
#---------------END-----------------

class ZiDan:

def __init__(self):
self.x=feiji.x
self.y=feiji.y
self.image_2 = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\zidan.png')
self.image_1 = pygame.transform.rotate(self.image_2, 30)
self.image_3 = pygame.transform.rotate(self.image_2, 150)
self.active = False
self.speed = 1

#重置子弹
def restart(self):
self.active = True
self.y=feiji.y +15
self.x=feiji.x + 7

#子弹的发射
def she1(self):
if self.active:
self.y-=self.speed
self.x-=self.speed
if (self.y < 0) and (self.x < 0) :
self.active = False
else:
self.y=-20
self.x=-20

def she2(self):
if self.active:
self.y-=self.speed
if self.y < 0:
self.active = False
else:
self.y=-20
self.x=-20

def she3(self):
if self.active:
self.y-=self.speed
self.x+=self.speed
if (self.y < 0) and (self.x > 300) :
self.active = False
else:
self.y=-20
self.x=-20

#-----------------------------------
#--------
4000
-------END-----------------

#创建石头击落函数
#判断子弹是否击中石头,只需要判断子弹是否穿过石头。
#方法--判断子弹的x坐标是否大于石头图像左边的x坐标或者是否小于石头图像右边的x坐标
def CheckHit(zidan, shitou):
if(shitou.x < zidan.x and zidan.x < shitou.x + shitou.image.get_width()) and (shitou.y < zidan.y and zidan.y < shitou.y + shitou.image.get_height()):
shitou.active = False
zidan.active = False
return True
return False

#创建飞机坠毁函数
#方法参考“CheckHit”
def CheckTouch(shitou, feiji):
if (shitou.x + shitou.image.get_width() > feiji.x and feiji.x + feiji.image.get_width() > shitou.x) and (shitou.y + shitou.image.get_height() > feiji.y and feiji.y + feiji.image.get_height() > shitou.y):
return True
return False

#-------------------------------------------------------------------------------------------------------------

#---------------------设置游戏常数------------------------------------------------------------------------------
width,height=(300,600)										#设置游戏窗口大小
bg=pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\black.png')#设置背景图路径
screen=pygame.display.set_mode((width,height))				#新建一个游戏窗口,窗口大小为(width,height)
font = pygame.font.Font(None, 32)							#游戏运行时的字体
font_over = pygame.font.Font(None, 128)						#游戏结束时的字体
pygame.display.set_caption("test-game")						#设置游戏窗口标题
keys = [False, False, False, False, False]					#按键初始化(w,a,s,d,r)
gameover = False											#游戏状态
jifen = 0													#初始化积分
zidan1_jiange = 0											#子弹发射间隔时间
zidan2_jiange = 0
zidan3_jiange = 0
zidan_index = 0												#引导子弹组
#------------------------END----------------------------------------------------------------------------------
#----------------------实例化类--------------------------------------------------------------------------------
feiji = FeiJi(200,500)										#实例化-飞机

shitou = []													#实例化-石头组
for s in range(5):											#设置实例化数目
shitou.append(ShiTou())									#-----------(5个)

zidan1 = []
for z in range(6):											#设置实例化数目
zidan1.append(ZiDan())

zidan2 = []
for z in range(6):											#设置实例化数目
zidan2.append(ZiDan())

zidan3 = []
for z in range(6):											#设置实例化数目
zidan3.append(ZiDan())

#------------------------END----------------------------------------------------------------------------------
while 1:#游戏主循环

for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
exit(0)

#screen.fill(0)												#将没有图像的空间填满(黑色)
screen.blit(bg, (0,0))										#载入背景图
screen.blit(feiji.image, (feiji.x,feiji.y))					#加载飞机图像

if not gameover:											#判断游戏状态
text_jifen = font.render("Score: %d" %jifen, 0, (0, 0, 0))#文本-积分统计
screen.blit(text_jifen, (0, 0))							#载入文本(积分统计)
feiji.move()											#调用 飞机 移动的方法

for s in shitou:										#加载 石头组 图像并且调用石头下落的方法
if CheckTouch(s, feiji):
gameover = True
s.down()
screen.blit(s.image, (s.x,s.y))

zidan1_jiange -= 1									#设置子弹组循环发射
if zidan1_jiange < 0:
zidan1_jiange = 300						#子弹发射间隔
zidan1[zidan_index].restart()						#子弹组发射
zidan_index = (zidan_index + 1) % 6					#设置子弹组循环发射

zidan2_jiange -= 1									#设置子弹组循环发射
if zidan2_jiange < 0:
zidan2_jiange = 300						#子弹发射间隔
zidan2[zidan_index].restart()						#子弹组发射
zidan_index = (zidan_index + 1) % 6					#设置子弹组循环发射

zidan3_jiange -= 1									#设置子弹组循环发射
if zidan3_jiange < 0:
zidan3_jiange = 300						#子弹发射间隔
zidan3[zidan_index].restart()						#子弹组发射
zidan_index = (zidan_index + 1) % 6					#设置子弹组循环发射

#
for z in zidan1:											#加载 子弹组 图像并且调用子弹射击的方法
z.she1()
screen.blit(z.image_1, (z.x,z.y))
for s in shitou:
if (CheckHit(z, s)):							#调用 检查击中 函数,判断子弹是否击中 石头
jifen+=100         							#当子弹 击中 石头,积分加 100

for z in zidan2:											#加载 子弹组 图像并且调用子弹射击的方法
z.she2()
screen.blit(z.image_2, (z.x,z.y))
for s in shitou:
if (CheckHit(z, s)):							#调用 检查击中 函数,判断子弹是否击中 石头
jifen+=100         							#当子弹 击中 石头,积分加 100

for z in zidan3:											#加载 子弹组 图像并且调用子弹射击的方法
z.she3()
screen.blit(z.image_3, (z.x,z.y))
for s in shitou:
if (CheckHit(z, s)):							#调用 检查击中 函数,判断子弹是否击中 石头
jifen+=100         							#当子弹 击中 石头,积分加 100

else:
text_restart = font.render("按 R 键重新游戏", 1, (0, 0, 0))	#文本-游戏restart
screen.blit(text_restart, (100,350))						#载入文本(游戏restart)
text_over = font.render("Game Over", 1, (0, 0, 0))			#文本-游戏结束
screen.blit(text_over, (100,250))							#载入文本(游戏结束)
text_jifen = font.render("Score: %d" %jifen, 1, (0, 0, 0))	#文本-获取的积分数
screen.blit(text_jifen, (100, 300))							#载入文本(获取的积分数)
if event.type==pygame.KEYDOWN:
if event.key==K_r:										#按R键重新游戏
keys[4]=True
if event.type==pygame.KEYUP:
if event.key==K_r:
keys[4]=False
if keys[4]==True:
jifen = 0												#积分清零
screen.blit(feiji.image, (200,500))						#加载飞机图像
for s in shitou:
s.restart()
for z in zidan1:
z.active = False
for z in zidan2:
z.active = False
for z in zidan3:
z.active = False
keys = [False, False, False, False, False]				#按键初始化(w,a,s,d,r)
gameover = False

#刷新屏幕
pygame.display.flip()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: