Version 2.0 using python-chess (also integrates Stockfish for chess AI) is published in my renpy-chess repository. It is under active and continuous maintenance.
Pros | Cons | |
---|---|---|
Ren'Py Chess 1.0 |
|
|
Ren'Py Chess 2.0 |
|
|
This repository contains the source code of a basic Chess Engine made with Ren'Py. The main purpose of this project is to demonstrate how to integrate a Mini-game into a Ren'Py Visual Novel with screen language and Ren'Py Displayable.
Within the Ren'Py Chess Game, there are two available gameplay modes, Player vs. Self and Player vs. Computer. Out of consideration for computation speed and VN players' expectations for a mini-game embedded in a Visual Novel, the Computer chess player is a chess AI of minimal implementation.
Player will be able to choose from the two available gameplay modes, Player vs. Self and Player vs. Computer. In Player vs. Self, the Player chooses moves for both Black and White. In Player vs. Computer, the Player plays as White by default.
Click on a piece and all of its available moves will be highlighted. Click on any of the available destination squares to make a move. Press <-
on the keyboard to undo moves.
The code for the Ren'Py Chess Game is in the Public Domain and can be used and / or modified in any free or commercial projects.
List of core files for the Chess Game:
images/bg chessboard.png
- the chess board imageimages/pieces_image
- the chess pieces imageschesslogic.py
- the rules and logic behind chesschessai.py
- the auto-player that evaluates and selects movechessgui.rpy
- the Renpy Displayable class and methodsscreens.rpy
- the mini-game screen holding the Displayablescript.rpy
- the game script that calls the mini-game screen
Copy the image files, chesslogic.py
, chessai.py
and chessgui.rpy
into your game/
directory.
Copy and paste the following code into specified .rpy
files.
In
screens.rpy
Note that ai_mode is a Boolean, for Player vs. Self, callscreen minigame(False)
and for Player vs. Computer, callscreen minigame(True)
## This screen is used for the chess game.
screen minigame(ai_mode):
default chess = ChessDisplayable(chess_ai=ai_mode)
add "bg chessboard"
add chess
if chess.winner:
timer 6.0 action Return(chess.winner)
In
script.rpy
(or any script file in which the chess game should occur) Note the wayscreen minigame()
is called with the variableai_mode
label start:
$ ai_mode = False
$ winner = None
"Welcome to the Ren'Py Chess Game!"
label opponent_selection:
menu:
"Please select the game mode."
"Player vs. Self":
$ ai_mode = False
"Player vs. Computer":
$ ai_mode = True
# Player plays White by default
$ player_color = 'White'
$ computer_color = 'Black'
window hide
# Start chess game
call screen minigame(ai_mode)
# End chess game
$ winner = _return
if ai_mode:
if winner == player_color:
"Congratulations! You won!"
elif winner == computer_color:
"The computer defeated you. Better luck next time!"
elif winner == 'draw':
"The game ended in a draw. See if you can win the next time!"
else:
if winner != 'draw':
"The winner is [winner]! Congratulations!"
else:
"The game ended in a draw."
By Ren'Py default configuration, this code assumes a window size of 1280 * 720
. If a different window size is used, the following changes will need to be made.
Files and directories of concern:
chesslogic.py
- change in configurationsimages/bg chessboard.png
- change in sizeimages/pieces_image
- change in size
bg chessboard.png
is a 1280 * 720
image, with the 720 * 720
chessboard at the center and two 280 * 720
black paddings on each side. A chessboard customized for your VN window should also position the chessboard at the center of the rectangular image.
The current chess pieces are of size 81 * 81
, fitting into each 90 * 90
square on the board. Make sure you change the size of these images accordingly to accomodate your window size.
Then make changes to the following configuration parameters in chesslogic.py
In
chesslogic.py
# Configurations for chess gui
# the leftmost coordinate of the chessboard
# (1280 - 720) / 2 = 280
X_MIN = 280
# the rightmost coordinate of the chessboard
# 1280 - (1280 - 720) / 2 = 1000
X_MAX = 1000
# the top coordinate of the chessboard
Y_MIN = 0
# the bottom coordinate of the chessboard
Y_MAX = 720
# the leftmost coordinate of the chessboard
# (1280 - 720) / 2 = 280
X_OFFSET = 280
# the size of each square on the chessboard
# 720 / 8 = 90
LOC_SIZE = 90
Files and directories of concern:
chessgui.rpy
- changes in displayed text and styleimages/bg chessboard.png
- changes in art styleimages/pieces_image
- changes in art style
Customize the art of the image files as you wish.
The following stylistic changes can be made in chessgui.rpy
.
In
chessgui.rpy
# customize the RGBA colors and texts
self.hover_image = Solid('#00ff0050', xsize=LOC_SIZE, ysize=LOC_SIZE)
self.clicked_image = Solid('#0a82ff88', xsize=LOC_SIZE, ysize=LOC_SIZE)
self.moves_image = Solid('#45b8ff88', xsize=LOC_SIZE, ysize=LOC_SIZE)
self.player_text = Text("Whose turn: White", color='#fff', size=26)
self.status_text = Text("")
To add new displayables in
chessgui.rpy
class ChessDisplayable(renpy.Displayable):
def __init__(self, chess_ai=None):
renpy.Displayable.__init__(self)
...
# create new displayables by setting color and text
self.image_displayable = Solid(color, **properties)
self.text_displayable = Text(text, **properties)
...
def render(self, width, height, st, at):
...
# render new displayables
image_displayable_render = renpy.render(self.image_displayable, width, height, st, at)
text_displayable_render = renpy.render(self.text_displayable, width, height, st, at)
...
# blit new displayables onto main render
# by setting xcoord and ycoord
render = renpy.Render(width, height)
render.blit(image_displayable_render, (xcoord_1, ycoord_1))
render.blit(text_displayable_render, (xcoord_2, ycoord_2))
...
return render
Chess GUI
- Ren'Py Displayables
- Ren'Py Creator-Defined Displayables
- Ren'Py Screens and Screen Language
- Pong Minigame from the Ren'Py Tutorial
Chess Logic
- UChicago CMSC 151 Project 1 & 2,
chess-logic.rkt
andchess-gui.rkt
Chess AI