Changeset View
Changeset View
Standalone View
Standalone View
support/arcanoid/arcanoid.py
- This file was moved from scripts/breakout.py.
#!/usr/bin/env python2 | #!/usr/bin/env python3 | ||||
import sys | import sys | ||||
import time | import time | ||||
import select | import select | ||||
import curses | import curses | ||||
from curses import wrapper | from curses import wrapper | ||||
entities = [] | entities = [] | ||||
grid = [] | grid = [] | ||||
class Wall: | class Wall: | ||||
def collide(self, ball): | def collide(self, ball): | ||||
return False | return False | ||||
class Block: | class Block: | ||||
killed = 0 | killed = 0 | ||||
total = 0 | total = 0 | ||||
def __init__(self, x, y, w, h, c): | def __init__(self, x, y, w, h, c): | ||||
self.x = x | self.x = int(round(x)) | ||||
self.y = y | self.y = int(round(y)) | ||||
self.w = w | self.w = int(round(w)) | ||||
self.h = h | self.h = int(round(h)) | ||||
self.fmt = curses.A_BOLD | curses.color_pair(c) | self.fmt = curses.A_BOLD | curses.color_pair(c) | ||||
self.alive = True | self.alive = True | ||||
for i in range(self.x, self.x + self.w): | for i in range(self.x, self.x + self.w): | ||||
for j in range(self.y, self.y + self.h): | for j in range(self.y, self.y + self.h): | ||||
grid[j + 1][i + 1] = self | grid[j + 1][i + 1] = self | ||||
Block.total += 1 | Block.total += 1 | ||||
def collide(self, ball): | def collide(self, ball): | ||||
Show All 11 Lines | def tick(self, win): | ||||
win.addch(j, i, curses.ACS_BLOCK, self.fmt) | win.addch(j, i, curses.ACS_BLOCK, self.fmt) | ||||
return self.alive | return self.alive | ||||
class Ball: | class Ball: | ||||
alive = False | alive = False | ||||
killed = 0 | killed = 0 | ||||
def __init__(self, x, y, vx, vy): | def __init__(self, x, y, vx, vy): | ||||
self.x = x | self.x = int(round(x)) | ||||
self.y = y | self.y = int(round(y)) | ||||
self.vx = vx | self.vx = vx | ||||
self.vy = vy | self.vy = vy | ||||
Ball.alive = True | Ball.alive = True | ||||
def collide(self, ball): | def collide(self, ball): | ||||
return True | return True | ||||
def encounter(self, dx, dy): | def encounter(self, dx, dy): | ||||
dx = int(round(dx)) | |||||
dy = int(round(dy)) | |||||
ent = grid[self.y + dy + 1][self.x + dx + 1] | ent = grid[self.y + dy + 1][self.x + dx + 1] | ||||
if ent and not ent.collide(self): | if ent and not ent.collide(self): | ||||
self.vx -= 2 * dx | self.vx -= 2 * dx | ||||
self.vy -= 2 * dy | self.vy -= 2 * dy | ||||
return ent | return ent | ||||
def tick(self, win): | def tick(self, win): | ||||
while self.y < ship.y: | while self.y < ship.y: | ||||
Show All 10 Lines | def tick(self, win): | ||||
win.addch(self.y, self.x, 'O') | win.addch(self.y, self.x, 'O') | ||||
except curses.error: | except curses.error: | ||||
Ball.alive = False | Ball.alive = False | ||||
Ball.killed += 1 | Ball.killed += 1 | ||||
return Ball.alive | return Ball.alive | ||||
class Ship: | class Ship: | ||||
def __init__(self, x, y): | def __init__(self, x, y): | ||||
self.x = x | self.x = int(round(x)) | ||||
self.y = y | self.y = int(round(y)) | ||||
self.hw = 10 | self.hw = 10 | ||||
self.v = 4 | self.v = 4 | ||||
self.last = 1 | self.last = 1 | ||||
self.update() | self.update() | ||||
def update(self): | def update(self): | ||||
grid[self.y + 1] = ( | grid[self.y + 1] = ( | ||||
[ None ] * (self.x - self.hw + 1) + | [ None ] * (self.x - self.hw + 1) + | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | for x in range(width + 2): | ||||
grid[0][x] = wall | grid[0][x] = wall | ||||
for y in range(height + 2): | for y in range(height + 2): | ||||
grid[y][0] = grid[y][-1] = wall | grid[y][0] = grid[y][-1] = wall | ||||
ship = Ship(width / 2, height - 5) | ship = Ship(width / 2, height - 5) | ||||
entities.append(ship) | entities.append(ship) | ||||
colors = [ 1, 3, 2, 6, 4, 5 ] | colors = [ 1, 3, 2, 6, 4, 5 ] | ||||
h = height / 10 | h = height / 10 | ||||
for x in range(1, width / 7 - 1): | for x in range(1, int(width / 7) - 1): | ||||
for y in range(1, 7): | for y in range(1, 7): | ||||
entities.append(Block(x * 7, | entities.append(Block(x * 7, | ||||
y * h + x / 2 % 2, | y * h + x / 2 % 2, | ||||
7, | 7, | ||||
h, | h, | ||||
colors[y - 1])) | colors[y - 1])) | ||||
while True: | while True: | ||||
▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines |