-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygame1.py
92 lines (64 loc) · 1.82 KB
/
pygame1.py
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
import sys
import pygame
import barvy
pygame.init()
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
fps = 60
bg = pygame.image.load("obrazky/sachy/chessboard2.png")
bg_w = bg.get_width()
bg_h = bg.get_height()
nahoru = pygame.K_w
dolu = pygame.K_s
vpravo = pygame.K_d
vlevo = pygame.K_a
pozice = [50, 50]
rychlost = [0, 0, 0, 0]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# zmackl klavesu
if event.key == nahoru:
rychlost[0] = 5
if event.key == dolu:
rychlost[1] = 5
if event.key == vlevo:
rychlost[2] = 5
if event.key == vpravo:
rychlost[3] = 5
if event.type == pygame.KEYUP:
# pustil klavesu
if event.key == nahoru:
rychlost[0] = 0
if event.key == dolu:
rychlost[1] = 0
if event.key == vlevo:
rychlost[2] = 0
if event.key == vpravo:
rychlost[3] = 0
screen.fill(barvy.WHITE)
screen.blit(bg, (0, 0))
screen.blit(bg, (bg_w, 0))
screen.blit(bg, (bg_w * 2, 0))
# gravitace
rychlost[1] += 0.5
pozice[1] += rychlost[1] - rychlost[0]
pozice[0] += rychlost[3] - rychlost[2]
if pozice[0] < 0:
pozice[0] = 0
if pozice[1] < 0:
pozice[1] = 0
if pozice[0] > width - 60:
pozice[0] = width - 60
if pozice[1] > height - 30:
pozice[1] = height - 30
rychlost[1] = 0
obdelnik = pygame.Rect(pozice[0], pozice[1], 60, 30)
pygame.draw.rect(screen, barvy.RED, obdelnik)
pygame.display.update()
clock.tick(fps)