# -*- coding: utf-8 -*-
import pygame
from myGameRole import *
# 导入按键的检测
from pygame.locals import *
import time
import random
if __name__ == '__main__':
# pygame初始化
pygame.init()
# 载入效果音乐
bullet_sound = pygame.mixer.Sound('resources/sound/bullet.wav')
enemy_down_sound = pygame.mixer.Sound('resources/sound/enemy1_down.wav')
game_over_sound = pygame.mixer.Sound('resources/sound/game_over.wav')
bullet_sound.set_volume(0.3)
enemy_down_sound.set_volume(0.3)
game_over_sound.set_volume(0.3)
# 载入游戏音乐并循环播放
pygame.mixer.music.load('resources/sound/game_music.wav')
pygame.mixer.music.play(-1, 0)
pygame.mixer.music.set_volume(0.25)
# 创建窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 读出背景图片
background = pygame.image.load('resources/image/background.png').convert()
# 创建对象
playerPlane = Player(screen)
# 敌机列表
enemyList = []
# 敌机产生频率
enemy_frequency = 0
running_control = True
gameOverFlag = False
# 1.显示背景
while running_control:
screen.blit(background,(0, 0))
# 产生敌方飞机
if enemy_frequency % 50 == 0:
num = random.randint(0, SCREEN_WIDTH - 51)
enemy = Enemy(screen, num)
enemyList.append(enemy)
enemy_frequency += 1
if enemy_frequency >= 100:
enemy_frequency = 0
# 判断按键
for event in pygame.event.get():
if event.type == QUIT:
print('exit')
exit()
elif event.type == KEYDOWN:
if event.key == K_a or event.key == K_LEFT:
# print('left')
playerPlane.keyHandle('left')
elif event.key == K_d or event.key == K_RIGHT:
# print('right')
playerPlane.keyHandle('right')
elif event.key == K_w or event.key == K_UP:
# print('up')
playerPlane.keyHandle('up')
elif event.key == K_s or event.key == K_DOWN:
# print('down')
playerPlane.keyHandle('down')
elif event.key == K_SPACE:
# print('space')
playerPlane.keyHandle('space')
bullet_sound.play()
# print(len(playerPlane.bulletList))
# 这里只是用于检测敌机的状态,如果出界就直接移除,如果被子弹打中就执行爆炸效果随后与子弹一起移除
# 还有一点,图片刷新不能与此同时进行,会有一顿一顿的感觉,所以只在执行爆炸效果时进行刷新,最后统一刷新
for enemy in enemyList:
enemy.move()
if enemy.checkOut():
enemyList.remove(enemy)
continue
x1,y1,w1,h1 = enemy.getPos()
for bullet in playerPlane.bulletList:
x2,y2,w2,h2 = bullet.getPos()
if (x2 + w2 // 2) >= x1 and (x2 + w2 // 2) <= (x1 + w1):
if y2 <= (y1 + h1):
enemy.crash()
playerPlane.bulletList.remove(bullet)
enemyList.remove(enemy)
enemy.draw()
enemy_down_sound.play()
break
x3,y3,w3,h3 = playerPlane.getPos()
if (x1 + w1 // 2) >= x3 and (x1 + w1 // 2) <= (x3 + w3):
if y3 <= (y1 + h1):
# playerPlane.crash()
# # 更新飞机图片
# playerPlane.draw()
# running_control = False
# break
gameOverFlag = True
for enemy in enemyList:
enemy.draw()
# 子弹移动,更新图片
for bullet in playerPlane.bulletList:
bullet.move()
bullet.draw()
# 清楚发射到顶部的子弹
playerPlane.bulletClear()
if gameOverFlag == True:
playerPlane.crash()
running_control = False
game_over_sound.play()
# 更新飞机图片
playerPlane.draw()
# 整个界面刷新
pygame.display.update()
# 延时0.01s
time.sleep(0.03)
# 游戏结束,退出循环
while True:
# 读出背景图片
background = pygame.image.load('resources/image/gameover.png').convert()
screen.blit(background, (0, 0))
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
print('exit')
exit()
time.sleep(0.03)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
MyPythonGame.zip (74个子文件)
MyPythonGame
myGameRole.py 3KB
resources
image
hero1.png 12KB
game_resume_pressed.png 2KB
shoot_copyright.png 39KB
life.png 3KB
enemy3_down3.png 59KB
enemy1_down3.png 5KB
enemy3_down2.png 52KB
enemy3_down1.png 47KB
enemy2_down2.png 11KB
enemy3_n1.png 41KB
enemy2_down4.png 3KB
enemy1_down1.png 3KB
hero_blowup_n4.png 3KB
hero_blowup_n2.png 16KB
game_loading2.png 5KB
enemy2_down3.png 13KB
enemy3_down5.png 73KB
enemy3_down6.png 9KB
hero_blowup_n1.png 15KB
bullet2.png 485B
enemy1_down2.png 4KB
enemy3_hit.png 44KB
background.png 36KB
game_pause_nor.png 2KB
hero2.png 12KB
enemy3_down4.png 65KB
bullet1.png 486B
enemy1.png 3KB
gameover.png 20KB
btn_finish.png 9KB
hero_blowup_n3.png 17KB
enemy2.png 8KB
enemy2_down1.png 10KB
ufo1.png 7KB
game_over.png 44KB
game_loading1.png 3KB
game_loading4.png 101B
ufo2.png 7KB
enemy1_down4.png 2KB
game_pause_pressed.png 2KB
enemy3_n2.png 42KB
enemy2_hit.png 9KB
game_loading3.png 6KB
game_resume_nor.png 2KB
bomb.png 5KB
sound
achievement.wav 38KB
big_spaceship_flying.wav 104KB
game_over.mp3 14KB
enemy3_down.mp3 9KB
enemy3_down.wav 70KB
big_spaceship_flying.mp3 12KB
get_bomb.wav 23KB
game_over.wav 44KB
bullet.wav 8KB
button.mp3 7KB
use_bomb.mp3 10KB
enemy2_down.wav 14KB
enemy2_out.wav 52KB
get_bomb.mp3 9KB
game_music.mp3 115KB
get_double_laser.mp3 13KB
button.wav 11KB
enemy1_down.wav 17KB
bullet.mp3 8KB
out_porp.mp3 10KB
get_double_laser.wav 104KB
game_music.wav 1.04MB
use_bomb.wav 83KB
achievement.mp3 9KB
enemy1_down.mp3 11KB
enemy2_down.mp3 13KB
myGame.py 4KB
myGameRole.pyc 6KB
共 74 条
- 1
资源评论
- 小舒2019-02-23还行 非常棒
hongbin_xu
- 粉丝: 2987
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功