猫史档案馆


【Python作品分享】新的作品-59-1【作业帖】

用户:务实的呆鲤鱼izou务实的呆鲤鱼izou查看:0 回复:1 评论:0 创建时间:2020-08-28T22:10:18


【作品展示】

center_image

 

【作品介绍】

作业

 

【作品源代码】

# 请尝试在之前的代码上修改,以使得小球每次碰到敞口边缘时,都会越来越快。
# 比如,假设小球每次碰到边缘后,增加的速度为1。
# 如果小球碰到了左右边界,则小球的x轴的速度加1;如果小球碰到了上下边界,则小球的y轴速度加1.
import pygame
from pygame.locals import *
import random

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,speedy_x,speedy_y):
        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:
                speedy_x = -speedy_x
                if speedy_x > 0:
                    speedy_x+=1
                else:
                    speedy_x-=1
        elif pos[0] >= 0+self.size and pos[0] <= sc_size - self.size:
            if pos[1] <= 0 + self.size:
                speedy_y = -speedy_y
            elif pos[1] >= sc_size - self.size-5:
                if rect_x <= pos[0] <= rect_x +80:
                    speedy_y = -speedy_y
                    if speedy_y > 0:
                        speedy_y+=1
                    else:
                        speedy_y-=1
                else:
                    pygame.quit()
        pos[0] += speedy_x
        pos[1] += speedy_y
        return speedy_x,speedy_y,pos[0],pos[1]


pygame.init()
sc_size = 300
sc = pygame.display.set_mode([sc_size, sc_size])

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)
# speed_y = random.choice(list_speed_y)

speed_x = 5
speed_y = 5

rect_x = 150-15
rect_y = 300-5
hight = 5
width = 80

pos_x = 150
pos_y =280
ball = Ball(sc,(255,0,0),20)
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))
    
    pygame.time.delay(35)
    pygame.display.update()
    speed_x,speed_y,pos_x,pos_y = ball.run([pos_x,pos_y],sc_size,speed_x,speed_y)
    

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
QAQ一只崽子QAQ一只崽子

arcade比pygame好用哦~

点赞0


评论