-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_arkanoid.py
More file actions
122 lines (90 loc) · 2.84 KB
/
simple_arkanoid.py
File metadata and controls
122 lines (90 loc) · 2.84 KB
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
import pygame
import random
import sys
import math
from pygame.time import Clock
from pygame import Color, Rect
pygame.init()
class World(object):
def __init__(self, x, y):
self.screen = pygame.display.set_mode((x, y))
class Scene(object):
def __init__(self, x, y):
pass
class Sprite(object):
def __init__(self, x, y, world):
self.x = x
self.y = y
self.world = world
self.color = self.random_color()
self.rect = Rect(self.x, self.y, 20, 20)
self.drawme = True
def random_color(self):
color = Color("white")
color.hsva = (random.randint(0,350), 90, 80, 60)
return color
class Bloque(Sprite):
def draw(self):
self.rect = Rect(self.x, self.y, 40, 20)
pygame.draw.rect(self.world.screen, self.color, self.rect)
class Barrita(Sprite):
def draw(self):
self.rect = Rect(self.x, self.y, 60, 20)
pygame.draw.rect(self.world.screen, self.color, self.rect)
def mover_izq(self):
self.x -= 3
def mover_der(self):
self.x += 3
class Pelota(Sprite):
def __init__(self, x, y, world):
self.x = x
self.y = y
self.world = world
self.color = self.random_color()
self.speed = 4
self.angle = math.radians(160)
def draw(self):
self.x += math.sin(self.angle) * self.speed
self.y += math.cos(self.angle) * self.speed
self.rect = Rect(self.x, self.y, 20, 20)
x = int(self.x + 10)
y = int(self.y + 10)
pygame.draw.circle(self.world.screen, self.color, (x, y), 10)
def collide(self):
self.angle = self.angle + math.radians(170)
def main():
bloques = []
world = World(800,600)
clock = Clock()
for n in xrange(0, 240):
x = 20 + (n%20) * 40
y = 20 + (n/20) * 20
bloques.append(Bloque(x, y, world))
barrita = Barrita(400, 500, world)
pelota = Pelota(400, 300, world)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]: barrita.mover_izq()
if key[pygame.K_RIGHT]: barrita.mover_der()
if key[pygame.K_UP]: pelota.collide()
world.screen.fill((0,0,0))
barrita.draw()
pelota.draw()
collided = True
if collided and barrita.rect.colliderect(pelota.rect):
pelota.collide()
collided = False
for c in bloques:
if c.drawme:
if collided and c.rect.colliderect(pelota.rect):
pelota.collide()
c.drawme = False
collided = False
c.draw()
collided = True
pygame.display.update()
clock.tick(50)
if __name__ == '__main__':
main()