Skip to content

Commit 82c0876

Browse files
committed
other
1 parent 87a9264 commit 82c0876

File tree

7 files changed

+269
-0
lines changed

7 files changed

+269
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
3+
import BaseHTTPServer
4+
import CGIHTTPServer
5+
6+
class MyHandler(CGIHTTPServer.CGIHTTPRequestHandler):
7+
8+
cgi_exts = ['.py']
9+
10+
def do_GET(self):
11+
"""Serve a GET request."""
12+
f = self.send_head()
13+
if f:
14+
try:
15+
self.wfile.write("ABBA\n")
16+
finally:
17+
f.close()
18+
19+
def is_cgi(self):
20+
collapsed_path = _url_collapse_path(self.path)
21+
dir_sep = collapsed_path.find('/', 1)
22+
head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
23+
if head in self.cgi_directories:
24+
if tail.endswith('.py'):
25+
self.cgi_info = head, tail
26+
return True
27+
return False
28+
29+
server = BaseHTTPServer.HTTPServer(('', 8000), MyHandler)
30+
server.serve_forever()
31+

pygame/Vector2-angle/example-1.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
angle between line from A to B and horizontal line
5+
'''
6+
7+
import math
8+
import pygame
9+
10+
data = [
11+
[(5, 2), (4, 6)], # 104.03624346792648
12+
[(0, 0), (14, 14)], # 45.0
13+
[(0, 0), (0, 14)], # 90.0
14+
[(0, 0), (0, -14)], # -90.0
15+
[(0, 0), (14, 0)], # 0.0
16+
[(0, 0), (-14, 0)], # 180.0
17+
[(0, 0), (2, 1.31545)], # 33.333
18+
]
19+
20+
for (ax, ay), (bx, by) in data:
21+
22+
print(" Math:", math.degrees(math.atan2(by - ay, bx - ax)))
23+
24+
#a = pygame.math.Vector2( (ax, ay) )
25+
#b = pygame.math.Vector2( (bx, by) )
26+
# or
27+
a = pygame.math.Vector2(ax, ay)
28+
b = pygame.math.Vector2(bx, by)
29+
30+
zero = pygame.math.Vector2()
31+
32+
print('PyGame:', zero.angle_to(b-a))
33+
34+
#a = pygame.math.Vector2( pygame.mouse.get_pos() )
35+
#b = pygame.math.Vector2( player.rect.center )
36+

pygame/Vector2-angle/example-2.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
angle between line from A to B and horizontal line
5+
'''
6+
7+
#!/usr/bin/env python3
8+
9+
#
10+
# pygame (empty) template - by furas
11+
#
12+
13+
# ---------------------------------------------------------------------
14+
15+
__author__ = 'Bartlomiej "furas" Burek'
16+
__webpage__ = 'http://blog.furas.pl'
17+
18+
# ---------------------------------------------------------------------
19+
20+
import pygame
21+
22+
# === CONSTANS === (UPPER_CASE names)
23+
24+
BLACK = ( 0, 0, 0)
25+
WHITE = (255, 255, 255)
26+
27+
RED = (255, 0, 0)
28+
GREEN = ( 0, 255, 0)
29+
BLUE = ( 0, 0, 255)
30+
31+
SCREEN_WIDTH = 600
32+
SCREEN_HEIGHT = 400
33+
34+
# === CLASSES === (CamelCase names)
35+
36+
# empty
37+
38+
# === FUNCTIONS === (lower_case names)
39+
40+
def angle(ax, ay, bx, by):
41+
42+
print(" Math:", math.degrees(math.atan2(by - ay, bx - ax)))
43+
44+
#a = pygame.math.Vector2( (ax, ay) )
45+
#b = pygame.math.Vector2( (bx, by) )
46+
# or
47+
a = pygame.math.Vector2(ax, ay)
48+
b = pygame.math.Vector2(bx, by)
49+
50+
zero = pygame.math.Vector2()
51+
52+
print('PyGame:', zero.angle_to(b-a))
53+
54+
#a = pygame.math.Vector2( pygame.mouse.get_pos() )
55+
#b = pygame.math.Vector2( player.rect.center )
56+
57+
58+
# === MAIN === (lower_case names)
59+
60+
# --- (global) variables ---
61+
62+
# --- init ---
63+
64+
pygame.init()
65+
66+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
67+
screen_rect = screen.get_rect()
68+
69+
# --- objects ---
70+
71+
ax, ay = screen_rect.center
72+
73+
# --- mainloop ---
74+
75+
clock = pygame.time.Clock()
76+
is_running = True
77+
78+
while is_running:
79+
80+
# --- events ---
81+
82+
for event in pygame.event.get():
83+
84+
# --- global events ---
85+
86+
if event.type == pygame.QUIT:
87+
is_running = False
88+
elif event.type == pygame.KEYDOWN:
89+
if event.key == pygame.K_ESCAPE:
90+
is_running = False
91+
92+
# --- objects events ---
93+
94+
# empty
95+
96+
# --- updates ---
97+
98+
bx, by = pygame.mouse.pos()
99+
100+
# --- draws ---
101+
102+
screen.fill(BLACK)
103+
104+
pygame.draw.line(screen, )
105+
106+
pygame.display.update()
107+
108+
# --- FPS ---
109+
110+
clock.tick(25)
111+
112+
# --- the end ---
113+
114+
pygame.quit()
115+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
from PyQt4 import QtGui, QtCore
5+
6+
def num(i):
7+
print i
8+
i += 1
9+
if i < 10:
10+
# run again after 2000ms with argument
11+
QtCore.QTimer.singleShot(2000, lambda:num(i))
12+
13+
app = QtGui.QApplication(sys.argv)
14+
15+
# run first time with start argument
16+
num(1)
17+
#QtCore.QTimer.singleShot(2000, lambda:num(1))
18+
19+
sys.exit(app.exec_())

tkinter/button-keypad/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
## example-1.py
3+
4+
![#1](screenshots/image-1.png?raw=true)

tkinter/button-keypad/example-1.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
3+
import tkinter as tk
4+
5+
6+
def code(value):
7+
# inform function to use external/global variable
8+
global pin
9+
10+
if value == '*':
11+
# remove last element from `pin`
12+
pin = pin[:-1]
13+
# remove all from `entry` and put new `pin`
14+
e.delete('0', 'end')
15+
e.insert('end', pin)
16+
17+
elif value == '#':
18+
# check pin
19+
if pin == "3529":
20+
print("PIN OK")
21+
else:
22+
print("PIN ERROR!")
23+
# clear pin
24+
pin = ''
25+
e.delete('0', 'end')
26+
27+
else:
28+
# add number to `pin`
29+
pin += value
30+
# add number to `entry`
31+
e.insert('end', value)
32+
33+
print("Current:", pin)
34+
35+
# --- main ---
36+
37+
# keypad description
38+
keys = [
39+
['1', '2', '3'],
40+
['4', '5', '6'],
41+
['7', '8', '9'],
42+
['*', '9', '#'],
43+
]
44+
45+
# create global variable
46+
pin = '' # empty string
47+
48+
# init
49+
root = tk.Tk()
50+
51+
# create `entry` to display `pin`
52+
e = tk.Entry(root, justify='right')
53+
e.grid(row=0, column=0, columnspan=3, ipady=5)
54+
55+
# create `buttons` using `keys
56+
for y, row in enumerate(keys, 1):
57+
for x, key in enumerate(row):
58+
# `lambda` inside `for` have to use `val=key:code(val)`
59+
# instead of direct `code(key)`
60+
b = tk.Button(root, text=key, command=lambda val=key:code(val))
61+
b.grid(row=y, column=x, ipadx=10, ipady=10)
62+
63+
# start program
64+
root.mainloop()
6.15 KB
Loading

0 commit comments

Comments
 (0)