- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python pygame实现五子棋小游戏由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
今天学习了如何使用pygame来制作小游戏,下面是五子棋的代码,我的理解都写在注释里了 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
import
pygame
# 导入pygame模块
print
(pygame.ver)
# 检查pygame的版本,检查pygame有没有导入成功
empty
=
0
black
=
1
white
=
2
# 定义三个常量函数,用来表示白棋,黑棋,以及 空
black_color
=
[
0
,
0
,
0
]
# 定义黑色(黑棋用,画棋盘)
white_color
=
[
255
,
255
,
255
]
# 定义白色(白棋用)
# 定义棋盘这个类
class
renjuboard(
object
):
def
__init__(
self
):
# self._board = board = [[empty] * 15 for _ in range(15)]
# 将棋盘每一个交叉点都看作列表的一个元素位,一共有15*15共225个元素
self
._board
=
[[]]
*
15
self
.reset()
#重置棋盘
def
reset(
self
):
for
row
in
range
(
len
(
self
._board)):
self
._board[row]
=
[empty]
*
15
#定义棋盘上的下棋函数,row表示行,col表示列,is_black表示判断当前点位该下黑棋,还是白棋
def
move(
self
, row, col, is_black):
if
self
._board[row][col]
=
=
empty:
self
._board[row][col]
=
black
if
is_black
else
white
return
true
return
false
# 给棋盘定义一个函数将自己在screen上面画出来,使用pygame.draw()函数。并且顺便将下了的棋子也画出来
def
draw(
self
, screen):
for
h
in
range
(
1
,
16
):
pygame.draw.line(screen, black_color,
[
40
, h
*
40
], [
600
, h
*
40
],
1
)
pygame.draw.line(screen, black_color,
# 给棋盘加一个外框,使美观
pygame.draw.rect(screen, black_color, [
36
,
36
,
568
,
568
],
3
)
# 在棋盘上标出,天元以及另外4个特殊点位
pygame.draw.circle(screen, black_color, [
320
,
320
],
5
,
0
)
pygame.draw.circle(screen, black_color, [
160
,
160
],
3
,
0
)
pygame.draw.circle(screen, black_color, [
160
,
480
],
3
,
0
)
pygame.draw.circle(screen, black_color, [
480
,
160
],
3
,
0
)
pygame.draw.circle(screen, black_color, [
480
,
480
],
3
,
0
)
#做2次for循环取得棋盘上所有交叉点的坐标
for
row
in
range
(
len
(
self
._board)):
for
col
in
range
(
len
(
self
._board[row])):
# 将下在棋盘上的棋子画出来
if
self
._board[row][col] !
=
empty:
ccolor
=
black_color \
if
self
._board[row][col]
=
=
black
else
white_color
# 取得这个交叉点下的棋子的颜色,并将棋子画出来
pos
=
[
40
*
(col
+
1
),
40
*
(row
+
1
)]
# 画出棋子
pygame.draw.circle(screen, ccolor, pos,
18
,
0
)
# 定义函数,传入当前棋盘上的棋子列表,输出结果,不管黑棋白棋胜,都是传回false,未出结果则为true
def
is_win(board):
for
n
in
range
(
15
):
# 判断垂直方向胜利
flag
=
0
# flag是一个标签,表示是否有连续以上五个相同颜色的棋子
for
b
in
board._board:
if
b[n]
=
=
1
:
flag
+
=
1
if
flag
=
=
5
:
print
(
'黑棋胜'
)
return
false
else
:
# else表示此时没有连续相同的棋子,标签flag重置为0
flag
=
0
flag
=
0
for
b
in
board._board:
if
b[n]
=
=
2
:
flag
+
=
1
if
flag
=
=
5
:
print
(
'白棋胜'
)
return
false
else
:
flag
=
0
# 判断水平方向胜利
flag
=
0
for
b
in
board._board[n]:
if
b
=
=
1
:
flag
+
=
1
if
flag
=
=
5
:
print
(
'黑棋胜'
)
return
false
else
:
flag
=
0
flag
=
0
for
b
in
board._board[n]:
if
b
=
=
2
:
flag
+
=
1
if
flag
=
=
5
:
print
(
'白棋胜'
)
return
false
else
:
flag
=
0
# 判断正斜方向胜利
for
x
in
range
(
4
,
25
):
flag
=
0
for
i,b
in
enumerate
(board._board):
if
14
>
=
x
-
i >
=
0
and
b[x
-
i]
=
=
1
:
flag
+
=
1
if
flag
=
=
5
:
print
(
'黑棋胜'
)
return
false
else
:
flag
=
0
for
x
in
range
(
4
,
25
):
flag
=
0
for
i,b
in
enumerate
(board._board):
if
14
>
=
x
-
i >
=
0
and
b[x
-
i]
=
=
2
:
flag
+
=
1
if
flag
=
=
5
:
print
(
'白棋胜'
)
return
false
else
:
flag
=
0
#判断反斜方向胜利
for
x
in
range
(
11
,
-
11
,
-
1
):
flag
=
0
for
i,b
in
enumerate
(board._board):
if
0
<
=
x
+
i <
=
14
and
b[x
+
i]
=
=
1
:
flag
+
=
1
if
flag
=
=
5
:
print
(
'黑棋胜'
)
return
false
else
:
flag
=
0
for
x
in
range
(
11
,
-
11
,
-
1
):
flag
=
0
for
i,b
in
enumerate
(board._board):
if
0
<
=
x
+
i <
=
14
and
b[x
+
i]
=
=
2
:
flag
+
=
1
if
flag
=
=
5
:
print
(
'白棋胜'
)
return
false
else
:
flag
=
0
return
true
def
main():
# 创建棋盘对象
board
=
renjuboard()
# 用于判断是下黑棋还是白棋
is_black
=
true
# pygame初始化函数,固定写法
pygame.init()
pygame.display.set_caption(
'五子棋'
)
# 改标题
# pygame.display.set_mode()表示建立个窗口,左上角为坐标原点,往右为x正向,往下为y轴正向
screen
=
pygame.display.set_mode((
640
,
640
))
# 给窗口填充颜色,颜色用三原色数字列表表示
screen.fill([
125
,
95
,
24
])
board.draw(screen)
# 给棋盘类发命令,调用draw()函数将棋盘画出来
pygame.display.flip()
# 刷新窗口显示
running
=
true
# while 主循环的标签,以便跳出循环
while
running:
# 遍历建立窗口后发生的所有事件,固定写法
for
event
in
pygame.event.get():
# 根据事件的类型,进行判断
if
event.
type
=
=
pygame.quit:
running
=
false
elif
event.
type
=
=
pygame.keyup:
pass
# pygame.mousebuttondown表示鼠标的键被按下
elif
event.
type
=
=
pygame.mousebuttondown
and
\
event.button
=
=
1
:
# button表示鼠标左键
x, y
=
event.pos
# 拿到鼠标当前在窗口上的位置坐标
# 将鼠标的(x, y)窗口坐标,转化换为棋盘上的坐标
row
=
round
((y
-
40
)
/
40
)
col
=
round
((x
-
40
)
/
40
)
if
board.move(row, col, is_black):
is_black
=
not
is_black
screen.fill([
125
,
95
,
24
])
board.draw(screen)
pygame.display.flip()
# 调用判断胜负函数
if
not
is_win(board):
#break
running
=
false
# 这里我有个bug没找到解决办法,就是判断出胜负后,使用break跳出事件遍历的for循环,但是老是不能跳出来,导致胜负分出来了还可以继续下,这里我采用判断胜负后就将running标签赋值为false,跳出主循环,但是这样棋盘的窗口也没了。明天再找找bug在哪
pygame.quit()
if
__name__
=
=
'__main__'
:
main()
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/qq_41772997/article/details/79562378 。
最后此篇关于python pygame实现五子棋小游戏的文章就讲到这里了,如果你想了解更多关于python pygame实现五子棋小游戏的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Surface.blit在 1.8 中有一个新参数:混合。定义了以下值: BLEND_ADD BLEND_SUB BLEND_MULT BLEND_MIN BLEND_MAX BLEND_RGBA_A
import sys import pygame import pygame.locals as pgl class Test: def __init__(self): pyg
我对 PyGame 比较陌生。我正在尝试制作一个简单的程序来显示表示鼠标在屏幕上的位置的字符串。 import pygame, sys from pygame.locals import * pyga
我有总是在后台运行的音乐和一些在触发时会播放声音的事件。音乐效果很好。 pygame.mixer.music.load(os.path.join(SOUND_FOLDER, 'WateryGrave.
我有这些代码 FONT = pygame.font.Font("font/calibri.ttf", 50) FONT.size = 25 但是编译器说 AttributeError: 'pygame
有我正在导入的图像: look_1 = pygame.image.load('data\\png\\look1.png').convert_alpha() 我试图减少它的大小是这样的: pygame.
我正在为我的 pygame 制作一个帮助屏幕,每当我运行它时,我都会收到此错误消息: > self.surface.blit(self.helpscreen) TypeError: argument
在 pyGame 中应用程序,我想渲染 SVG 中描述的无分辨率 GUI 小部件。 我怎样才能做到这一点? (我喜欢 OCEMP GUI 工具包,但它的渲染似乎依赖于位图) 最佳答案 这是一个完整的例
有没有办法将多首歌曲加载到 Pygame 中?我不是在谈论这样的音效; crash_sound = pygame.mixer.Sound("crash.ogg") #and pygame.mixer.
我还有一个问题。当我尝试运行我的代码时,pygame 启动然后立即停止。 这是我的代码: import pygame import os import time import random pygam
我正在使用 pymunk 和 pygame 开发一个项目。我正在使用 PivotJoint 约束将我的 body 连接在一起。如果可能的话,我想让关节不可见 - 有什么办法可以做到这一点吗?现在关节在
我使用 fedora 20、Python 2.7 和 virtualenv 1.10.1。我想在 virtualenv 中安装 pygame,我得到了 You are installing a pot
尝试将文本添加到矩形中并使用箭头键在屏幕上移动矩形。我想让文字不会超出边缘。到目前为止,我已经在没有将其放入 Rect 中的情况下工作了,但我想让 Rect 函数工作。现在文本只是反弹回来,我不知道要
我是第一次玩 pygame(总的来说我是 python 的新手),想知道是否有人可以帮助我... 我正在制作一款小型射击游戏,希望能够为坏人创建一个类。我的想法是类应该继承自 pygame.Surfa
我在 Windows 10 机器上运行了 python 3.9.1。我通过 pip 在我的机器上安装了 pygame 2.0.1 (python -m pip install https://gith
错误: File "/home/alien/cncell/core/animator.py", line 413, in create_animation_from_data pygame
这是代码。 5000 个弹跳旋转的红色方块。 (16x16 png) 在 pygame 版本上,我获得 30 fps,但使用 pyglet 获得 10 fps。对于这种事情,OpenGl 不应该更快吗
我以为 pygame.font.Font 是用来加载 .ttf 字体的,如果没有 .ttf 文件在同一个目录下就无法加载字体,但我看过一个视频,有人在没有 .ttf 文件的情况下加载字体。 ttf 字
我正在尝试使用 Travis CI 设置一个项目。项目也使用 pygame。我曾多次尝试设置它 - 但它似乎失败了。 我得到的最接近的是以下内容: .travis.yml : language: py
这个问题已经有答案了: Python error "ImportError: No module named" (38 个回答) 已关闭 3 年前。 我正在使用 Mac 并输入 pip install
我是一名优秀的程序员,十分优秀!