单摆模拟pygame版.py

"""
   单摆模拟pygame版.py
"""
__author__ = "李兴球"
__date__ = "2019/8/22"

import math
import pygame
from pygame.locals import *

def main():
    """主要函数"""
    width,height = 480,360
    screen = pygame.display.set_mode((width,height))
    pygame.display.set_caption("单摆模拟pygame版,作者:李兴球")
    ball = Pendulum_ball((width//2,100),200,10)

    clock = pygame.time.Clock()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:running=False
        ball.update()
        screen.fill((0,0,0))
        ball.draw(screen)
        pygame.display.update()
        clock.tick(60)
    pygame.quit()

class Pendulum_ball:
    def __init__(self,rotate_center,rotate_radius,ball_radius):
        """参数依次为旋转中心,旋转半径,球的半径"""
        self.rc = rotate_center
        self.ra = rotate_radius
        self.r = ball_radius
        self.image = pygame.Surface((self.r*2,self.r*2))
        self.image.set_colorkey((0,0,0))
        p = self.r,self.r
        pygame.draw.circle(self.image,(255,0,0),p,self.r)
        p = self.rc[0] + self.ra , self.rc[1]
        self.rect = self.image.get_rect(center=p)
        self.angle = 0      # 和x轴的角度
        self.da = 0         # 每次增加的角度,角速度
        # 线条属性
        self.line = pygame.Surface((self.ra,2))

You must log in to read the rest of this article. Please log in or register as a user.