Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
Update: sound volumn control.
Browse files Browse the repository at this point in the history
Fix: Bugs in the settings.
  • Loading branch information
JesseSenior committed Jan 12, 2023
1 parent c067303 commit 1ccab9e
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 78 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ A python based gobang game.

## Status

The game has currently been released version v0.1.0, currently to basically complete the functional goals, but still **may exist huge amount of bugs**. :(
The game has currently been released version v0.2.1, currently to basically complete the functional goals.

If you find a bug, feel free to write issues and let me know ~ 😉

The game uses pygame as the graphics engine and uses sqlite to record historical games.
The game uses pygame as the graphics engine and uses SQLite to record historical games.

The human-computer part of the game is modified from @Xshellye 's open source project [GoBang-python-homework](https://github.com/Xshellye/GoBang-python-homework).

Expand Down
6 changes: 3 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

## 状态

游戏当前已发布版本v0.1.1,目前以基本完成功能性目标,但仍然**可能存在巨量BUG**。 :(
游戏当前已发布版本v0.2.1,目前已基本完成功能性目标。

如果发现BUG,欢迎写issue踢我~😉
如果发现BUG,欢迎拿issue砸我~😉

游戏使用pygame作为图形引擎,利用sqlite记录历史棋局
游戏使用pygame作为图形引擎,利用SQLite记录历史棋局

游戏人机对战部分修改自@Xshellye的开源项目[GoBang-python-homework](https://github.com/Xshellye/GoBang-python-homework)

Expand Down
Binary file modified doc/Project Report.pdf
Binary file not shown.
38 changes: 33 additions & 5 deletions doc/Project Report.tex
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ \section{项目实现}
\end{center}
\end{multicols}

截止本文档完成时,本项目为v0.2.0版本。项目源代码以及预编译二进制文件可以从\href{https://github.com/JesseSenior/pyGobang}{JesseSenior/pyGobang}获得,同时可以从\href{https://www.bilibili.com/video/BV1iL4y1N79m}{Bilibili}观看本项目v0.1.1版本的演示视频。
截止本文档完成时,本项目为v0.2.1版本。项目源代码以及预编译二进制文件可以从\href{https://github.com/JesseSenior/pyGobang}{JesseSenior/pyGobang}获得,同时可以从\href{https://www.bilibili.com/video/BV1iL4y1N79m}{Bilibili}观看本项目v0.1.1版本的演示视频。

\section{项目目标完成情况}

Expand All @@ -309,9 +309,37 @@ \section{项目目标完成情况}

\subsection{源代码总行数统计结果}

\begin{figure}[H]
\centering
\includegraphics[width=.4\columnwidth]{Line Count}
\end{figure}
\begin{minted}{shell}
LINES | FILE
-----------|--------------------
28 | ./pyGobang.py
239 | ./src/ai.py
103 | ./src/constants.py
223 | ./src/core.py
117 | ./src/database.py
51 | ./src/main.py
79 | ./src/players.py
0 | ./src/__init__.py
205 | ./display/effect.py
175 | ./display/texture.py
57 | ./display/tool.py
0 | ./display/__init__.py
575 | ./screen/game_screen.py
135 | ./screen/init_screen.py
665 | ./screen/main_screen.py
99 | ./screen/__init__.py
138 | ./widget/background.py
459 | ./widget/board.py
174 | ./widget/button.py
349 | ./widget/input_box.py
116 | ./widget/logo.py
282 | ./widget/table.py
90 | ./widget/text.py
191 | ./widget/__init__.py
115 | ./test/core_test.py
67 | ./test/database_test.py
0 | ./test/__init__.py
Total Lines: 4732
\end{minted}

\end{document}
Binary file removed doc/figures/Line Count.png
Binary file not shown.
4 changes: 2 additions & 2 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"

MUTE_SOUND = False
SOUND_VOLUME = 1.0

BLINK_PERIOD = 60 / 110 # BPM of the sound

EFFECT_DURATION_TINY = BLINK_PERIOD / 4 # Switch Status
Expand Down Expand Up @@ -58,8 +60,6 @@ def res_path(path):
return os.path.join(RESPATH, path)


AI_ABILITY = 7500

pygame.freetype.init()
TEXT_FONT = pygame.freetype.Font(
res_path("font/sarasa-mono-sc-nerd/sarasa-mono-sc-nerd-regular.ttf"),
Expand Down
6 changes: 2 additions & 4 deletions src/display/screen/game_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pygame.locals import QUIT

import src.constants
from src.display.tool import play_sound
from src.constants import (
COLOR_BLACK,
COLOR_RED,
Expand All @@ -35,8 +36,6 @@
EFFECT_DURATION_TINY,
EFFECT_DURATION_MINI,
EFFECT_DURATION_NORMAL,
MUTE_SOUND,
res_path,
)
from src.core import Board
from src.display.effect import (
Expand Down Expand Up @@ -443,8 +442,7 @@ def _shift_out(self, event: pygame.event.Event = None):
except:
pass

if not MUTE_SOUND:
pygame.mixer.Sound(res_path("sound/sound3.ogg")).play()
play_sound("sound/sound3.ogg")

def onexit(event):
self._visible = False
Expand Down
6 changes: 2 additions & 4 deletions src/display/screen/init_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
from pygame.locals import QUIT

import src.constants
from src.display.tool import play_sound
from src.display.screen import Screen
from src.constants import (
SCREEN_CHANGE,
WINDOW_SIZE,
EFFECT_DURATION_MINI,
EFFECT_DURATION_NORMAL,
MUTE_SOUND,
res_path,
)
from src.display.effect import (
temporary_flag,
Expand Down Expand Up @@ -112,8 +111,7 @@ def on_exit(event):
self._stop_loop = event.screen

def delayed_shift():
if not MUTE_SOUND:
pygame.mixer.Sound(res_path("sound/sound3.ogg")).play()
play_sound("sound/sound3.ogg")
return mosaic_effect(
self._surface,
"ease_out",
Expand Down
87 changes: 65 additions & 22 deletions src/display/screen/main_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
MAINSCREEN_BOARD_SIZE,
EFFECT_DURATION_MINI,
EFFECT_DURATION_NORMAL,
MUTE_SOUND,
res_path,
)
from src.core import Board
from src.display.tool import play_sound
from src.display.effect import (
mosaic_effect,
surface_mosaic,
Expand Down Expand Up @@ -132,8 +131,7 @@ def _shift_out(self, event: pygame.event.Event = None):
except:
pass

if not MUTE_SOUND:
pygame.mixer.Sound(res_path("sound/sound3.ogg")).play()
play_sound("sound/sound3.ogg")

def onexit(event):
self._visible = False
Expand Down Expand Up @@ -560,36 +558,64 @@ def __init__(self, parent: MainScreen, rect: pygame.Rect) -> None:
rect.width,
rect.height / 11,
),
text="AI智商:",
text="游戏BGM大小:",
)
)

self._sub_widgets.append(
SettingMenu.setting_ai_ability_inputbox(
SettingMenu.setting_music_volume_inputbox(
self,
pygame.Rect(
0,
rect.height / 11 * 6,
rect.width,
rect.height / 11,
),
text_hint=str(src.constants.AI_ABILITY),
text_hint=str("{:.0%}".format(pygame.mixer.music.get_volume())),
)
)

self._sub_widgets.append(
Text(
self,
pygame.Rect(
0,
rect.height / 11 * 7,
rect.width,
rect.height / 11,
),
text="游戏音效大小:",
)
)

self._sub_widgets.append(
SettingMenu.setting_sound_volume_inputbox(
self,
pygame.Rect(
0,
rect.height / 11 * 8,
rect.width,
rect.height / 11,
),
text_hint=str("{:.0%}".format(src.constants.SOUND_VOLUME)),
)
)

@property
def board(self):
self.parent._board: BoardUI
return self.parent._board

class setting_board_size_inputbox(InputBox):
def _activate_out(self):
try:
tmp = eval(self.text)
if (
type(tmp) == tuple
and len(tmp) == 2
and type(tmp[0]) == int
and type(tmp[1]) == int
):
src.constants.DEFAULT_BOARD_SIZE = tmp
self._parent._board.load_board()
self.text_hint = str(tmp)
import re

tmp = re.findall("[0-9]+", self.text)
src.constants.DEFAULT_BOARD_SIZE = int(tmp[0]), int(tmp[1])
self._parent.board: BoardUI
self._parent.board.load_board()
self.text_hint = str(src.constants.DEFAULT_BOARD_SIZE)
except:
pass
self.text = ""
Expand All @@ -607,13 +633,30 @@ def _activate_out(self):
self.text = ""
super()._activate_out()

class setting_ai_ability_inputbox(InputBox):
class setting_music_volume_inputbox(InputBox):
def _activate_out(self):
try:
tmp = eval(self.text)
if type(tmp) == int and tmp > 0:
src.constants.AI_ABILITY = tmp
self.text_hint = str(tmp)
import re
pygame.mixer.music.set_volume(float(re.findall(r"\d+(?:\.\d+)?", self.text)[0]) / 100)

self.text_hint = str(
str("{:.0%}".format(pygame.mixer.music.get_volume()))
)
except:
pass
self.text = ""
super()._activate_out()

class setting_sound_volume_inputbox(InputBox):
def _activate_out(self):
try:
import re
src.constants.SOUND_VOLUME = float(re.findall(r"\d+(?:\.\d+)?", self.text)[0]) / 100
if src.constants.SOUND_VOLUME>1.0:
src.constants.SOUND_VOLUME=1.0
self.text_hint = str(
str("{:.0%}".format(src.constants.SOUND_VOLUME))
)
except:
pass
self.text = ""
Expand Down
8 changes: 8 additions & 0 deletions src/display/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
Description: Other practical tools for the display.
"""
import pygame
import src.constants

from PIL import Image


Expand Down Expand Up @@ -47,3 +49,9 @@ def surface_to_image(surface: pygame.Surface) -> Image.Image:
surface.get_size(),
pygame.image.tostring(surface, "RGBA", False),
)

def play_sound(sound_path):
if not src.constants.MUTE_SOUND:
sound=pygame.mixer.Sound(src.constants.res_path(sound_path))
sound.set_volume(src.constants.SOUND_VOLUME)
sound.play()
21 changes: 16 additions & 5 deletions src/display/widget/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
)

from src.display.widget import Widget
from src.display.tool import play_sound
from src.constants import (
COLOR_BLACK,
COLOR_BOARD,
Expand Down Expand Up @@ -65,16 +66,17 @@ def _place_a_piece(
) -> None:
if move == None:
move = self.get_move()
self._board_ui.place_a_piece(move, mute_sound)
self._board_ui.place_a_piece(move, mute_sound, self)


class MonkeyUIPlayer(UIPlayer, MonkeyPlayer):
def __init__(self, board_ui: BoardUI) -> None:
super().__init__(board_ui)

def _timer_tick(self, event: pygame.event.Event):
if self._timer_tick in self._board_ui._handlers[TIMER_TICK]:
self._board_ui._handlers[TIMER_TICK].remove(self._timer_tick)
for handler in self._board_ui._handlers[TIMER_TICK]:
if handler.__qualname__ == self._timer_tick.__qualname__:
self._board_ui._handlers[TIMER_TICK].remove(handler)
self._place_a_piece(mute_sound=True)

def place_a_piece(self) -> None:
Expand Down Expand Up @@ -233,9 +235,17 @@ def onexit():
self.editable = _editable
self.set_player_list(player_list)

def place_a_piece(self, pos: Tuple[int, int], mute_sound: bool = False):
def place_a_piece(
self,
pos: Tuple[int, int],
mute_sound: bool = False,
current_player: UIPlayer = None,
):
if not self.editable:
return
if current_player != None:
if current_player != self._player:
return

try:
piece = BoardUI.Piece(
Expand All @@ -247,8 +257,9 @@ def place_a_piece(self, pos: Tuple[int, int], mute_sound: bool = False):
)
except:
return

if not mute_sound:
pygame.mixer.Sound(res_path("sound/sound2.ogg")).play()
play_sound("sound/sound2.ogg")
self._sub_widgets.append(piece)

try:
Expand Down
Loading

0 comments on commit 1ccab9e

Please sign in to comment.