猫史档案馆


【Python作品分享】50-乒乒乓乓【作业帖】

用户:阿短的死敌阿短的死敌查看:1 回复:4 评论:1 创建时间:2019-12-25T22:42:40


【作品展示】

center_image

 

【作品介绍】

grre3ewf

 

【作品源代码】

import pygame
import random
from pygame.locals import*

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

    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] >= self.size and pos[1] <= sc_size - self.size:
            if pos[0] <= self.size or pos[0] >= sc_size - self.size:
                speed_x = -speed_x
        elif pos[0] >= self.size and pos[0] <= sc_size-self.size:
            if pos[1] <=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
        pos[1] += speed_y
        return speed_x, speed_y, pos[0], pos[1],scores


pygame.init()
sc_size = 300
sc = pygame.display.set_mode([sc_size, sc_size])
pos_x = 150
pos_y = 280
list_speed_x = [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]
speed_x = random.choice(list_speed_x)
list_speed_y = [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]
speed_y = random.choice(list_speed_y)
hight = 5
width = 80
rect_x = 150-15
rect_y = 300-hight

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 == pygame.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.display.update()
    pygame.time.delay(15)
    speed_x, speed_y, pos_x, pos_y ,score= ball.run(
        [pos_x, pos_y], sc_size, speed_x, speed_y,score)

    print(pos_x)

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
比比芭比波哩波比比芭比波哩波

看起来好腻害的样子

点赞0


评论


TFJacksonTFJackson

看看第23行,语法错误

点赞0


评论


TFJacksonTFJackson

-语法表达式没有参函表达

点赞1


评论


云裳ovo云裳ovo

挖坟

点赞0


评论