-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.py
130 lines (115 loc) · 3.55 KB
/
first.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pygame
import sys
ancho = 645
alto = 489
color_azul = (0,0, 64)
class Bolita(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__
#Load image
self.image = pygame.image.load('*****bolita.png')
# Obtain square
self.rect = self.image.get_rect()
# Ball position
self.rect.centerx = ancho /2
self.rect.centery = alto /2
# Speed
self.speed = [12, 12]
def update(self):
# Limit ball movement
if self.rect.bottom >= alto or self.rect.top <= 0:
self.speed[1] = -self.speed[1]
if self.rect.right >= ancho or self.rect.left <= 0:
self.speed[0] = -self.speed[0]
# Movement
self.rect.move_ip(self.speed)
class Paleta(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__
#Load image
self.image = pygame.image.load('*******paleta.png')
# Obtain square
self.rect = self.image.get_rect()
# Paleta position X
self.rect.midbottom = (ancho /2, alto-20)
# Speed
self.speed = [0, 0]
def update(self, evento):
# Player position
if evento.key == pygame.K_RIGHT and self.rect.right > 0:
self.speed = [30, 0]
elif evento.key == pygame.K_LEFT and self.rect.left > 0:
self.speed = [-30, 0]
else:
self.speed = [0,0]
# Movement
self.rect.move_ip(self.speed)
class Ladrillo(pygame.sprite.Sprite):
def __init__(self, posicion):
pygame.sprite.Sprite.__init__(self)
#Load image
self.image = pygame.image.load('*****ladrillo.png')
# Obtain square
self.rect = self.image.get_rect()
# Initial position
self.rect.topleft= posicion
class Muro(pygame.sprite.Group):
def __init__(self,cantidadladrillos):
pygame.sprite.Group.__init__(self)
pos_x = 0
pos_y = 20
for i in range(cantidadladrillos):
ladrillo= Ladrillo((pos_x, pos_y))
self.add(ladrillo)
pos_x += ladrillo.rect.width
if pos_x >= ancho:
pos_x = 0
pos_y += ladrillo.rect.height
# Screen
pantalla = pygame.display.set_mode((ancho, alto))
# Screen caption
pygame.display.set_caption("Brick game")
# Watch
reloj = pygame.time.Clock()
# Set press key
pygame.key.set_repeat(30)
bolita= Bolita()
jugador= Paleta()
muro= Muro(50)
# Keep game-window working
while True:
# Deploy FPS
reloj.tick(60)
# Events check
for evento in pygame.event.get():
# Close game window
if evento.type == pygame.QUIT:
sys.exit()
# Keyboard events
elif evento.type == pygame.KEYDOWN:
jugador.update(evento)
# Ball update
bolita.update()
# Colision ball vs player
if pygame.sprite.collide_rect(bolita, jugador):
bolita.speed[1] = -bolita.speed[1]
# Colision ball vs brick
lista= pygame.sprite.spritecollide(bolita, muro, False)
if lista:
ladrillo = lista[0]
cx= bolita.rect.centerx
if cx < ladrillo.rect.left or ladrillo.rect.right:
bolita.speed[0] = -bolita.speed[0]
else:
bolita.speed[1] = -bolita.speed[1]
muro.remove(ladrillo)
# Screen color
pantalla.fill(color_azul)
# Draw our ball
pantalla.blit(bolita.image, bolita.rect)
# Draw our player
pantalla.blit(jugador.image, jugador.rect )
# Draw our bricks
muro.draw(pantalla)
# Refresh screen element
pygame.display.flip()