Skip to content

Instantly share code, notes, and snippets.

@pirocks
Created October 13, 2016 18:54
Show Gist options
  • Save pirocks/f8f6c43945c5979c9bc50e03a03f240c to your computer and use it in GitHub Desktop.
Save pirocks/f8f6c43945c5979c9bc50e03a03f240c to your computer and use it in GitHub Desktop.
import curses
import random
import math
import os
import sys
import time
import logging
logger = logging.getLogger('guessinggame')
hdlr = logging.FileHandler('/var/tmp/guessinggame.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)
class Paddle:
x = 0
y = 0
def __init__(self,x,y):
self.x = x
self.y = y
def move_up(self):
if(self.y >= 0):
self.y -= 1
def move_down(self):
self.y += 1
def draw(self,surface):
try:
surface.addstr(int(self.y),int(self.x),'''p
a
d
d
l
e''')
except:
self.y -= 1
class Ball:
x = 10
y = 10
direction = 0#in radians
def __init__(self):
self.direction = random.uniform(0,6.28)#fix magic constant later
pass
def draw(self,surface):
try:
surface.addstr(int(self.y + 1),int(self.x + 1),"ball")
except curses.error:
self.turn_around()
self.move(1)
def move(self,distance):
def detect_collision():
return False;
self.x += 0.1*math.cos(self.direction)
self.y += 0.1*math.sin(self.direction)
def turn_around(self):
self.direction -= (3.14/2)
left_paddle = None
right_paddle = None
ball = None
things_to_draw = []
window = None
up_arrow = 259
down_arrow = 258
def main():
global left_paddle, right_paddle,ball, window
window = curses.initscr()
curses.start_color()
curses.curs_set(0)
window = curses.newwin(0,0)
window.keypad(1)
left_paddle = Paddle(0,0)
right_paddle = Paddle(0,0)
ball = Ball()
things_to_draw.append(left_paddle)
things_to_draw.append(right_paddle)
things_to_draw.append(ball)
try:
main_loop()
finally:
curses.endwin()#cleans up after we're done
def main_loop():
global time
window.nodelay(1)
while True:
draw_everything()
time.sleep(0.01)
character = int(window.getch())
logger.info(character)
if(character == up_arrow):
logger.info("up")
left_paddle.move_up()
elif(character == down_arrow):
logger.info("down")
right_paddle.move_down()
def draw_everything():
window.erase()
for thing in things_to_draw:
thing.draw(window)
window.refresh()
curses.wrapper(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment