<commit_before>#!/usr/bin/env python

# (c) <NAME>
# http://github.com/itsdogtime/-y-bird

import os
from random import randint
from time import sleep
import curses

class Player():
	def __init__(self):
		self.tile = "@"
		self.x = 16
		self.y = 12
		self.vel = 0
		self.vcap = 3
		self.score = 0
		self.dead = False

	def move(self,flap):
		if flap:
			self.flap()
		else:
			self.gravity()
			if self.vcap > 3:
				self.vel = 3

		player.y += player.vel

	def flap(self):
		player.vel = -3

	def gravity(self):
		self.vel += 1

class Walls():
	def __init__(self):
		self.tile = "|"
		self.space_size = 15
		self.hole_size = 2
		self.edge_buffer = 2 + self.hole_size
		self.walls = []
		self.generate_walls()

		# Check how far into space_size we should be initially
		self.step = 0
		for i in self.walls[::-1]:
			if i:
				break
			self.step += 1

	def __getitem__(self,i):
		return self.walls[i]

	def generate_walls(self): # returns a list of ints. the list[pos] is the wall itself, the number is the hole
		self.walls = [randint(2,res["h"]-self.edge_buffer) if x % self.space_size == 0 else 0 for x in xrange(0,res["w"])]

	def increment_walls(self):
		self.step += 1
		self.walls.pop(0)

		if self.step == self.space_size:
			self.walls.append(randint(self.edge_buffer,res["h"]-self.edge_buffer))
			self.step = 0
		else:
			self.walls.append(0)

def get_res():
	h,w = stdscr.getmaxyx()
	return {"w" : w, "h": h}

def build_map(res):
	line = [" " for x in xrange(res["w"])]
	cmap = [line for x in xrange(res["h"])]
	return cmap

def print_map(cmap, walls):
	max_height = len(cmap)
	max_width = len(cmap[0])

	mapstring = []

	for i_y,y in enumerate(cmap): # iter over y
		for i_x,x in enumerate(y):# iter over x

			if i_y == max_height - 1 and i_x == max_width - 1: 
				break # Do not write to last character in screen or curses will abort (ERR)

			if i_x == 2 and i_y == 12:
				mapstring.append("Score: %03d" % player.score) # print the score
			elif i_x > 2 and i_x < 12 and i_y == 12: #don't print 9 chars to account for adding 10 in 1 iter
				pass
			elif player.x == i_x and player.y == i_y:
				mapstring.append(player.tile)
			elif walls[i_x]:
				if i_y < walls[i_x] - walls.hole_size or i_y > walls[i_x] + walls.hole_size:
					mapstring.append(walls.tile)
				else:
					mapstring.append(x)
			else:
				# stdscr.addstr(x)
				mapstring.append(x)

	return "".join(mapstring)


def collision_detect():
	collision = False
	if walls[player.x]:
		if player.y < walls[player.x] - walls.hole_size or player.y > walls[player.x] + walls.hole_size:
			collision = True # if player is not in walls.hole_size
		else:
			player.score += 1
	return collision


def main(fps):
	global player
	global walls
	cmap = build_map(res)

	while True:

		p_input = stdscr.getch()

		if p_input == ord('q') or p_input == ord('Q'):
			stdscr.clear()
			stdscr.refresh()
			exit(0)
		elif p_input == ord('r') or p_input == ord('R'):
			player = Player()
			walls = Walls()
		elif p_input == ord(' '):
			player.move(True)
		else:
			player.move(False)

		if not player.dead:
			walls.increment_walls()
			player.dead = collision_detect()
			frame = print_map(cmap, walls)
		else:
			frame = ["Game Over. Thanks Obama.\n",
			"Final score = %03d\n"% player.score,
			"Press 'r' to reset.\n",
			"Press 'q' to quit."]
			frame = "".join(frame)

		stdscr.clear()
		stdscr.addstr(frame)
		stdscr.refresh()

		sleep(fps)


if __name__ == "__main__":

	stdscr = curses.initscr()
	stdscr.keypad(1)
	stdscr.nodelay(1)

	fps = .1
	res = get_res()
	walls = Walls()
	player = Player()
	main(fps)
<commit_msg>	modified:   main.py Fixed display issues resuming to terminal on 	exit. Removed some pointless string/list manipulation. Fixed 	player class referring to itself by name.<commit_after>#!/usr/bin/env python2

# (c) <NAME>
# http://github.com/itsdogtime/-y-bird

import os
from random import randint
from time import sleep
import curses

class CursesScreen():
	def __enter__(self):
		self.stdscr = curses.initscr()
		curses.cbreak()
		curses.noecho()
		self.stdscr.keypad(1)
		self.stdscr.nodelay(1)
		return self.stdscr

	def __exit__(self,a,b,c):
		curses.nocbreak()
		self.stdscr.keypad(0)
		stdscr.clear()
		stdscr.refresh()
		curses.echo()
		curses.endwin()

class Player():
	def __init__(self):
		self.tile = "@"
		self.x = 16
		self.y = 12
		self.vel = 0
		self.vcap = 3
		self.score = 0
		self.dead = False

	def move(self,flap):
		if flap:
			self.flap()
		else:
			self.gravity()
			if self.vcap > 3:
				self.vel = 3

		self.y += self.vel

	def flap(self):
		self.vel = -3

	def gravity(self):
		self.vel += 1

class Walls():
	def __init__(self,res,player_x):
		self.res = res
		self.tile = "|"
		self.space_size = 15
		self.hole_size = 2
		self.edge_buffer = 2 + self.hole_size
		self.generate_walls(player_x)
		self.step = 0

	def __getitem__(self,i):
		return self.walls[i]

	def generate_wall(self):
		return randint(self.edge_buffer,self.res["h"]-self.edge_buffer)

	def generate_walls(self,p_x): # sets self.walls as list of ints. the list[pos] is the wall itself, the number is height of the hole
		self.walls = [self.generate_wall() if x % self.space_size == 0 and x > p_x + self.space_size else 0 for x in xrange(0,self.res["w"])]

	def update(self):
		self.step += 1
		self.walls.pop(0)

		if self.step >= self.space_size:
			self.walls.append(self.generate_wall())
			self.step = 0
		else:
			self.walls.append(0)

def get_res():
	h,w = stdscr.getmaxyx()
	return {"w" : w, "h": h}


def draw_map(res, player, walls):

	mapstring = []

	for y in xrange(res['h']):
		for x in xrange(res['w']):

			if y == res['h'] - 1 and x == res['w'] - 1: 
				break # Do not write to last character in screen or curses will abort (ERR)
			if x == 2 and y == res['h'] / 2:
				mapstring.append("Score: %03d" % player.score) # print the score
			elif x > 2 and x < 12 and y == res['h'] / 2: #don't print 9 chars to account for adding 10 in the score
				pass
			elif player.x == x and player.y == y:
				mapstring.append(player.tile)
			elif walls[x]:
				if y < walls[x] - walls.hole_size or y > walls[x] + walls.hole_size:
					mapstring.append(walls.tile)
				else:
					mapstring.append(' ')
			else:
				mapstring.append(' ')

	return "".join(mapstring)


def collision_detect(player,walls):
	collision = False
	if walls[player.x]: # only check on non-zero value (a wall)
		if player.y < walls[player.x] - walls.hole_size or player.y > walls[player.x] + walls.hole_size:
			collision = True # if player is not in walls.hole_size
		else:
			player.score += 1
	return collision


def main_loop(fps):
	player = Player()
	res = get_res()
	walls = Walls(res,player.x)


	while True:

		p_input = stdscr.getch()

		if p_input == ord('q') or p_input == ord('Q'):
			exit(0)
		elif p_input == ord('r') or p_input == ord('R'):
			player = Player()
			walls = Walls(res,player.x)
		elif p_input == ord(' '):
			player.move(True)
		else:
			player.move(False)

		if not player.dead:
			walls.update()
			player.dead = collision_detect(player,walls)
			frame = draw_map(res, player, walls)
		else:
			frame = ["Game Over. Thanks Obama.\n",
			"Final score = %03d\n"% player.score,
			"Press 'r' to reset.\n",
			"Press 'q' to quit."]
			frame = "".join(frame)

		stdscr.clear()
		stdscr.addstr(frame)
		stdscr.refresh()

		sleep(fps)


if __name__ == "__main__":
	with CursesScreen() as stdscr:
		fps = .1
		curses.wrapper(main_loop(fps))
{'fraction_non_alphanumeric': 0.1454349582547805, 'fraction_numerical': 0.012792889846485321, 'mean_word_length': 3.1865839909808344, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6084143', 'n_tokens_mistral': 3278, 'n_tokens_neox': 2831, 'n_words': 768}