This repository was archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.py
More file actions
123 lines (108 loc) · 3.71 KB
/
Graphics.py
File metadata and controls
123 lines (108 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import numpy as np
import time
import pymunk
import pygame
import json
class Wind():
def __init__(self,base_force=300,force_variance=200,constancy=1,changeability=0.01, space= None):
self.base_force=base_force
self.force_variance=force_variance
self.constancy=constancy
self.wind=0
self.changeability=changeability
def blow(self):
if(np.random.random()<self.changeability):
self.wind=np.sign(np.random.random()*2-1)*(self.base_force+np.random.randint(0,self.force_variance))
class Box():
'''
This class rappresent physical object, with a predetermined inizial position,size, density and color
Methods:
--------
MoveX(offset):
Moves the box of a given offset on the X axis.
draw():
Draws the box on the pygame window
'''
def __init__ (self, x ,y,width,height,density = 1, static=False, color =(0,0,0), space= None):
'''
Create an istance of box.
Parameters:
----------
x,y: float
initial position.
width: int
the width of the object. It must be positive.
height: int
the height of the object. It must be positive
density (optional): int
the density of the object. The default value is 1.
static (optional): boolean
decide if the body is code-driven(True) or physics-driven(False). The default value is False.
color (optional): tuple of size 3
the color of the object, rappresented in RGB notation. The default value is (0,0,0).
Returns:
Box
a box instance with the given values.
'''
#setup the attributes with the parameters
if static:
self.body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
else:
self.body = pymunk.Body(5, 500)
self.body.position = x, y
self.width = width
self.height = height
self.shape = pymunk.Poly.create_box(self.body, (width, height))
self.shape.density = density
self.space = space
self.space.add(self.body, self.shape)
self.color = color
def moveX(self,offset):
'''
Moves the box of a given offset on the X axis.
Parameters:
----------
offset: int
the given offset.
'''
self.body.velocity = (offset, 0)
def draw(self, display,xcamera):
'''
Draws the box on the pygame window
'''
x, y = self.body.position
pygame.draw.rect(display, self.color,(int(x-self.width/2) - xcamera, int(y- self.height/2) , self.width, self.height))
class String():
'''
This class rappresent a black physical string that connect two bodies.
Methods:
--------
draw():
Draws the box on the pygame window
'''
def __init__(self, body1, attachment, space= None):
'''
Create an instance of String that connect two bodies.
Parameters:
----------
body1: body
the first body to connect.
attachment: body
the second body to connect.
Returns:
--------
String
the string that connect the two bodies.
'''
self.body1 = body1
self.body2 = attachment
self.shape= pymunk.PinJoint(self.body1, self.body2)
self.space = space
self.space.add(self.shape)
def draw(self, display, xcamera):
'''
Draws the box on the pygame window
'''
x1, y1 = self.body1.position
x2, y2 = self.body2.position
pygame.draw.line(display,(0,0,0), (int(x1)-xcamera, int(y1)), (int(x2)-xcamera, int(y2)), 2)