Skip to content

Commit 9b177e5

Browse files
authored
Add files via upload
1 parent 8b0bf43 commit 9b177e5

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed

Diff for: Fourier Square Wave.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import numpy as np
2+
3+
import matplotlib.pyplot as plt
4+
5+
from scipy.signal import square
6+
7+
from scipy.integrate import quad
8+
9+
from math import* #import all function from math
10+
11+
x=np.arange(-np.pi,np.pi,0.001) # x axis has been chosen from –π to +π, value of 1 smallest square along x axis is 0.001
12+
13+
y=square(x) #defining square wave function 𝑦 =−1, 𝑓𝑜𝑟 − 𝜋 ≤ 𝑥 ≤ 0
14+
#y= +1, 𝑓𝑜𝑟 0 ≤ 𝑥 ≤ 𝜋
15+
16+
#define fuction
17+
18+
fc=lambda x:square(x)*cos(i*x) #i :dummy index
19+
20+
fs=lambda x:square(x)*sin(i*x)
21+
22+
n=50 #max value of I, not taken infinity, better result with high value
23+
24+
An=[] # defining array
25+
26+
Bn=[]
27+
28+
sum=0
29+
30+
for i in range(n):
31+
32+
an=quad(fc,-np.pi,np.pi)[0]*(1.0/np.pi)
33+
34+
An.append(an)
35+
36+
for i in range(n):
37+
38+
bn=quad(fs,-np.pi,np.pi)[0]*(1.0/np.pi)
39+
40+
Bn.append(bn) #putting value in array Bn
41+
42+
for i in range(n):
43+
44+
if i==0.0:
45+
sum=sum+An[i]/2
46+
47+
else:
48+
sum=sum+(An[i]*np.cos(i*x)+Bn[i]*np.sin(i*x))
49+
50+
plt.plot(x,sum,'g')
51+
52+
plt.plot(x,y,'r--')
53+
54+
plt.title("fourier series for square wave")
55+
56+
plt.show()

Diff for: Simple 3D Animation.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
============
3+
3D animation
4+
============
5+
6+
A simple example of an animated plot... In 3D!
7+
"""
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
import mpl_toolkits.mplot3d.axes3d as p3
11+
import matplotlib.animation as animation
12+
13+
14+
def Gen_RandLine(length, dims=2):
15+
"""
16+
Create a line using a random walk algorithm
17+
18+
length is the number of points for the line.
19+
dims is the number of dimensions the line has.
20+
"""
21+
lineData = np.empty((dims, length))
22+
lineData[:, 0] = np.random.rand(dims)
23+
for index in range(1, length):
24+
# scaling the random numbers by 0.1 so
25+
# movement is small compared to position.
26+
# subtraction by 0.5 is to change the range to [-0.5, 0.5]
27+
# to allow a line to move backwards.
28+
step = ((np.random.rand(dims) - 0.5) * 0.1)
29+
lineData[:, index] = lineData[:, index - 1] + step
30+
31+
return lineData
32+
33+
34+
def update_lines(num, dataLines, lines):
35+
for line, data in zip(lines, dataLines):
36+
# NOTE: there is no .set_data() for 3 dim data...
37+
line.set_data(data[0:2, :num])
38+
line.set_3d_properties(data[2, :num])
39+
return lines
40+
41+
# Attaching 3D axis to the figure
42+
fig = plt.figure()
43+
ax = p3.Axes3D(fig)
44+
45+
# Fifty lines of random 3-D lines
46+
data = [Gen_RandLine(25, 3) for index in range(50)]
47+
48+
# Creating fifty line objects.
49+
# NOTE: Can't pass empty arrays into 3d version of plot()
50+
lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data]
51+
52+
# Setting the axes properties
53+
ax.set_xlim3d([0.0, 1.0])
54+
ax.set_xlabel('X')
55+
56+
ax.set_ylim3d([0.0, 1.0])
57+
ax.set_ylabel('Y')
58+
59+
ax.set_zlim3d([0.0, 1.0])
60+
ax.set_zlabel('Z')
61+
62+
ax.set_title('3D Test')
63+
64+
# Creating the Animation object
65+
line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs=(data, lines),
66+
interval=50, blit=False)
67+
68+
plt.show()

Diff for: Snake Game.py

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import pygame
2+
import time
3+
import random
4+
5+
pygame.init()
6+
7+
white = (255, 255, 255)
8+
yellow = (255, 255, 102)
9+
black = (0, 0, 0)
10+
red = (213, 50, 80)
11+
green = (0, 255, 0)
12+
blue = (50, 153, 213)
13+
14+
dis_width = 600
15+
dis_height = 400
16+
17+
dis = pygame.display.set_mode((dis_width, dis_height))
18+
# pygame.display.set_caption('Snake Game by Edureka')
19+
20+
clock = pygame.time.Clock()
21+
22+
snake_block = 10
23+
snake_speed = 10
24+
25+
font_style = pygame.font.SysFont("bahnschrift", 25)
26+
score_font = pygame.font.SysFont('comicsansms', 35)
27+
28+
def Your_score(score):
29+
value = score_font.render("Your Score: " + str(score), True, yellow)
30+
dis.blit(value, [0, 0])
31+
32+
def our_snake(snake_block, snake_list):
33+
for x in snake_list:
34+
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
35+
36+
37+
def message(msg, color):
38+
mesg = font_style.render(msg, True, color)
39+
dis.blit(mesg, [dis_width / 6, dis_height / 3])
40+
41+
42+
def gameLoop():
43+
game_over = False
44+
game_close = False
45+
46+
x1 = dis_width / 2
47+
y1 = dis_height / 2
48+
49+
x1_change = 0
50+
y1_change = 0
51+
52+
snake_List = []
53+
Length_of_snake = 1
54+
55+
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
56+
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
57+
58+
while not game_over:
59+
while game_close == True:
60+
dis.fill(blue)
61+
message('Game Over! Press C-Play; Q-Quit', red)
62+
Your_score(Length_of_snake - 1)
63+
pygame.display.update()
64+
65+
for event in pygame.event.get():
66+
if event.type == pygame.KEYDOWN:
67+
if event.key == pygame.K_q:
68+
game_over = True
69+
game_close = False
70+
if event.key == pygame.K_c:
71+
gameLoop()
72+
73+
for event in pygame.event.get():
74+
if event.type == pygame.QUIT:
75+
game_over = True
76+
if event.type == pygame.KEYDOWN:
77+
if event.key == pygame.K_LEFT:
78+
x1_change = -snake_block
79+
y1_change = 0
80+
elif event.key == pygame.K_RIGHT:
81+
x1_change = snake_block
82+
y1_change = 0
83+
elif event.key == pygame.K_UP:
84+
y1_change = -snake_block
85+
x1_change = 0
86+
elif event.key == pygame.K_DOWN:
87+
y1_change = snake_block
88+
x1_change = 0
89+
90+
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
91+
game_close = True
92+
x1 += x1_change
93+
y1 += y1_change
94+
dis.fill(blue)
95+
pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
96+
snake_Head = []
97+
snake_Head.append(x1)
98+
snake_Head.append(y1)
99+
snake_List.append(snake_Head)
100+
if len(snake_List) > Length_of_snake:
101+
del snake_List[0]
102+
103+
for x in snake_List[:-1]:
104+
if x == snake_Head:
105+
game_close = True
106+
107+
our_snake(snake_block, snake_List)
108+
Your_score(Length_of_snake - 1)
109+
110+
pygame.display.update()
111+
112+
if x1 == foodx and y1 == foody:
113+
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
114+
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
115+
Length_of_snake += 1
116+
117+
clock.tick(snake_speed)
118+
119+
pygame.quit()
120+
quit()
121+
122+
gameLoop()

0 commit comments

Comments
 (0)