-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmove_rect.py
62 lines (41 loc) · 1.34 KB
/
move_rect.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
# SOURCE: http://www.nerdparadise.com/programming/pygame/part1
# pip install pygame
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
game_active = True
is_blue = True
x = 100
y = 100
clock = pygame.time.Clock()
while game_active:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_active = False
break
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
is_blue = not is_blue
break
pressed = pygame.key.get_pressed()
# If Esc clicked
if pressed[pygame.K_ESCAPE] or not game_active:
break
if pressed[pygame.K_UP] or pressed[pygame.K_w]:
y -= 3
if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
y += 3
if pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
x -= 3
if pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
x += 3
screen.fill((0, 0, 0))
color = (0, 128, 255) if is_blue else (255, 100, 0)
pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
pygame.display.flip()
pygame.display.set_caption(f"move_rect [{int(clock.get_fps())} fps]")
# will block execution until 1/60 seconds have passed
# since the previous time clock.tick was called.
clock.tick(60)