猫史档案馆


【Python作品分享】ppq-副本【作品秀】

用户:认真的月见草bkmP认真的月见草bkmP查看:0 回复:0 评论:0 创建时间:2020-04-18T10:25:54


【作品展示】

center_image

 

【作品介绍】

bs

 

【作品源代码】

import pygame
import random
from pygame.locals import *
pygame.init()  # 初始化
sc_size = 300  # 设置窗体的宽、高
sc = pygame.display.set_mode((sc_size, sc_size))  # 设置窗体大小


class Ball:
    def __init__(self, surface, color, size):
        self.surface = surface
        self.color = color
        self.size = size

    def create_ball(self, pos):
        pygame.draw.circle(self.surface, self.color, pos, self.size)

    def run(self, pos, sc_size, speed_x, speed_y, scores):
        if pos[1] >= 0 + self.size and pos[1] <= sc_size - self.size:
            if pos[0] <= 0 + self.size or pos[0] >= sc_size - self.size:
                speed_x = -speed_x
        elif pos[0] >= 0 + self.size and pos[0] <= sc_size - self.size:
            if pos[1] <= 0 + self.size:
                speed_y = -speed_y
            elif pos[1] >= sc_size - self.size-5:
                if rect_x < pos[0] <= rect_x+80:
                    scores += 1
                    speed_y = -speed_y
                else:
                    pygame.quit()
        pos[0] += speed_x  # 小球的x坐标加运行速度
        pos[1] += speed_y  # 小球的y坐标加运行速度
        return speed_x, speed_y, pos[0], pos[1], scores


# 小球参数
pos_x = 100  # 设置小球x坐标
pos_y = 150  # 设置小球y坐标
list_speed_x = [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]  # 将速度放到列表中,然后随机获取
list_speed_y = [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]  # 将速度放到列表中,然后随机获取
speed_x = random.choice(list_speed_x)  # 小球x轴初始速度
speed_y = random.choice(list_speed_y)  # 小球y轴初始速度

# 方块参数
rect_x = 150-15  # 方块的x轴坐标
rect_y = 300-5  # 方块的y轴坐标
hight = 5  # 球拍高5
width = 80  # 球拍宽80
# 创建对象
ball = Ball(sc, (255, 0, 0), 20)
# 字体设置
ziti = pygame.font.SysFont('Arial', 24)
# 分数初始化
score = 0
while True:
    for event in pygame.event.get():  # 获取事件列表 
        if event.type == QUIT:  # 判断事件是否成立
            pygame.display.quit()  # 退出
            exit()
        elif event.type == MOUSEMOTION:
            rect_x, _ = pygame.mouse.get_pos()

    sc.fill((255, 255, 255))  # 设置背景颜色
    # 创建小球设置位置
    ball.create_ball([pos_x, pos_y])
    # 创建方块
    pygame.draw.rect(sc, (0, 0, 255), (rect_x, rect_y, width, hight))
    text = ziti.render(str(score), True, (255, 0, 0))
    sc.blit(text,(10, 10))

    pygame.time.delay(35)
    pygame.display.update()  # 更新窗口
    speed_x, speed_y, pos_x, pos_y, score = ball.run(
        [pos_x, pos_y], sc_size, speed_x, speed_y, score)

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页