赞
踩
---------------------------------备注-----------------------------------
运行环境 :
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.1
- if keys[1]==True:
- if self.x > 0:
- self.x-=0.1
- if keys[2]==True:
- if self.y < 600 - feiji.image.get_height():
- self.y+=0.1
- if 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 = False
- else:
- self.restart()
- #-----------------------------------
- #---------------END-----------------
-
- class ZiDan:
-
- def __init__(self):
- self.x = feiji.x + 7
- self.y = feiji.y
- self.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 = True
- self.y=feiji.y
- self.x=feiji.x + 7
- #子弹的发射
- def she(self):
- if self.active:
- self.speed()
- if self.y < 0:
- self.active = False
- else:
- 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 = False
-
- font = 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 = True
- s.down()
- screen.blit(s.image, (s.x,s.y))
-
- #设置子弹组循环发射--------------------------------
- zidan_jiange -= 1
- if 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:
- #文本-游戏restart
- text_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 = 0
- for 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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。