• 大小: 13.94MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-16
  • 语言: Python
  • 标签: python  

资源简介

import pygame import sys from pygame.locals import * from random import * #球类继承自Sprite类 class Ball(pygame.sprite.Sprite):

资源截图

代码片段和文件信息

import pygame
import sys
import math
from pygame.locals import *
from random import *


class Ball(pygame.sprite.Sprite):
    def __init__(self image position speed bg_size):
        #初始化动画精灵
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load(image).convert_alpha()
        self.rect = self.image.get_rect()
        #将小球 放在指定位置
        self.rect.left self.rect.top = position
        self.speed = speed
        self.width self.height = bg_size[0] bg_size[1]

    def move(self):
        self.rect = self.rect.move(self.speed)
        
        if self.rect.right < 0:
            self.rect.left = self.width

        elif self.rect.left > self.width:
            self.rect.right = 0

        elif self.rect.top > self.height:
            self.rect.bottom = 0

        elif self.rect.bottom < 0:
            self.rect.top = self.height

def collide_check(item target):
    #碰撞检测
    col_balls = []
    for each in target:
        distance = math.sqrt(\
            math.pow((item.rect.center[0]-each.rect.center[0]) 2) + \
            math.pow((item.rect.center[1]-each.rect.center[1]) 2))
        if distance <= (item.rect.width + each.rect.width) / 2:
            col_balls.append(each)

    return col_balls


def main():
    pygame.init()

    ball_image = “gray_ball.png“
    bg_image = “background.png“

    running = True

    bg_size = width height = 1024 661
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption(“Play the ball“)

    background = pygame.image.load(bg_image).convert_alpha()

    #用来存放小球对象的列表
    balls = []

    #创建五个球
    BALL_NUM = 5
    for i in range(BALL_NUM):
        #位置、速度随机
        position = randint(0 width-128) randint(0 height-128)
        speed = [randint(-10 10) randint(-10 10)]
        ball = Ball(ball_image position speed bg_size)
        while collide_check(ball balls):
            ball.rect.width ball.rect.height = randint(0 width-128) randint(0 height-128)
        balls.append(ball)

    clock = pygame.time.Clock()
    
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

        screen.blit(background (0 0))

        for each in balls:
            each.move()
            screen.blit(each.image each.rect)

        for i in range(BALL_NUM):
            item = balls.pop(i)
            if collide_check(item balls):
                item.speed[0] = -item.speed[0]
                item.speed[1] = -item.speed[1]

            balls.insert(i item)

        pygame.display.flip()
        clock.tick(30)
        

if __name__ == “__main__“:
    main()
        

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件      598617  2018-05-26 15:41  PlayTheBall\background.png
     文件     8507261  2018-02-21 19:32  PlayTheBall\bg_music.mp3
     文件        2854  2018-05-26 20:45  PlayTheBall\collide_check.py
     文件       58564  2018-05-24 11:05  PlayTheBall\glass.png
     文件        6397  2018-05-26 15:34  PlayTheBall\gray_ball.png
     文件        1128  2018-05-28 16:32  PlayTheBall\hand.png
     文件      210044  2018-05-26 21:25  PlayTheBall\hole.wav
     文件      360044  2018-05-26 21:23  PlayTheBall\laugh.wav
     文件      320044  2018-05-26 21:29  PlayTheBall\loser.wav
     文件        8994  2018-05-30 15:36  PlayTheBall\mian.py
     文件       31076  2018-05-28 17:29  PlayTheBall\red_ball.png
     文件       34021  2018-05-30 10:47  PlayTheBall\win.png
     文件      170044  2018-05-26 21:21  PlayTheBall\winner.wav
     目录           0  2018-05-26 20:40  PlayTheBall\播放声音和音效示例\
     文件     4625957  2018-03-25 17:03  PlayTheBall\播放声音和音效示例\bg_music.mp3
     文件       30044  2018-05-26 20:32  PlayTheBall\播放声音和音效示例\cat.wav
     文件      147500  2018-05-26 20:32  PlayTheBall\播放声音和音效示例\dog.wav
     文件        1606  2018-05-26 20:42  PlayTheBall\播放声音和音效示例\music.py
     文件        5236  2018-05-26 20:06  PlayTheBall\播放声音和音效示例\pause.png
     文件      208736  2018-05-26 20:32  PlayTheBall\播放声音和音效示例\tiger.wav
     文件        5367  2018-05-26 20:06  PlayTheBall\播放声音和音效示例\unpause.png

评论

共有 条评论