-
Notifications
You must be signed in to change notification settings - Fork 0
/
testings.py
116 lines (81 loc) · 3.13 KB
/
testings.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pygame
import random
from random import randint as rcolor
from pygame. constants import QUIT, K_DOWN, K_UP, K_LEFT, K_RIGHT
pygame.init()
FPS = pygame.time.Clock()
screen = width, height = 800, 600
BLACK = 0, 0, 0
WHITE = 255, 255, 255
RED = 255, 0, 0
YELLOW = 255, 255, 0
font = pygame.font.SysFont('Verdana', 20)
main_surface = pygame.display.set_mode(screen)
ball = pygame.Surface((20, 20))
ball.fill((WHITE))
ball_rect = ball.get_rect()
ball_speed = 5
def create_bonus():
bonus = pygame.Surface((20, 20))
bonus.fill(YELLOW)
bonus_rect = pygame.Rect(random.randint(0, width), 0, *bonus.get_size())
bonus_speed = random.randint(1, 5)
return [bonus, bonus_rect, bonus_speed]
def create_enemy():
enemy = pygame.Surface((20, 20))
enemy.fill(RED)
enemy_rect = pygame.Rect(width, random.randint(0, height), *enemy.get_size())
enemy_speed = random.randint(1, 5)
return [enemy, enemy_rect, enemy_speed]
CREATE_ENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(CREATE_ENEMY, 1500)
CREATE_BONUS = pygame.USEREVENT + 2
pygame.time.set_timer(CREATE_BONUS, 1500)
score = 0
enemies = []
bonuses = []
is_working = True
while is_working:
FPS.tick(120)
for event in pygame.event.get():
if event.type == QUIT:
is_working = False
if event.type == CREATE_ENEMY:
enemies.append(create_enemy())
if event.type == CREATE_BONUS:
bonuses.append(create_bonus())
pressed_keys = pygame.key.get_pressed()
#if ball_rect.bottom >= height or ball_rect.top <= 0:
# ball_speed[1] = -ball_speed[1]
# ball.fill((rcolor(0, 255), rcolor(0, 255), rcolor(0,255)))
#if ball_rect.left <= 0 or ball_rect.right >= width:
# ball.fill((rcolor(0, 255), rcolor(0, 255), rcolor(0,255)))
# ball_speed[0] = -ball_speed[0]
main_surface.fill((BLACK))
main_surface.blit(ball, (ball_rect))
main_surface.blit(font.render(str(score), True, WHITE), (width - 30, 0))
for bonus in bonuses:
bonus[1] = bonus[1].move(0, bonus[2])
main_surface.blit(bonus[0], bonus[1])
if bonus[1].bottom > height:
bonuses.pop(bonuses.index(bonus))
if ball_rect.colliderect(bonus[1]):
bonuses.pop(bonuses.index(bonus))
score += 1
for enemy in enemies:
enemy[1] = enemy[1].move(-enemy[2], 0)
main_surface.blit(enemy[0], enemy[1])
if enemy[1].left < 0:
enemies.pop(enemies.index(enemy))
if ball_rect.colliderect(enemy[1]):
is_working = False
if pressed_keys[K_DOWN] and not ball_rect.bottom >= height:
ball_rect = ball_rect.move(0, ball_speed)
if pressed_keys[K_UP] and not ball_rect.top <= 0:
ball_rect = ball_rect.move(0, -ball_speed)
if pressed_keys[K_RIGHT] and not ball_rect.right >= width:
ball_rect = ball_rect.move(ball_speed, 0)
if pressed_keys[K_LEFT] and not ball_rect.left <= 0:
ball_rect = ball_rect.move(-ball_speed, 0)
# main_surface.fill((155, 145, 155))
pygame.display.flip()