zoukankan      html  css  js  c++  java
  • python-pygame的触碰方法

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*- 
     3 # Author: ss
     4 
     5 import pygame
     6 import sys
     7 import random
     8 import math
     9 
    10 
    11 class Ball(pygame.sprite.Sprite):
    12     def __init__(self, image, position, speed, bg_size):
    13         pygame.sprite.Sprite.__init__(self)
    14         self.image = pygame.image.load(image).convert_alpha()
    15         self.rect = self.image.get_rect()
    16         self.rect.left, self.rect.right = position
    17         self.speed = speed
    18         self.width, self.height = bg_size[0], bg_size[1]
    19 
    20     def move(self):
    21         self.rect = self.rect.move(self.speed)
    22 
    23         if self.rect.right < 0:
    24             self.rect.left = self.width
    25         if self.rect.left > self.
    26             self.rect.right = 0
    27         if self.rect.bottom < 0:
    28             self.rect.top = self.height
    29         if self.rect.top > self.height:
    30             self.rect.bottom = 0
    31 
    32 def collide_check(item,target):
    33     col_balls = []
    34     for each in target:
    35         distance = math.sqrt(math.pow((item.rect.center[0] - each.rect.center[0]), 2) + 
    36                              math.pow((item.rect.center[1] - each.rect.center[1]), 2))
    37         if distance <= (item.rect.width + each.rect.width) / 2:
    38             col_balls.append(each)
    39     return col_balls
    40 
    41 
    42 def main():
    43     pygame.init()
    44 
    45     ball_image = 'gray_ball.png'
    46     bg_image = 'background.png'
    47 
    48     bg_size = width, height = 1024, 681
    49     screen = pygame.display.set_mode(bg_size)
    50     pygame.display.set_caption('我的滚动的球 -s.s')
    51     background = pygame.image.load(bg_image)#.convert_alpha
    52 
    53     balls = []
    54 
    55 
    56     for i in range(5):
    57         position = random.randint(0, width - 100), random.randint(0, height - 100)
    58         speed = [random.randint(-10, 10), random.randint(-10, 10)]
    59         ball = Ball(ball_image, position, speed, bg_size)
    60         while collide_check(ball,balls):
    61             ball.rect.left, ball.rect.right = random.randint(0, width - 100), random.randint(0, height - 100)
    62         balls.append(ball)
    63 
    64 
    65     clock = pygame.time.Clock()
    66 
    67     running = True
    68 
    69     while running:
    70         for event in pygame.event.get():
    71             if event.type == pygame.QUIT:
    72                 sys.exit()
    73 
    74         screen.blit(background, (0, 0))
    75 
    76         for each in balls:
    77             each.move()
    78             screen.blit(each.image, each.rect)
    79 
    80         for i in range(5):
    81             item = balls.pop(i)
    82             if collide_check(item, balls):
    83                 item.speed[0] = -item.speed[0]
    84                 item.speed[1] = -item.speed[1]
    85             balls.insert(i, item)
    86 
    87         pygame.display.flip()
    88 
    89         clock.tick(50)
    90 
    91 
    92 if __name__ == "__main__":
    93     main()
  • 相关阅读:
    C# 实现 Snowflake算法生成唯一性Id
    kafka可视化客户端工具(Kafka Tool)的基本使用(转)
    docker 安装kafka
    Model类代码生成器
    使用docker 部署rabbitmq 镜像
    Vue 增删改查 demo
    git 提交代码到库
    Android ble蓝牙问题
    mac 配置 ssh 到git (Could not resolve hostname github.com, Failed to connect to github.com port 443 Operation timed out)
    okhttp
  • 原文地址:https://www.cnblogs.com/ssxsy/p/9215987.html
Copyright © 2011-2022 走看看