猫史档案馆


【Python作品分享】平板弹球【教程贴】

用户:可爱的暴风雀666可爱的暴风雀666查看:0 回复:0 评论:0 创建时间:2021-05-03T14:04:20


【作品展示】

center_image

 

【作品介绍】

按空格键开始

用w、a、s、d来操控平板,使皮球吃到金币

 

【作品源代码】

from pgzrun import *
from random import *

WIDTH = 450
HEIGTH = 650
score = 0
x_speed, y_speed = 7, -7
move_speed = 6
game_state = 'start'
count = 0

ball = Actor("ball", (300,300))
coin = [Actor("coin", (75, 150)), Actor("coin", (150, 150)),
        Actor("coin", (225, 150)), Actor("coin", (300, 150)),
        Actor("coin", (375, 150))]
panel = Actor("panel", (225, 500))

def draw():
    global game_state
    if game_state == "start":
        screen.fill("gold")
        screen.draw.text("Bounce Ball", (136, 260), fontsize=50)
        screen.draw.text("Press SPACE to start!", (132, 310), fontsize=30)
        if keyboard.space:
            game_state = "play"
    elif game_state == "play":
        screen.fill("gold")
        screen.draw.text("socre: " + str(score), (10, 10),
                         fontsize = 30)
        panel.draw()
        ball.draw()
        for i in range(len(coin)):
            coin[i].draw()
    elif game_state == "game_over":
        screen.fill("gold")
        panel.draw()
        ball.draw()
        for i in range(len(coin)):
            coin[i].draw()
        screen.draw.text("Game Over", (136, 260),
                         color="red", fontsize=50)
        screen.draw.text("Your Score: " + str(score), 
                         (140, 310), fontsize=40)


def update():
    global game_state, score, x_speed, y_speed, count
    if game_state == "play":
        ball.x += x_speed
        ball.y += y_speed

        if ball.x < 25 or ball.x > WIDTH - 25:
            x_speed = - x_speed
            ball.x += x_speed
        if ball.y < 25 or ball.y > HEIGTH - 70:
            y_speed = - y_speed
            ball.y += y_speed
        elif ball.y > HEIGTH - 80:
            game_state = "game_over"
        
        if keyboard.a or keyboard.left:
            panel.x -= move_speed
        if keyboard.d or keyboard.right:
            panel.x += move_speed
        if keyboard.w or keyboard.up:
            panel.y -= move_speed
        if keyboard.s or keyboard.down:
            panel.y += move_speed
        
        if panel.x < 60:
            panel.x = 60
        elif panel.x > 390:
            panel.x = 390
        if panel.y > 590:
            panel.y = 590
        elif panel.y < 5:
            panel.y = 5

        if count == 0:
            if panel.colliderect(ball):
                count = 1
                y_speed = - y_speed
                
        else:
            count += 1
            if count > 60:
                cound = 0

        if panel.colliderect(ball):
            y_speed = - y_speed

        for i in range(len(coin)):
            if coin[i].collidepoint(ball.pos):
                score += 1
                coin[i].x = randint(30, WIDTH - 30)
                coin[i].y = randint(30, HEIGTH / 2)

go()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页