python-小游戏(打飞机)v1.1

来源:互联网 发布:aoi软件测试工程师 编辑:程序博客网 时间:2024/06/11 21:30

---------------------------------备注-----------------------------------

运行环境 :

python --v3.2.5

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

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

# -*- coding: utf-8 -*-import pygameimport randomfrom pygame.locals import *from sys import exitpygame.init()class FeiJi:def __init__(self,x,y):self.x = xself.y = yself.image = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\feiji.png')def restart(self):self.x = 200self.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]=Trueelif event.key==K_a:keys[1]=Trueelif event.key==K_s:keys[2]=Trueelif event.key==K_d:keys[3]=Trueif event.type==pygame.KEYUP:if event.key==K_w:keys[0]=Falseelif event.key==K_a:keys[1]=Falseelif event.key==K_s:keys[2]=Falseelif event.key==K_d:keys[3]=False#-----------------------------------# 按键 的操作if keys[0]==True:if self.y > 0:self.y-=0.1if keys[1]==True:if self.x > 0:self.x-=0.1if keys[2]==True:if self.y < 600 - feiji.image.get_height():self.y+=0.1if keys[3]==True:if self.x < 300 - feiji.image.get_width():self.x+=0.1#-----------------------------------#---------------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#设置石头下落速度def speed(self):self.y+=0.1#重置石头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.speed()if self.y > 600:self.active = Falseelse:self.restart()#-----------------------------------#---------------END-----------------class ZiDan:def __init__(self):self.x = feiji.x + 7self.y = feiji.yself.image = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\zidan.png')self.active = False#设置子弹发射速度def speed(self):self.y-=0.8#重置子弹def restart(self):self.active = Trueself.y=feiji.yself.x=feiji.x + 7#子弹的发射def she(self):if self.active:self.speed()if self.y < 0:self.active = Falseelse:self.restart()#-----------------------------------#---------------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#游戏状态gameover = Falsefont = pygame.font.Font(None, 32)font_over = pygame.font.Font(None, 128)#飞机实例feiji = FeiJi(200,500)#石头实例组shitou = []for i in range(5):shitou.append(ShiTou())#子弹实例组zidan = []for i in range(5):zidan.append(ZiDan())#子弹发射间隔zidan_jiange = 1200#引导子弹组zidan_index = 0#初始化积分jifen = 0#按键初始化(w,a,s,d,r)keys = [False, False, False, False]#设置游戏窗口大小width,height = 300,600#新建一个游戏窗口,窗口大小为(width,height)screen=pygame.display.set_mode((width,height))#游戏窗口标题pygame.display.set_caption("test-game")bg = pygame.image.load('C:\\Users\\bad\\Desktop\\python-test\\image\\black.png').convert()#游戏主循环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 = Trues.down()screen.blit(s.image, (s.x,s.y))#设置子弹组循环发射--------------------------------zidan_jiange -= 1if zidan_jiange < 0:zidan_jiange = 1200#子弹发射间隔zidan[zidan_index].restart()#子弹组发射zidan_index = (zidan_index + 1) % 5 #设置子弹组循环发射#加载 子弹组 图像并且调用子弹射击的方法---------------for z in zidan:z.she()screen.blit(z.image, (z.x,z.y))for s in shitou:if (CheckHit(z, s)):#调用 检查击中 函数,判断子弹是否击中 石头jifen+=100          #当子弹 击中 石头,积分加 100 else:#文本-游戏restarttext_restart = font.render("按 R 键重新游戏", 1, (0, 0, 0))screen.blit(text_restart, (100,350))#文本-游戏状态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:jifen = 0for z in zidan:z.restart()for s in shitou:s.restart()#初始化飞机feiji.restart()#按键初始化(w,a,s,d,r)keys = [False, False, False, False]gameover = False#刷新屏幕pygame.display.flip()

0 0
原创粉丝点击