(包含重力矢量)Pygame粒子模拟

半成品,目前速度不能修改,另外某些状况下路径会比较奇怪,因为没有速度计算,包含了重力矢量,可以修改重力方向

(包含重力矢量)Pygame粒子模拟_第1张图片

import pygame as pg
import math
import time
import random
import math

class Particle(): #Tile is for generating maze
    def __init__(self,x,y):
        self.x,self.y = x,y

        
    def draw(self,color = (100,100,100)): #x,y represents the tile coordinates  
        pg.draw.rect(screen,color,(self.x,self.y,2,2))

def rotation_matrix(angle,vector):
    x,y = vector
    angle = angle/180*math.pi
    x_ = math.cos(angle)*x - math.sin(angle)*y
    y_ = math.sin(angle)*x + math.cos(angle)*y
    return x_,y_


def get_clicked():
    x,y = pg.mouse.get_pos() #pixel coordinates

    return x,y

def add_particle(x,y):
    particle = Particle(x,y)
    print(x,y)
    matrix[y][x] = particle
    particles.append(particle)

def frame_calculation():
    for particle in particles:

        particle.draw()
        x,y = particle.x,particle.y

        for dx,dy in move_direction:
            x_,y_ = int(particle.x+dx), int(particle.y+dy)
            if x_ not in range(grid_size[0]) or y_ not in range(grid_size[1]):
                break
            if matrix[y_][x_] == None:
                particle.x,particle.y = particle.x+dx, particle.y+dy
                matrix[y_][x_] = True
                matrix[int(y)][int(x)] = None
                break



#================================initialize parameter===================================

screen_size = [800,800]
grid_size = [800,800]
move_direction = [  rotation_matrix(45,[1,1]),  #gravitational vector
                    rotation_matrix(90,[1,1]),
                    rotation_matrix(0,[1,1])]
print(move_direction)

particles = []

matrix = []
for y in range(grid_size[1]+1):
    temp = []
    for x in range(grid_size[0]+1):
        temp.append(None)
    matrix.append(temp)

screen = pg.display.set_mode(screen_size)
pg.init()


#================================controls===============================================
mouse_l_clicked = False


#================================game loop==============================================
run = True
while run:
    screen.fill((255,255,255))
    frame_calculation()
    pg.display.update()
    if mouse_l_clicked:
        x,y = get_clicked()
        add_particle(x,y)
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False
            pg.quit()
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_g:
                pass
        if event.type == pg.MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_l_clicked = True
            else:
                x,y = get_clicked()
                dx,dy = x-grid_size[0]/2,(y-grid_size[1]/2)
                print(dx,dy)
                norm = (dx**2+dy**2)**0.5
                vec = [dx/norm,dy/norm]
                move_direction = [  rotation_matrix(0,vec),  #gravitational vector
                                    rotation_matrix(45,vec),
                                    rotation_matrix(-45,vec)]
                
        elif event.type == pg.MOUSEBUTTONUP:
            if event.button == 1:
                mouse_l_clicked = False

                

你可能感兴趣的:(Pygame,像素游戏,pygame,python,游戏引擎,游戏程序)