Skip to content

Commit dd8321a

Browse files
committed
changes
1 parent effdd0d commit dd8321a

File tree

7 files changed

+236
-2
lines changed

7 files changed

+236
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
It calculate new position (after one frame/move) if you have bullet's position and speed, and target's position.
2+
3+
If target doesn't change place or bullet doesn't change angle then you can calculate cx,cy only once.
4+

pygame/math-bullet-to-target/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import math
2+
3+
def move(x1, y1, x2, y2, speed):
4+
5+
# distance
6+
dx = x2 - x1
7+
dy = y2 - y1
8+
9+
# angle
10+
angle = math.atan2(dx, dy)
11+
#print(math.degrees(angle))
12+
13+
#
14+
cx = speed * math.sin(angle)
15+
cy = speed * math.cos(angle)
16+
#print(cx, cy)
17+
18+
# if distance is smaller then `cx/cy`
19+
# then you have to stop in target.
20+
21+
if abs(cx) < abs(dx) or abs(cy) < abs(dy):
22+
# move bullet to new position
23+
x1 += cx
24+
y1 += cy
25+
in_target = False
26+
else:
27+
# move bullet to target
28+
x1 = x2
29+
y1 = y2
30+
in_target = True
31+
32+
return x1, y1, in_target
33+
34+
#---
35+
36+
speed = 10
37+
38+
# bullet position
39+
x1 = 10
40+
y1 = 0
41+
42+
# taget possition
43+
x2 = -130
44+
y2 = 30
45+
46+
print('x: {:6.02f} | y: {:6.02f}'.format(x1, y1))
47+
48+
in_target = False
49+
50+
while not in_target and :
51+
x1, y1, in_target = move(x1, y1, x2, y2, speed)
52+
print('x: {:6.02f} | y: {:6.02f}'.format(x1, y1))

pygame/waypoints-move/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
Moving player using list with waypoints.
3+
4+
Image:
5+
![#1](images/waypoint-move.png?raw=true)
10.3 KB
Loading

pygame/waypoints-move/main.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#!/usr/bin/env python3
2+
3+
import pygame
4+
5+
# === CONSTANS === (UPPER_CASE names)
6+
7+
BLACK = ( 0, 0, 0)
8+
WHITE = (255, 255, 255)
9+
10+
RED = (255, 0, 0)
11+
GREEN = ( 0, 255, 0)
12+
BLUE = ( 0, 0, 255)
13+
14+
SCREEN_WIDTH = 600
15+
SCREEN_HEIGHT = 400
16+
17+
# === CLASSES === (CamelCase names)
18+
19+
class Player():
20+
21+
def __init__(self, waypoints, loop=False):
22+
23+
# create green circe
24+
r = 10
25+
26+
self.image = pygame.Surface((2*r, 2*r)).convert_alpha()
27+
self.rect = self.image.get_rect()
28+
29+
self.image.fill((0,0,0,0))
30+
pygame.draw.circle(self.image, (0,255,0), (r, r), r)
31+
32+
# ---
33+
34+
self.loop = loop
35+
36+
self.speed = 5
37+
38+
self.waypoints = waypoints
39+
self.next_point = 0
40+
41+
# set current position
42+
# I use Vector2 because it keeps position as float numbers
43+
# and it makes calcuations easier and more precise
44+
self.current = pygame.math.Vector2(self.waypoints[0])
45+
46+
# set position in rect to draw it
47+
self.rect.center = self.current
48+
49+
# set end point if exists on list
50+
self.target_index = 1
51+
if self.target_index < len(self.waypoints) - 1:
52+
self.target = pygame.math.Vector2(self.waypoints[self.target_index])
53+
self.moving = True
54+
else:
55+
self.target = self.current
56+
self.moving = False
57+
58+
def move(self):
59+
60+
if self.moving:
61+
62+
# get distance to taget
63+
distance = self.current.distance_to(self.target)
64+
65+
#
66+
if distance > self.speed:
67+
self.current = self.current + (self.target - self.current).normalize() * self.speed
68+
self.rect.center = self.current
69+
else:
70+
# put player in tagert place,
71+
# and find new target on list with waypoints
72+
73+
self.current = self.target
74+
self.rect.center = self.current
75+
76+
# set next end point if exists on list
77+
self.target_index += 1
78+
if self.target_index < len(self.waypoints):
79+
self.target = pygame.math.Vector2(self.waypoints[self.target_index])
80+
else:
81+
if self.loop:
82+
self.target_index = 0
83+
else:
84+
self.moving = False
85+
86+
87+
def draw(self, surface):
88+
surface.blit(self.image, self.rect)
89+
90+
# === MAIN === (lower_case names)
91+
92+
# --- init ---
93+
94+
pygame.init()
95+
96+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
97+
screen_rect = screen.get_rect()
98+
99+
# --- objects ---
100+
101+
start = pygame.math.Vector2(screen_rect.centerx, screen_rect.bottom)
102+
end = start
103+
length = 150
104+
105+
waypoints = [(50, 50), (400, 150), (500, 50), (450, 350), (200, 200), (100, 350), (50, 50)]
106+
107+
player = Player(waypoints, True)
108+
109+
# --- mainloop ---
110+
111+
clock = pygame.time.Clock()
112+
is_running = True
113+
114+
115+
while is_running:
116+
117+
# --- events ---
118+
119+
for event in pygame.event.get():
120+
121+
# --- global events ---
122+
123+
if event.type == pygame.QUIT:
124+
is_running = False
125+
elif event.type == pygame.KEYDOWN:
126+
if event.key == pygame.K_ESCAPE:
127+
is_running = False
128+
129+
# --- objects events ---
130+
131+
# empty
132+
133+
# --- updates ---
134+
135+
# empty
136+
137+
player.move()
138+
139+
# --- draws ---
140+
141+
screen.fill(BLACK)
142+
143+
for start, end in zip(waypoints, waypoints[1:]):
144+
pygame.draw.line(screen, RED, start, end)
145+
146+
player.draw(screen)
147+
148+
pygame.display.update()
149+
150+
# --- FPS ---
151+
152+
clock.tick(25)
153+
154+
# --- the end ---
155+
156+
pygame.quit()

selenium/CONTROL_A__CONTROL_C/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from selenium import webdriver
44
from selenium.webdriver.common.keys import Keys
5-
import pyperclip
5+
#import pyperclip
66

77
driver = webdriver.Chrome()
88
driver.get("http://www.iplsuperfanatic.com/")
@@ -13,7 +13,7 @@
1313
# it works with <body> but not have to with other tags.
1414
element.send_keys(Keys.CONTROL, "a")
1515
element.send_keys(Keys.CONTROL, "c")
16-
print(pyperclip.paste()) # text from Clipboard
16+
#print(pyperclip.paste()) # text from Clipboard
1717

1818
# it gets all text in HTML
1919
# ie. country names in <select>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
3+
from selenium import webdriver
4+
5+
driver = webdriver.Chrome()
6+
driver.get("http://quotes.toscrape.com/js/")
7+
8+
elements = driver.find_elements_by_xpath('//div[@class="quote"]')
9+
10+
for item in elements:
11+
print('text:', item.find_element_by_class_name('text').text[:20] + ' ...')
12+
print('author:', item.find_element_by_class_name('text').text)
13+
14+
all_tags = item.find_elements_by_class_name('tag')
15+
print('tags:', ', '.join(tag.text for tag in all_tags))
16+
17+
print('-----')

0 commit comments

Comments
 (0)