Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- import random
- from pygame.locals import *
- #setup
- background_colour = (0,0,0)
- (width, height) = (500, 500)
- screen = pygame.display.set_mode((width, height))#,pygame.FULLSCREEN)
- screen.fill(background_colour)
- pygame.display.set_caption('Wave equation')
- pygame.font.init()
- myfont = pygame.font.SysFont("monospace", 20)
- clock = pygame.time.Clock()
- class particle:
- def __init__(self, i, j):
- self.x = i
- self.y = j
- self.value = 0
- self.velocity = 0
- self.neighbours = []
- def get_neighbours(self):
- if self.x >= 1 and self.x <= size_x - 2 and self.y >= 1 and self.y <= size_y - 2:
- self.neighbours.append(values[self.x - 1][self.y])
- self.neighbours.append(values[self.x + 1][self.y])
- self.neighbours.append(values[self.x][self.y - 1])
- self.neighbours.append(values[self.x][self.y + 1])
- def update(self):
- if self.x >= 1 and self.x <= size_x - 2 and self.y >= 1 and self.y <= size_y - 2:
- lap = 0
- for n in self.neighbours:
- lap += n.value
- lap -= 4*self.value
- self.velocity += 0.01*lap
- self.velocity *= 0.97
- self.value += self.velocity
- else:
- self.value = 0
- size_x = 50
- size_y = 50
- values = []
- for i in range(size_x):
- values.append([])
- for j in range(size_y):
- values[i].append(particle(i,j))
- for i in range(size_x - 1):
- for j in range(size_y - 1):
- values[i][j].get_neighbours()
- #values[int(size_x/2)][int(size_y/2)].value = 20
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == KEYDOWN:
- if event.key == K_ESCAPE:
- running = False
- screen.fill(background_colour)
- pos = pygame.mouse.get_pos()
- grid_pos = [int(pos[0]/10), int(pos[1]/10)]
- values[grid_pos[0]][grid_pos[1]].value = 20
- for i in range(size_x):
- for j in range(size_x):
- values[i][j].update()
- pygame.draw.circle(screen,
- [30,min(255,max(0,100 - 10*values[i][j].value)),min(255,max(0,100 + 10*values[i][j].value))],
- [10*i,10*j], 7)
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement