用Python做一个跑酷游戏
本帖最后由 Ewan-Ahiouy 于 2023-7-11 07:03 编辑用Python做一个跑酷游戏
制作背景
前几天逛论坛,逛到一个精华帖,叫 【鱼币】用 Scratch 做一个跑酷 {:10_256:}
看了里面的题外话{:10_277:}
https://fishc.com.cn/thread-228545-1-1.html
上次,我不是说
Python,C++ 都很难做出来对吧?
这就是Scratch的优势!
结果呢???
https://fishc.com.cn/thread-228775-1-1.html
https://fishc.com.cn/thread-228778-1-1.html
https://fishc.com.cn/thread-229450-1-1.html
就连@Twilight6 斑竹也说:
很棒,论坛就需要这种学习良性竞争的氛围!!!
(https://fishc.com.cn/forum.php?mod=redirect&goto=findpost&ptid=228775&pid=6243751)
555,脸丢尽了
于是,我又写了一个作品,@zhangjinxuan 来啊!
嘿嘿{:10_334:} 你 sfqxx 可真的是太嚣张了,强大的Python做不出来?开什么玩笑{:10_323:}
于是,打开 VS Code ,一顿操作猛如虎......
大概两天就做出来了{:10_307:}
代码不到200行,代码自我感觉良好{:10_279:}
引用一下 zhangjinxuan 的名言:
sfqxx,不服来战!
{:10_279:}
游戏截图
https://s1.ax1x.com/2023/07/04/pCsbirT.png
https://s1.ax1x.com/2023/07/04/pCsb9x0.png
https://s1.ax1x.com/2023/07/04/pCsbPMV.png
https://s1.ax1x.com/2023/07/04/pCsbp2q.png
https://s1.ax1x.com/2023/07/04/pCsbFqU.png
附加文件
这是打包的文件和依赖文件 ->
Code
import pygame, sys
pygame.init()
width, height = 800, 600
window = pygame.display.set_mode((width, height))
windowrect = pygame.Rect(0, 0, width, height)
pygame.display.set_caption("跑酷")
shibai = 0
guan = 1
zhujue_old = pygame.image.load("imges/zhujue.png")
zhujue1 = pygame.transform.rotozoom(zhujue_old, 0, 0.6)
zhujue2 = pygame.transform.flip(zhujue1, True, False)
zhujue = zhujue1
zhujuew, zhujueh = zhujue.get_size()
x, y = 10, 470
xsu, ysu = 0, 0
boolwalk = False
walk = None
up = False
peng = False
zuidaguan = 10
yanjiang = [
[],
[],
[, ],
[],
[],
[],
[, ],
[],
[],
[]
]
zhangai = [
[, ],
[],
[],
[, , , ],
[, , , ],
[, , , ],
[, , , , ],
[, , ],
[],
[]
]
zilist = [
"欢迎来到我的跑酷游戏~试试按住上键吧~", "小心岩浆哦~", "难度+1", "经典正F", "经典反F", "天梯~", "小心哦~", "难度很高~", "信仰之跃! 加油! ", "恭喜你, 成功通关~"
]
font = pygame.font.Font("font/1.ttf", 30)
def ifin(x, y, w, h, mx, my):
if x <= mx <= x + w and y <= my <= y + h:
return True
return False
while True:
pygame.time.Clock().tick(60)
text = font.render("失败次数: " + str(shibai), True, (0, 0, 0))
text2 = font.render("第 {} 关".format(guan), True, (0, 0, 0))
text3 = font.render(zilist, True, (0, 0, 0))
textrect = text3.get_rect()
textrect.centerx = windowrect.centerx
if guan == 8:
textrect.y = 100
else:
textrect.y = 70
window.fill((255, 255, 255))
if y > height:
shibai += 1
x, y = 10, 470
ysu = 0
xsu = 0
if boolwalk:
if walk == pygame.K_d or walk == pygame.K_RIGHT:
if xsu <= 8:
xsu += 2
zhujue = zhujue1
if walk == pygame.K_a or walk == pygame.K_LEFT:
if xsu >= -8:
xsu -= 2
zhujue = zhujue2
xsu *= 0.9
x += xsu
ysu -= 1
y -= ysu
peng = False
for i in zhangai:
if i:
wx, wy, ww, wh = i
if ifin(wx, wy, ww, wh, x + 1, y + zhujueh) or ifin(wx, wy, ww, wh, x + zhujuew - 1, y + zhujueh) or ifin(wx, wy, ww, wh, x + int(zhujuew / 2), y + zhujueh) or \
ifin(wx, wy, ww, wh, x + 1, y) or ifin(wx, wy, ww, wh, x + zhujuew - 1, y) or ifin(wx, wy, ww, wh, x + int(zhujuew / 2), y):
peng = True
if peng:
y += ysu
ysu = 0
if up:
ysu = 20
for i in zhangai:
if i:
mx, my, mw, mh = i
pygame.draw.rect(window, (0, 0, 0), (mx, my, mw, mh))
if ifin(mx, my, mw, mh, x + zhujuew, y) or \
ifin(mx, my, mw, mh, x, y) or \
ifin(mx, my, mw, mh, x, y + zhujueh) or \
ifin(mx, my, mw, mh, x + zhujuew, y + zhujueh) or \
ifin(mx, my, mw, mh, x, y + int(zhujueh / 2)) or \
ifin(mx, my, mw, mh, x + zhujuew, y + int(zhujueh / 2)):
x -= xsu
if up:
if xsu > 0:
xsu = -10
else:
xsu = 10
ysu = 15
else:
ysu = 0
for i in yanjiang:
if i:
mx, my, mw, mh = i
pygame.draw.rect(window, (255, 0, 0), (mx, my, mw, mh))
if ifin(mx, my, mw, mh, x + zhujuew, y) or ifin(mx, my, mw, mh, x, y) or ifin(mx, my, mw, mh, x, y + zhujueh) or ifin(mx, my, mw, mh, x + zhujuew, y + zhujueh):
shibai += 1
x, y = 10, 470
ysu = 0
xsu = 0
if ifin(width - 5, 0, 50, height, x + zhujuew, y) and guan < zuidaguan:
x, y = 10, 470
guan += 1
window.blit(zhujue, (x, y))
window.blit(text, (10, 10))
window.blit(text2, (10, 50))
window.blit(text3, textrect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key in :
walk = event.key
boolwalk = True
if event.key == pygame.K_w or event.key == pygame.K_UP:
up = True
if event.type == pygame.KEYUP:
if event.key in :
boolwalk = False
if event.key == pygame.K_w or event.key == pygame.K_UP:
up = False
pygame.display.update()
育碧换评分,好吗{:10_266:}
求评分{:10_254:}
求求了~~~~~
题外话
唉,你们这些人啊,说玩,附件都不下载,咋玩{:10_250:}
我做这个游戏,不就是想让你们玩的开心吗?{:10_269:}
结果到头来,一个玩的都没有,唉,有什么意思?{:10_243:}
哇巧! {:9_232:} 好厉害啊{:10_257:}{:10_275:} 评负分{:10_334:} wtm,我的又一个项目被抢了 不过,我相信。这上不了精华 本帖最后由 Ewan-Ahiouy 于 2023-7-8 19:43 编辑
sfqxx 发表于 2023-7-4 23:18
不过,我相信。这上不了精华
谁说我发帖子都要精华?我只是做出来了分享一下而已~{:5_109:}
而且我想升级{:10_266:} 歌者文明清理员 发表于 2023-7-4 22:28
wtm,我的又一个项目被抢了
az......不是吧{:10_277:} sfqxx 发表于 2023-7-4 21:31
评负分
求评分,正的{:10_254:} zhangjinxuan 发表于 2023-7-4 21:08
哇巧!
求评分,快升级{:10_254:} {:10_326:} wangxiangtan2 发表于 2023-7-5 08:15
{:10_339:} {:10_279:}{:10_279:}{:10_279:} 厉害了!! {:5_106:} {:5_106:} 琅琊王朝 发表于 2023-7-5 13:50
评个分吧{:10_254:} {:10_249:}{:10_266:}{:10_279:}{:10_260:}{:10_245:}{:10_254:}好耶好耶 冲冲冲,开玩