Skip to content

Commit

Permalink
lc
Browse files Browse the repository at this point in the history
  • Loading branch information
luchaoqi committed May 11, 2021
1 parent 182d3d1 commit 23eb0a5
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 12 deletions.
12 changes: 0 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@
# Left arrow is 68, right arrow is 67, space is 32
import getch

# Terminal Invaders
#
# A terminal-based clone of Space Invaders.
# Eric Matthes
# @ehmatthes

# This version is meant to use only lists, if statements, while loops
# No classes, dictionaries, etc.


### PARAMETERS ###

# What the ship looks like.
ship_string = '|-/\-|'

Expand Down
121 changes: 121 additions & 0 deletions main2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import pygame


class Game:
screen = None
aliens = []
rockets = []
lost = False

def __init__(self, width, height):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((width, height))
self.clock = pygame.time.Clock()
done = False

hero = Hero(self, width / 2, height - 20)
generator = Generator(self)
rocket = None

while not done:
if len(self.aliens) == 0:
self.displayText("VICTORY ACHIEVED")

pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]: # sipka doleva
hero.x -= 2 if hero.x > 20 else 0 # leva hranice plochy
elif pressed[pygame.K_RIGHT]: # sipka doprava
hero.x += 2 if hero.x < width - 20 else 0 # prava hranice

for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and not self.lost:
self.rockets.append(Rocket(self, hero.x, hero.y))

pygame.display.flip()
self.clock.tick(60)
self.screen.fill((0, 0, 0))

for alien in self.aliens:
alien.draw()
alien.checkCollision(self)
if (alien.y > height):
self.lost = True
self.displayText("YOU DIED")

for rocket in self.rockets:
rocket.draw()

if not self.lost: hero.draw()

def displayText(self, text):
pygame.font.init()
font = pygame.font.SysFont('Arial', 50)
textsurface = font.render(text, False, (44, 0, 62))
self.screen.blit(textsurface, (110, 160))


class Alien:
def __init__(self, game, x, y):
self.x = x
self.game = game
self.y = y
self.size = 30

def draw(self):
pygame.draw.rect(self.game.screen, # renderovací plocha
(81, 43, 88), # barva objektu
pygame.Rect(self.x, self.y, self.size, self.size))
self.y += 0.05

def checkCollision(self, game):
for rocket in game.rockets:
if (rocket.x < self.x + self.size and
rocket.x > self.x - self.size and
rocket.y < self.y + self.size and
rocket.y > self.y - self.size):
game.rockets.remove(rocket)
game.aliens.remove(self)


class Hero:
def __init__(self, game, x, y):
self.x = x
self.game = game
self.y = y

def draw(self):
pygame.draw.rect(self.game.screen,
(210, 250, 251),
pygame.Rect(self.x, self.y, 8, 5))


class Generator:
def __init__(self, game):
margin = 30 # mezera od okraju obrazovky
width = 50 # mezera mezi alieny
for x in range(margin, game.width - margin, width):
for y in range(margin, int(game.height / 2), width):
game.aliens.append(Alien(game, x, y))

# game.aliens.append(Alien(game, 280, 50))


class Rocket:
def __init__(self, game, x, y):
self.x = x
self.y = y
self.game = game

def draw(self):
pygame.draw.rect(self.game.screen, # renderovací plocha
(254, 52, 110), # barva objektu
pygame.Rect(self.x, self.y, 2, 4))
self.y -= 2 # poletí po herní ploše nahoru 2px/snímek


if __name__ == '__main__':
game = Game(600, 400)
86 changes: 86 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import curses
import collections
import time
import random

# setup the window

curses.initscr()
nlines,ncols,begin_y,begin_x = 10,30,0,0
win = curses.newwin(nlines,ncols,begin_y,begin_x)
curses.noecho()
curses.curs_set(False)
win.keypad(True)
# win.scroll(1)
# win.scrollok(1)
# win.border(0)
# win.nodelay(1)



# spaceship and obstacle init
ship = [nlines-1,ncols//2] # last line, mid of cols
obstacles = collections.deque()



# game logic

score = 0
c = 1
while True:
win.clear()
win.addch(ship[0],ship[1], '*')

if len(obstacles) == nlines:
obstacles.pop()
score += 1

ncol = random.sample(range(begin_x,begin_x + ncols),k = random.randint(0,(ncols-begin_x)//4)) # col index of obstacle
obstacles.appendleft(ncol)


# draw obstacles
for nline in range(len(obstacles)):
for ncol in obstacles[nline]:
win.insstr(nline,ncol,'-')
win.refresh()



# event = win.getch()
# key = event
# if key == curses.KEY_LEFT:

# if key == curses.KEY_RIGHT:


# # margin
# if x > ncols:
# x = ncols



# ship =

# if ship in obstacles[-1]:
# break

# if key in [curses.KEY_LEFT, curses.KEY_RIGHT]:






time.sleep(0.5)
c += 1
if c == 20:
break





curses.endwin()
print('Final score: ' + str(score))

0 comments on commit 23eb0a5

Please sign in to comment.