Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
lesson4_4_2
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
06070a65
authored
Sep 11, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
5cc5aaf6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
276 additions
and
0 deletions
f.py
jj.py
kfdhj.py
f.py
0 → 100644
View file @
06070a65
import
os
import
sys
import
random
from
modules
import
from
PyQt5.QtGui
import
from
PyQt5.QtCore
import
from
PyQt5.QtWidgets
import
'''定义俄罗斯方块游戏类'''
class
TetrisGame
(
QMainWindow
):
def
__init__
(
self
,
parent
=
None
):
super
(
TetrisGame
,
self
)
.
__init__
(
parent
)
# 是否暂停ing
self
.
is_paused
=
False
# 是否开始ing
self
.
is_started
=
False
self
.
initUI
()
'''界面初始化'''
def
initUI
(
self
):
# icon
self
.
setWindowIcon
(
QIcon
(
os
.
path
.
join
(
os
.
getcwd
(),
'resources/icon.jpg'
)))
# 块大小
self
.
grid_size
=
22
# 游戏帧率
self
.
fps
=
200
self
.
timer
=
QBasicTimer
()
# 焦点
self
.
setFocusPolicy
(
Qt
.
StrongFocus
)
# 水平布局
layout_horizontal
=
QHBoxLayout
()
self
.
inner_board
=
InnerBoard
()
self
.
external_board
=
ExternalBoard
(
self
,
self
.
grid_size
,
self
.
inner_board
)
layout_horizontal
.
addWidget
(
self
.
external_board
)
self
.
side_panel
=
SidePanel
(
self
,
self
.
grid_size
,
self
.
inner_board
)
layout_horizontal
.
addWidget
(
self
.
side_panel
)
self
.
status_bar
=
self
.
statusBar
()
self
.
external_board
.
score_signal
[
str
]
.
connect
(
self
.
status_bar
.
showMessage
)
self
.
start
()
self
.
center
()
self
.
setWindowTitle
(
'Tetris —— 九歌'
)
self
.
show
()
self
.
setFixedSize
(
self
.
external_board
.
width
()
+
self
.
side_panel
.
width
(),
self
.
side_panel
.
height
()
+
self
.
status_bar
.
height
())
'''游戏界面移动到屏幕中间'''
def
center
(
self
):
screen
=
QDesktopWidget
()
.
screenGeometry
()
size
=
self
.
geometry
()
self
.
move
((
screen
.
width
()
-
size
.
width
())
//
2
,
(
screen
.
height
()
-
size
.
height
())
//
2
)
'''更新界面'''
def
updateWindow
(
self
):
self
.
external_board
.
updateData
()
self
.
side_panel
.
updateData
()
self
.
update
()
'''开始'''
def
start
(
self
):
if
self
.
is_started
:
return
self
.
is_started
=
True
self
.
inner_board
.
createNewTetris
()
self
.
timer
.
start
(
self
.
fps
,
self
)
'''暂停/不暂停'''
def
pause
(
self
):
if
not
self
.
is_started
:
return
self
.
is_paused
=
not
self
.
is_paused
if
self
.
is_paused
:
self
.
timer
.
stop
()
self
.
external_board
.
score_signal
.
emit
(
'Paused'
)
else
:
self
.
timer
.
start
(
self
.
fps
,
self
)
self
.
updateWindow
()
'''计时器事件'''
def
timerEvent
(
self
,
event
):
if
event
.
timerId
()
==
self
.
timer
.
timerId
():
removed_lines
=
self
.
inner_board
.
moveDown
()
self
.
external_board
.
score
+=
removed_lines
self
.
updateWindow
()
else
:
super
(
TetrisGame
,
self
)
.
timerEvent
(
event
)
'''按键事件'''
def
keyPressEvent
(
self
,
event
):
if
not
self
.
is_started
or
self
.
inner_board
.
current_tetris
==
tetrisShape
()
.
shape_empty
:
super
(
TetrisGame
,
self
)
.
keyPressEvent
(
event
)
return
key
=
event
.
key
()
# P键暂停
if
key
==
Qt
.
Key_P
:
self
.
pause
()
return
if
self
.
is_paused
:
return
# 向左
elif
key
==
Qt
.
Key_Left
:
self
.
inner_board
.
moveLeft
()
# 向右
elif
key
==
Qt
.
Key_Right
:
self
.
inner_board
.
moveRight
()
# 旋转
elif
key
==
Qt
.
Key_Up
:
self
.
inner_board
.
rotateAnticlockwise
()
# 快速坠落
elif
key
==
Qt
.
Key_Space
:
self
.
external_board
.
score
+=
self
.
inner_board
.
dropDown
()
else
:
super
(
TetrisGame
,
self
)
.
keyPressEvent
(
event
)
self
.
updateWindow
()
'''run'''
if
__name__
==
'__main__'
:
app
=
QApplication
([])
tetris
=
TetrisGame
()
sys
.
exit
(
app
.
exec_
())
jj.py
0 → 100644
View file @
06070a65
'''游戏开始界面'''
def
__init__
(
self
,
parent
=
None
,
**
kwargs
):
super
(
gameStartUI
,
self
)
.
__init__
(
parent
)
self
.
setFixedSize
(
760
,
650
)
self
.
setWindowTitle
(
'五子棋 —— 九歌'
)
self
.
setWindowIcon
(
QIcon
(
cfg
.
ICON_FILEPATH
))
# 背景图片
palette
=
QPalette
()
palette
.
setBrush
(
self
.
backgroundRole
(),
QBrush
(
QPixmap
(
cfg
.
BACKGROUND_IMAGEPATHS
.
get
(
'bg_start'
))))
self
.
setPalette
(
palette
)
# 按钮
# --人机对战
self
.
ai_button
=
PushButton
(
cfg
.
BUTTON_IMAGEPATHS
.
get
(
'ai'
),
self
)
self
.
ai_button
.
move
(
250
,
200
)
self
.
ai_button
.
show
()
self
.
ai_button
.
click_signal
.
connect
(
self
.
playWithAI
)
# --联机对战
self
.
online_button
=
PushButton
(
cfg
.
BUTTON_IMAGEPATHS
.
get
(
'online'
),
self
)
self
.
online_button
.
move
(
250
,
350
)
self
.
online_button
.
show
()
self
.
online_button
.
click_signal
.
connect
(
self
.
playOnline
)
'''人机对战'''
def
playWithAI
(
self
):
self
.
close
()
self
.
gaming_ui
=
playWithAIUI
(
cfg
)
self
.
gaming_ui
.
exit_signal
.
connect
(
lambda
:
sys
.
exit
())
self
.
gaming_ui
.
back_signal
.
connect
(
self
.
show
)
self
.
gaming_ui
.
show
()
'''联机对战'''
def
playOnline
(
self
):
self
.
close
()
self
.
gaming_ui
=
playOnlineUI
(
cfg
,
self
)
self
.
gaming_ui
.
show
()
'''run'''
if
__name__
==
'__main__'
:
app
=
QApplication
(
sys
.
argv
)
handle
=
gameStartUI
()
font
=
QFont
()
font
.
setPointSize
(
12
)
handle
.
setFont
(
font
)
handle
.
show
()
sys
.
exit
(
app
.
exec_
())
\ No newline at end of file
kfdhj.py
0 → 100644
View file @
06070a65
import
os
import
cfg
import
sys
import
pygame
import
random
from
modules
import
*
'''游戏初始化'''
def
initGame
():
# 初始化pygame, 设置展示窗口
pygame
.
init
()
screen
=
pygame
.
display
.
set_mode
(
cfg
.
SCREENSIZE
)
pygame
.
display
.
set_caption
(
'catch coins —— 九歌'
)
# 加载必要的游戏素材
game_images
=
{}
for
key
,
value
in
cfg
.
IMAGE_PATHS
.
items
():
if
isinstance
(
value
,
list
):
images
=
[]
for
item
in
value
:
images
.
append
(
pygame
.
image
.
load
(
item
))
game_images
[
key
]
=
images
else
:
game_images
[
key
]
=
pygame
.
image
.
load
(
value
)
game_sounds
=
{}
for
key
,
value
in
cfg
.
AUDIO_PATHS
.
items
():
if
key
==
'bgm'
:
continue
game_sounds
[
key
]
=
pygame
.
mixer
.
Sound
(
value
)
# 返回初始化数据
return
screen
,
game_images
,
game_sounds
'''主函数'''
def
main
():
# 初始化
screen
,
game_images
,
game_sounds
=
initGame
()
# 播放背景音乐
pygame
.
mixer
.
music
.
load
(
cfg
.
AUDIO_PATHS
[
'bgm'
])
pygame
.
mixer
.
music
.
play
(
-
1
,
0.0
)
# 字体加载
font
=
pygame
.
font
.
Font
(
cfg
.
FONT_PATH
,
40
)
# 定义hero
hero
=
Hero
(
game_images
[
'hero'
],
position
=
(
375
,
520
))
# 定义食物组
food_sprites_group
=
pygame
.
sprite
.
Group
()
generate_food_freq
=
random
.
randint
(
10
,
20
)
generate_food_count
=
0
# 当前分数/历史最高分
score
=
0
highest_score
=
0
if
not
os
.
path
.
exists
(
cfg
.
HIGHEST_SCORE_RECORD_FILEPATH
)
else
int
(
open
(
cfg
.
HIGHEST_SCORE_RECORD_FILEPATH
)
.
read
())
# 游戏主循环
clock
=
pygame
.
time
.
Clock
()
while
True
:
# --填充背景
screen
.
fill
(
0
)
screen
.
blit
(
game_images
[
'background'
],
(
0
,
0
))
# --倒计时信息
countdown_text
=
'Count down: '
+
str
((
90000
-
pygame
.
time
.
get_ticks
())
//
60000
)
+
":"
+
str
((
90000
-
pygame
.
time
.
get_ticks
())
//
1000
%
60
)
.
zfill
(
2
)
countdown_text
=
font
.
render
(
countdown_text
,
True
,
(
0
,
0
,
0
))
countdown_rect
=
countdown_text
.
get_rect
()
countdown_rect
.
topright
=
[
cfg
.
SCREENSIZE
[
0
]
-
30
,
5
]
screen
.
blit
(
countdown_text
,
countdown_rect
)
# --按键检测
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
pygame
.
quit
()
sys
.
exit
()
key_pressed
=
pygame
.
key
.
get_pressed
()
if
key_pressed
[
pygame
.
K_a
]
or
key_pressed
[
pygame
.
K_LEFT
]:
hero
.
move
(
cfg
.
SCREENSIZE
,
'left'
)
if
key_pressed
[
pygame
.
K_d
]
or
key_pressed
[
pygame
.
K_RIGHT
]:
hero
.
move
(
cfg
.
SCREENSIZE
,
'right'
)
# --随机生成食物
generate_food_count
+=
1
if
generate_food_count
>
generate_food_freq
:
generate_food_freq
=
random
.
randint
(
10
,
20
)
generate_food_count
=
0
food
=
Food
(
game_images
,
random
.
choice
([
'gold'
,]
*
10
+
[
'apple'
]),
cfg
.
SCREENSIZE
)
food_sprites_group
.
add
(
food
)
# --更新食物
for
food
in
food_sprites_group
:
if
food
.
update
():
food_sprites_group
.
remove
(
food
)
# --碰撞检测
for
food
in
food_sprites_group
:
if
pygame
.
sprite
.
collide_mask
(
food
,
hero
):
game_sounds
[
'get'
]
.
play
()
food_sprites_group
.
remove
(
food
)
score
+=
food
.
score
if
score
>
highest_score
:
highest_score
=
score
# --画hero
hero
.
draw
(
screen
)
# --画食物
food_sprites_group
.
draw
(
screen
)
# --显示得分
score_text
=
f
'Score: {score}, Highest: {highest_score}'
score_text
=
font
.
render
(
score_text
,
True
,
(
0
,
0
,
0
))
score_rect
=
score_text
.
get_rect
()
score_rect
.
topleft
=
[
5
,
5
]
screen
.
blit
(
score_text
,
score_rect
)
# --判断游戏是否结束
if
pygame
.
time
.
get_ticks
()
>=
90000
:
break
# --更新屏幕
pygame
.
display
.
flip
()
clock
.
tick
(
cfg
.
FPS
)
# 游戏结束, 记录最高分并显示游戏结束画面
fp
=
open
(
cfg
.
HIGHEST_SCORE_RECORD_FILEPATH
,
'w'
)
fp
.
write
(
str
(
highest_score
))
fp
.
close
()
return
showEndGameInterface
(
screen
,
cfg
,
score
,
highest_score
)
'''run'''
if
__name__
==
'__main__'
:
while
main
():
pass
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment