Skip to content

Commit e6de56e

Browse files
committed
changes
1 parent 96c2ca1 commit e6de56e

File tree

11 files changed

+389
-0
lines changed

11 files changed

+389
-0
lines changed

pygame/__other__/flapbird/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
StackOverflow: [https://stackoverflow.com/a/47985300/1832058](https://stackoverflow.com/a/47985300/1832058)
3+
4+
---
5+
6+
![#1](images/flapbird.png?raw=true)
9.77 KB
Loading

pygame/__other__/flapbird/main.py

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import pygame
2+
3+
# --- constants --- (UPPER_CASE_NAMES)
4+
5+
# - colors -
6+
7+
BACKGROUND_0 = (36, 38, 82)
8+
BACKGROUND_1 = (40, 42, 86)
9+
10+
BLACK = (0, 0, 0)
11+
WHITE = (255, 255, 255)
12+
RED = (255, 47, 47)
13+
GREEN = (79, 255, 101)
14+
15+
WOOD = (253, 197, 136)
16+
PIPE = (152, 228, 86)
17+
END = (137, 226, 57)
18+
GOLD = (219, 178, 58)
19+
GOLDEN = (254, 197, 34)
20+
GOLDER = (255, 206, 63)
21+
22+
# - states -
23+
24+
STATE_INTRO = 1
25+
STATE_GAME = 2
26+
STATE_GAMEOVER = 3
27+
28+
# --- classes --- (CamelCaseNames)
29+
30+
#class Wall(pygame.sprite.Sprite):
31+
#
32+
# def __init__(self, x, y, width, height):
33+
# super().__init__()
34+
#
35+
# self.image = pygame.Surface((width, height))
36+
# self.image.fill(GREY)
37+
#
38+
# self.rect = self.image.get_rect()
39+
# self.rect.y = y
40+
# self.rect.x = x
41+
42+
# --- functions --- (lower_case_names)
43+
44+
# empty
45+
46+
# --- main ---
47+
48+
# - init -
49+
50+
pygame.init()
51+
screen = pygame.display.set_mode((1400,700))
52+
53+
font1 = pygame.font.SysFont("Berlin Sans FB Demi", 100, True, False)
54+
text1 = font1.render("You Lost", True, BLACK)
55+
font2 = pygame.font.SysFont("Aharoni", 50, True, False)
56+
text2 = font2.render("RESET", True, WHITE)
57+
58+
# - objects -
59+
60+
player_y = 200
61+
player_x = 10
62+
63+
x_change = 5
64+
y_change = 0
65+
66+
all_pipes = [
67+
#upper pipes
68+
(END, pygame.Rect(195,300,80,40)),
69+
(PIPE, pygame.Rect(200,0,70,300)),
70+
(PIPE, pygame.Rect(350,0,70,450)),
71+
(END, pygame.Rect(345,420,80,40)),
72+
(PIPE, pygame.Rect(490,0,70,480)),
73+
(END, pygame.Rect(485,480,80,40)),
74+
(PIPE, pygame.Rect(630,0,70,450)),
75+
(END, pygame.Rect(625,450,80,40)),
76+
(PIPE, pygame.Rect(770,0,70,430)),
77+
(END, pygame.Rect(765,420,80,40)),
78+
(PIPE, pygame.Rect(910,0,70,400)),
79+
(END, pygame.Rect(905,400,80,40)),
80+
(PIPE, pygame.Rect(1050,0,70,470)),
81+
(END, pygame.Rect(1045,470,80,40)),
82+
(PIPE, pygame.Rect(1190,0,70,430)),
83+
(END, pygame.Rect(1185,430,80,40)),
84+
(GOLD, pygame.Rect(1330,0,70,410)),
85+
(GOLDER, pygame.Rect(1350,0,70,410)),
86+
(GOLDEN, pygame.Rect(1325,410,80,40)),
87+
(PIPE, pygame.Rect(200,400,70,240)),
88+
#lower pipes
89+
(END, pygame.Rect(195,400,80,40)),
90+
(PIPE, pygame.Rect(350,520,70,500)),
91+
(END, pygame.Rect(345,515,80,40)),
92+
(PIPE, pygame.Rect(490,570,70,100)),
93+
(END, pygame.Rect(485,570,80,40)),
94+
(PIPE, pygame.Rect(630,570,70,100)),
95+
(END, pygame.Rect(625,540,80,40)),
96+
(PIPE, pygame.Rect(770,550,70,120)),
97+
(END, pygame.Rect(765,510,80,40)),
98+
(PIPE, pygame.Rect(910,530,70,220)),
99+
(END, pygame.Rect(905,490,80,40)),
100+
(PIPE, pygame.Rect(1050,560,70,220)),
101+
(END, pygame.Rect(1045,560,80,40)),
102+
(PIPE, pygame.Rect(1190,530,70,220)),
103+
(END, pygame.Rect(1185,530,80,40)),
104+
(GOLD, pygame.Rect(1330,530,70,220)),
105+
(GOLDER, pygame.Rect(1350,530,70,220)),
106+
(GOLDEN, pygame.Rect(1325,510,80,40)),
107+
(WOOD, pygame.Rect(0,650,1400,50)),
108+
(GREEN, pygame.Rect(0,640,1400,10)),
109+
]
110+
111+
# - mainloop -
112+
113+
state = STATE_INTRO # STATE_GAME, STATE_GAMEOVER
114+
115+
clock = pygame.time.Clock()
116+
done = False
117+
118+
while not done:
119+
120+
# - events -
121+
122+
for event in pygame.event.get():
123+
if event.type == pygame.QUIT:
124+
done = True
125+
126+
if state == STATE_INTRO:
127+
if event.type == pygame.KEYDOWN:
128+
state = STATE_GAME
129+
130+
elif state == STATE_GAME:
131+
if event.type == pygame.KEYDOWN:
132+
if event.key == pygame.K_SPACE:
133+
y_change = -5
134+
elif event.type == pygame.KEYUP:
135+
if event.key == pygame.K_SPACE:
136+
y_change = 5
137+
138+
elif state == STATE_GAMEOVER:
139+
if event.type == pygame.KEYDOWN:
140+
state = STATE_INTRO
141+
player_y = 200
142+
player_x = 10
143+
144+
# - updates (without draws) -
145+
146+
if state == STATE_GAME:
147+
player_x += x_change
148+
player_y += y_change
149+
150+
# check collisions with all pipes
151+
152+
for pipe_color, pipe_rect in all_pipes:
153+
if pipe_rect.collidepoint(player_x, player_y):
154+
state = STATE_GAMEOVER
155+
break # no need to check other
156+
157+
# - draws (without updates) -
158+
159+
if state in (STATE_INTRO, STATE_GAME):
160+
screen.fill(BACKGROUND_0)
161+
pygame.draw.rect(screen, BACKGROUND_1, (0, 350, 1400, 350), 0)
162+
163+
# draw all pipes
164+
165+
for pipe_color, pipe_rect in all_pipes:
166+
pygame.draw.rect(screen, pipe_color, pipe_rect, 0)
167+
168+
pygame.draw.circle(screen, WHITE, (player_x, player_y), 5)
169+
170+
if state == STATE_GAMEOVER:
171+
screen.fill(WHITE)
172+
screen.blit(text1, (500, 100))
173+
pygame.draw.rect(screen, GREEN, (600, 400, 190, 60), 0)
174+
screen.blit(text2, (630, 410))
175+
176+
pygame.display.update()
177+
clock.tick(25)
178+
179+
# - end -
180+
181+
pygame.quit()

pygame/__other__/particles/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
StackOverflow: [https://stackoverflow.com/a/47971392/1832058](https://stackoverflow.com/a/47971392/1832058)
3+
4+
---
5+
![#1](images/particles.png?raw=true)
12.1 KB
Loading

pygame/__other__/particles/main.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import pygame
2+
import sys
3+
import random
4+
import math
5+
6+
# --- constants ---- (UPPER_CASE_NAMES)
7+
8+
WHITE = (255, 255, 255)
9+
BLACK = (0, 0, 0)
10+
RED = (255, 0, 0)
11+
GREEN = (0, 255, 0)
12+
BLUE = (0, 0, 255)
13+
14+
SCREEN_WIDTH = 800
15+
SCREEN_HEIGHT = 600
16+
17+
FPS = 30
18+
19+
# --- classes --- (CamelCaseNames)
20+
21+
class Particle:
22+
23+
def __init__(self, x, y, color=BLUE, radius=4, speed=0, angle=0):
24+
25+
self.color = color
26+
self.radius = radius
27+
self.speed = speed
28+
self.angle = angle
29+
30+
self.image = pygame.Surface((radius*2, radius*2)).convert()
31+
pygame.draw.circle(self.image, color, (radius, radius), radius)
32+
33+
self.rect = self.image.get_rect(centerx=x, centery=y)
34+
35+
def draw(self, surface):
36+
surface.blit(self.image, self.rect)
37+
38+
def update(self, surface_rect):
39+
40+
self.dx = math.sin(self.angle) * self.speed
41+
self.dy = math.cos(self.angle) * self.speed
42+
43+
self.rect.x += self.dx
44+
self.rect.y -= self.dy
45+
46+
if self.rect.right >= surface_rect.right:
47+
self.rect.right = surface_rect.right
48+
self.angle = -self.angle
49+
elif self.rect.left <= surface_rect.left:
50+
self.rect.left = surface_rect.left
51+
self.angle = -self.angle
52+
53+
if self.rect.bottom >= surface_rect.bottom:
54+
self.rect.bottom = surface_rect.bottom
55+
self.angle = math.pi - self.angle
56+
57+
elif self.rect.top <= surface_rect.top:
58+
self.rect.top = surface_rect.top
59+
self.angle = math.pi - self.angle
60+
61+
# --- functions --- (lower_case_names)
62+
63+
def collide(p1, p2):
64+
65+
dx = p1.rect.x - p2.rect.x
66+
dy = p1.rect.y - p2.rect.y
67+
68+
distance = math.hypot(dx, dy)
69+
70+
if distance < p1.radius + p2.radius:
71+
tangen = math.atan2(dy, dx)
72+
angle = math.pi/2 + tangen
73+
#print('angle:', math.degrees(angle))
74+
75+
#print('before:', math.degrees(p1.angle),math.degrees(p2.angle))
76+
p1.angle = 2*tangen - p1.angle
77+
p2.angle = 2*tangen - p2.angle
78+
#print(' after:', math.degrees(p1.angle),math.degrees(p2.angle))
79+
#print('-----')
80+
81+
# --- main ---
82+
83+
number_of_particles = 100
84+
particles = []
85+
86+
# - init -
87+
88+
pygame.init()
89+
90+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
91+
screen_rect = screen.get_rect()
92+
93+
# - objects -
94+
95+
for n in range(number_of_particles):
96+
#margin = random.randint(10, 50)
97+
margin = 10
98+
x = random.randint(margin, SCREEN_WIDTH-margin)
99+
y = random.randint(margin, SCREEN_HEIGHT-margin)
100+
speed = 4
101+
angle = random.uniform(0, 2*math.pi)
102+
color = random.choice([RED, GREEN, BLUE])
103+
104+
item = Particle(x, y, speed=speed, angle=angle, color=color)
105+
particles.append(item)
106+
107+
item = Particle(100, 100, speed=2, angle=math.radians(-45+180), color=RED)
108+
particles.append(item)
109+
110+
item = Particle(300, 300, speed=2, angle=math.radians(-45), color=GREEN)
111+
particles.append(item)
112+
113+
# - mainloop -
114+
115+
speed = 0
116+
117+
clock = pygame.time.Clock()
118+
119+
while True:
120+
121+
for event in pygame.event.get():
122+
if event.type == pygame.QUIT:
123+
pygame.quit()
124+
sys.exit()
125+
126+
elif event.type == pygame.KEYDOWN:
127+
if event.key == pygame.K_ESCAPE:
128+
pygame.quit()
129+
sys.exit()
130+
elif event.key == pygame.K_UP:
131+
speed += 1
132+
elif event.key == pygame.K_DOWN:
133+
speed -= 1
134+
135+
elif event.type == pygame.KEYUP:
136+
if event.key == pygame.K_UP:
137+
speed = 0
138+
elif event.key == pygame.K_DOWN:
139+
speed = 0
140+
141+
screen.fill(BLACK)
142+
143+
#partikel.api = 1 #masih g bisa
144+
for i, item1 in enumerate(particles):
145+
item1.speed += speed
146+
item1.update(screen_rect)
147+
item1.draw(screen)
148+
for item2 in particles[i+1:]:
149+
collide(item1, item2)
150+
151+
pygame.display.update()
152+
clock.tick(FPS)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
StackOverflow: [https://stackoverflow.com/a/47984929/1832058](https://stackoverflow.com/a/47984929/1832058)
3+
4+
---
5+
6+
Press key `q` to draw random line on `Canvas`.
7+
8+
## main-1.py
9+
10+
![#1](images/tkinter-canvas-random-lines-1.png?raw=true)
11+
12+
## main-2.py
13+
14+
![#2](images/tkinter-canvas-random-lines-2.png?raw=true)
Loading
Loading
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import tkinter as tk
2+
3+
def myfunction(event):
4+
canvas.create_line(0,0,400,400)
5+
6+
root = tk.Tk()
7+
canvas = tk.Canvas(root, width=400, height=400)
8+
canvas.pack()
9+
10+
root.bind('q', myfunction)
11+
12+
root.mainloop()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import tkinter as tk
2+
import random
3+
4+
def myfunction(event):
5+
x1 = random.randint(0, 400)
6+
y1 = random.randint(0, 400)
7+
x2 = random.randint(0, 400)
8+
y2 = random.randint(0, 400)
9+
canvas.create_line(x1, y1, x2, y2)
10+
11+
root = tk.Tk()
12+
root.title('Picasso')
13+
14+
canvas = tk.Canvas(root, width=400, height=400)
15+
canvas.pack()
16+
17+
root.bind('q', myfunction)
18+
19+
root.mainloop()

0 commit comments

Comments
 (0)