Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN-Ada助手
Python_280508
提交
b2892fab
P
Python_280508
项目概览
CSDN-Ada助手
/
Python_280508
与 Fork 源项目一致
Fork自
inscode / Python
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python_280508
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b2892fab
编写于
5月 06, 2023
作者:
6
6281efeadc86617fa50519b7
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Sat May 6 10:10:00 UTC 2023 inscode
上级
becd727a
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
90 addition
and
1 deletion
+90
-1
bullet.png
bullet.png
+0
-0
main.py
main.py
+89
-1
requirements.txt
requirements.txt
+1
-0
tank.png
tank.png
+0
-0
未找到文件。
bullet.png
0 → 100644
浏览文件 @
b2892fab
110.0 KB
main.py
浏览文件 @
b2892fab
print
(
'欢迎来到 InsCode'
)
\ No newline at end of file
import
pygame
# 初始化Pygame
pygame
.
init
()
# 设置游戏窗口尺寸
window_size
=
(
800
,
600
)
screen
=
pygame
.
display
.
set_mode
(
window_size
)
# 设置游戏标题
pygame
.
display
.
set_caption
(
'Tank Battle'
)
# 设置游戏帧率
clock
=
pygame
.
time
.
Clock
()
fps
=
60
# 加载游戏资源
tank_image
=
pygame
.
image
.
load
(
'tank.png'
)
bullet_image
=
pygame
.
image
.
load
(
'bullet.png'
)
# 定义坦克类
class
Tank
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
x
,
y
):
pygame
.
sprite
.
Sprite
.
__init__
(
self
)
self
.
image
=
tank_image
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
x
self
.
rect
.
y
=
y
self
.
speed
=
5
def
update
(
self
,
keys
):
if
keys
[
pygame
.
K_LEFT
]:
self
.
rect
.
x
-=
self
.
speed
elif
keys
[
pygame
.
K_RIGHT
]:
self
.
rect
.
x
+=
self
.
speed
elif
keys
[
pygame
.
K_UP
]:
self
.
rect
.
y
-=
self
.
speed
elif
keys
[
pygame
.
K_DOWN
]:
self
.
rect
.
y
+=
self
.
speed
# 定义子弹类
class
Bullet
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
x
,
y
):
pygame
.
sprite
.
Sprite
.
__init__
(
self
)
self
.
image
=
bullet_image
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
x
=
x
self
.
rect
.
y
=
y
self
.
speed
=
10
def
update
(
self
):
self
.
rect
.
y
-=
self
.
speed
# 创建游戏对象
player_tank
=
Tank
(
400
,
300
)
all_sprites
=
pygame
.
sprite
.
Group
()
all_sprites
.
add
(
player_tank
)
bullets
=
pygame
.
sprite
.
Group
()
# 游戏循环
while
True
:
# 处理事件
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
pygame
.
quit
()
quit
()
elif
event
.
type
==
pygame
.
KEYDOWN
:
if
event
.
key
==
pygame
.
K_SPACE
:
bullet
=
Bullet
(
player_tank
.
rect
.
x
+
32
,
player_tank
.
rect
.
y
-
16
)
bullets
.
add
(
bullet
)
all_sprites
.
add
(
bullet
)
# 更新游戏对象
keys
=
pygame
.
key
.
get_pressed
()
player_tank
.
update
(
keys
)
bullets
.
update
()
# 绘制游戏界面
screen
.
fill
((
255
,
255
,
255
))
all_sprites
.
draw
(
screen
)
# 更新屏幕显示
pygame
.
display
.
update
()
# 限制帧数
clock
.
tick
(
fps
)
requirements.txt
浏览文件 @
b2892fab
pygame
\ No newline at end of file
tank.png
0 → 100644
浏览文件 @
b2892fab
41.9 KB
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录