猫史档案馆


【Python作品分享】方块快跑(未完)【作品秀】

用户:九月小猫九月小猫查看:0 回复:0 评论:0 创建时间:2023-03-26T14:22:05


【作品展示】

center_image

 

【作品介绍】

用pgzrun做的跑酷游戏

因py游戏制作不易,只做了一关试试

后续还会更像

 

【作品源代码】

import pgzrun
# 方块快跑游戏
WIDTH = 800
HEIGHT = 600

#设置背景
bg = Actor('bg')

#创建方块对象
player = Rect(100, 100, 30, 30)
goal = Rect(600, 500, 30, 30)

#创建矩形障碍物对象
block1 = Rect(200, 0, 50, 400)
block2 = Rect(350, 200, 50, 400)
block3 = Rect(500, 0, 50, 400)

#方块及障碍物颜色
player_color = (255, 0, 0)
goal_color = (0, 0, 255)
block1_color = (180, 245, 100)
block2_color = (200, 255, 100)
block3_color = (50, 200, 50)

#碰撞检测变量
test = 0


def draw():
    bg.draw()
    # 画障碍物
    screen.draw.filled_rect(block1, block1_color)
    screen.draw.filled_rect(block2, block2_color)
    screen.draw.filled_rect(block3, block3_color)
    #画红方块
    screen.draw.filled_rect(player, player_color)
    if test == 1:
        screen.draw.filled_rect(player, block1_color)
    if test == 2:
        screen.draw.filled_rect(player, block2_color)
    if test == 3:
        screen.draw.filled_rect(player, block3_color)
    screen.draw.rect(player, (0, 0, 0))
    if test == 4:
       screen.draw.filled_rect(player, player_color)
    #画蓝方块
    screen.draw.filled_rect(goal, goal_color)
    screen.draw.rect(goal, (0, 0, 0))  

def update():
    global test
    #控制红方块上下左右移动
    if player.y <= 570:
        player.y += 3
    if keyboard.a:
        player.x -= 5
    if keyboard.d:
        player.x += 5
    if keyboard.w:
        player.y -= 5
    if keyboard.s:
        player.y += 5
    #碰撞检测:红方块撞到障碍物,输出“撞到墙了”
    if  player.colliderect(block1):
        player.x = 100
        player.y = 100
        print('撞到墙了!')
        test = 1
    if  player.colliderect(block2):
        player.x = 100
        player.y = 100
        print('撞到墙了!')
        test = 2
    if player.colliderect(block3):
        player.x = 100
        player.y = 100
        print('撞到墙了!')
        test = 3
    if player.x <= 0 or player.x >= 800 or player.y <= 0 or player.y >= 600:
        player.x = 100
        player.y = 100
        print('不要作弊哦~')
        test = 4
    #碰撞检测:红方块撞到蓝方块,蓝方块消失
    #输出“成功了”
    if player.colliderect(goal):
        player.x = 1000
        print("成功啦!请关闭窗口结束游戏~")


pgzrun.go()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页