Skip to content

Commit

Permalink
添加安装依赖、实现暂停功能、打包项目
Browse files Browse the repository at this point in the history
  • Loading branch information
babyAnnie committed Jun 11, 2022
1 parent 9686332 commit 4e18851
Show file tree
Hide file tree
Showing 36 changed files with 255 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
build/
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/python_game-master.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
# python_game
### 这是一个射击类游戏。当有外星人入侵,火箭射击出子弹,被子弹击中的外星人就会消失。
# The game Alien Invasion

- [Game Description] (#Game Description)
- [Operating Environment] (#Operating Environment)
- [Page Effects] (#Page Effects)
- [Game download] (#Game download)

## Game Description

This is a shooting game. When an alien invades, the rocket shoots out a bullet, and the alien hit by the bullet will disappear.

## Operating Environment
> pygame
> certifi==2019.11.28
> chardet==3.0.4
> idna==2.8
> soupsieve==1.9.5
> urllib3==1.25.7
## Page Effects
<img src="./images/game.png" alt="Page renderings" />

## Game download
[Download alien invasion game]()
38 changes: 38 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 游戏《外星人入侵》

- [游戏说明](#游戏说明)
- [运行环境](#运行环境)
- [页面效果](#页面效果)
- [游戏下载](#游戏下载)

## 游戏说明

这是一个射击类游戏。当有外星人入侵,火箭射击出子弹,被子弹击中的外星人就会消失。

## 运行环境
> pygame
> certifi==2019.11.28
> chardet==3.0.4
> idna==2.8
> soupsieve==1.9.5
> urllib3==1.25.7
windows安装pygame步骤:
```sh
python --version
```
1. 查看python版本,选择合适的pygame版本。如:我的是python3.7,所以就选择cp37
2. 查看系统版本,我的是64位的,所以我选择win_amd64
3. 确定好下载版本信息,在[pygame官网](https://github.com/pygame/pygame/releases)下载相应的pygame文件
4. 安装pygame
```sh
pip install --user xxx.whl(pygame文件名)
```

其他系统自己探索安装。

## 页面效果
<img src="./images/game.png" alt="页面效果图" />

## 游戏下载
[下载《外星人入侵》游戏]()
Binary file added __pycache__/alien.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/all_music.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bullet.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/button.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/game_functions.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/game_stats.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/scoreboard.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/settings.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/ship.cpython-37.pyc
Binary file not shown.
Binary file added alien.ico
Binary file not shown.
10 changes: 9 additions & 1 deletion alien.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import pygame
from pygame.sprite import Sprite
# 获取路径
import os, sys


def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)


class Alien(Sprite):
Expand All @@ -12,7 +20,7 @@ def __init__(self, ai_settings, screen):
self.ai_settings = ai_settings

# 加载外星人图像,并设置其rect属性
self.image = pygame.image.load('images/alien.bmp')
self.image = pygame.image.load(get_resource_path('images/alien.png'))
self.rect = self.image.get_rect()

# 每个外星人最初都在屏幕左上角附近
Expand Down
5 changes: 3 additions & 2 deletions alien_invasion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pygame
import game_functions as gf
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
from game_stats import GameStats
from button import Button
Expand All @@ -20,6 +20,7 @@ def run_game():

# 创建Play按钮
play_button = Button(ai_settings, screen, "Play")
continue_button = Button(ai_settings, screen, "Continue")

# 创建一个用于存储游戏统计信息的实例, 并创建记分牌
stats = GameStats(ai_settings)
Expand All @@ -43,7 +44,7 @@ def run_game():
ship.update()
gf.update_bullets(ai_settings, screen, stats, sb, ship, bullets, aliens)
gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets)
gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, continue_button)


run_game()
45 changes: 45 additions & 0 deletions alien_invasion.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
['alien_invasion.py'],
pathex=[],
binaries=[],
datas=[('images/alien.png', 'images'),('images/ship.bmp','images'),('high_score.json','.'),('music/Explo_Large.wav','music'),('music/Explo_Small.wav','music'),('music/Bullet_Whiz.wav','music'),('music/order_music.mp3','music')],
hiddenimports=['pygame.pkgdata'],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='alien_invasion',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon='alien.ico',
)
16 changes: 12 additions & 4 deletions all_music.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import pygame
import os
import sys


def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)


def explosion_large():
# 外星人到达了屏幕底部或者撞到飞船的大爆炸声
explosion_large = pygame.mixer.Sound("music/Explo_Large.wav")
explosion_large = pygame.mixer.Sound(get_resource_path("music/Explo_Large.wav"))
explosion_large.play()


def explosion_small():
# 增加子弹和外星人碰撞的小爆炸声
explosion_small = pygame.mixer.Sound("music/Explo_Small.wav")
explosion_small = pygame.mixer.Sound(get_resource_path("music/Explo_Small.wav"))
explosion_small.play()


def bullet_whiz():
# 增加子弹射出的biu声
bullet_whiz = pygame.mixer.Sound("music/Bullet_Whiz.wav")
bullet_whiz = pygame.mixer.Sound(get_resource_path("music/Bullet_Whiz.wav"))
bullet_whiz.play()


def bg_music():
# 游戏背景音乐(若游戏开始就一直播放)
pygame.mixer.music.load("music/order_music.mp3")
pygame.mixer.music.load(get_resource_path("music/order_music.mp3"))

2 changes: 1 addition & 1 deletion button.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, ai_settings, screen, msg):

# 设置按钮的尺寸和其他属性
self.width, self.height = 200, 50
self.button_color = (0, 255, 0)
self.button_color = (33, 33, 33)
self.text_color = (255, 255, 255)
self.font = pygame.font.SysFont(None, 48)

Expand Down
56 changes: 46 additions & 10 deletions game_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@
from time import sleep
import json
import all_music as am
import os
import sys


def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)


def play_bg_music():
# 检查音乐流播放,有返回True,没有返回False 如果没有音乐流则选择播放
# 检查背景音乐流播放,有返回True,没有返回False 如果没有音乐流则选择播放
""" 此方法同Sound方法一样!都是以流的方式呈现。一直播放需要使用while循环!"""
if not pygame.mixer.music.get_busy():
pygame.mixer.music.play(loops=0,start=0)


def pause_bg_music():
# 暂停背景音乐
""" 此方法同Sound方法一样!都是以流的方式呈现。一直播放需要使用while循环!"""
if not pygame.mixer.music.get_busy():
pygame.mixer.music.play()
pygame.mixer.music.pause()


def check_high_score(stats, sb):
Expand Down Expand Up @@ -140,14 +155,30 @@ def check_keydown_events(event, ai_settings, screen, ship, bullets, stats, sb, a
ship.moving_left = True
elif event.key == pygame.K_SPACE:
fire_bullet(ai_settings, screen, ship, bullets)
elif event.key == pygame.K_q:
with open('high_score.json', 'w') as f_obj:
elif event.key == pygame.K_ESCAPE:
# 退出游戏
with open(get_resource_path('high_score.json'), 'w') as f_obj:
json.dump(stats.high_score, f_obj)
pygame.quit()
sys.exit()
elif event.key == pygame.K_p:
elif event.key == pygame.K_r:
# 重新开始游戏
start_play_game(ai_settings, screen, stats, sb, ship, aliens, bullets)
stats.game_active = True
stats.continue_active = False
elif event.key == pygame.K_w:
sleep(5)
# 暂停游戏
# 显示光标
pygame.mouse.set_visible(True)
stats.continue_active = True
stats.game_active = False
# pause_bg_music()
# sleep(3600)
elif event.key == pygame.K_c:
# 继续游戏
pygame.mouse.set_visible(False)
stats.continue_active = False
stats.game_active = True


def check_keyup_events(event, ship):
Expand All @@ -162,7 +193,9 @@ def check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens,
""" 在玩家单击Play按钮时开始新游戏"""
button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
if button_clicked:
start_play_game(ai_settings, screen, stats, sb, ship, aliens, bullets)
# start_play_game(ai_settings, screen, stats, sb, ship, aliens, bullets)
stats.game_active = True
stats.continue_active = False


def start_play_game(ai_settings, screen, stats, sb, ship, aliens, bullets):
Expand Down Expand Up @@ -195,8 +228,9 @@ def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bull
""" 响应按键和鼠标事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
with open('high_score.json', 'w') as f_obj:
with open(get_resource_path('high_score.json'), 'w') as f_obj:
json.dump(stats.high_score, f_obj)
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, ai_settings, screen, ship, bullets, stats, sb, aliens)
Expand All @@ -207,7 +241,7 @@ def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bull
check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y)


def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button):
def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, continue_button):
""" 更新屏幕上的图像,并切换到新屏幕"""
# 每次循环时都会重绘屏幕
screen.fill(ai_settings.bg_color)
Expand All @@ -219,8 +253,10 @@ def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_bu
# 显示得分
sb.show_score()
# 如果游戏处于非活动状态,就绘制Play按钮
if stats.game_active is False:
if (stats.game_active is False) & (stats.continue_active is False):
play_button.draw_button()
elif stats.continue_active is True:
continue_button.draw_button()
# 让最近绘制的屏幕可见
pygame.display.flip()

Expand Down
Loading

0 comments on commit 4e18851

Please sign in to comment.