Skip to content

Instantly share code, notes, and snippets.

@shazow shazow/pytron.py

Created Dec 5, 2010
Embed
What would you like to do?
My Tron-like game from 2006.
'''
pytron - Tron clone written in Python with PyGame
By Andrey Petrov
Thanks to:
Igor Foox, for debugging (v0.3)
Changelog:
2006-11-16 Version 0.3
* Fixed collision detection
* Fixed multi-keystroke detection
* Fixed game restart after each win
2006-11-06 Version 0.1
* Original version
* Only 90-degree turns
* Ends after each game
'''
import math
import pygame
from pygame.locals import *
import random
import time
width, height = 640, 480
pygame.init()
pygame.display.set_caption('PyTron - Version 0.3')
pygame.mouse.set_visible(0)
pygame.key.set_repeat(200, 20)
clock = pygame.time.Clock()
display_flags = DOUBLEBUF
if pygame.display.mode_ok((width, height), display_flags ):
screen = pygame.display.set_mode((width, height), display_flags)
def rotate(vector, angle):
'''Rotate vector by some angle (in radians)'''
x, y = vector
sin, cos = math.sin(angle), math.cos(angle)
newx = x * cos - y * sin
newy = x * sin + y * cos
return (newx, newy)
class Player(pygame.sprite.Sprite):
turn_rate = 0.015
angle = 0
loser = False
speed = 1
def __init__(self, config, screen):
self.color = config['color']
self.controls = config['controls']
self.name = config['name']
self.screen = screen
self.pos = (random.randint(100,width-100), random.randint(100, height-100)) # Random start position
self.lastpos = self.pos
self.angle = (random.randint(0,math.floor(2/self.turn_rate))) * self.turn_rate
def getPos(self):
(x, y) = self.pos
return (int(x), int(y))
def turn(self, direction):
if direction == self.controls['left']:
self.angle = self.angle - self.turn_rate
elif direction == self.controls['right']:
self.angle = self.angle + self.turn_rate
def move(self):
(x,y) = self.pos
(delta_x, delta_y) = rotate((self.speed,0), self.angle * math.pi)
(new_x, new_y) = (self.pos[0] + delta_x, self.pos[1] + delta_y)
self.pos = (new_x, new_y)
# Do we need to check collision?
if (int(new_x), int(new_y)) != (int(x), int(y)):
self.loser = self.isCollided(self.screen)
def draw(self, screen):
self.move()
screen.set_at(self.getPos(), self.color)
def isCollided(self, screen):
(x,y) = self.getPos()
if x <= 0 or y <= 0 or y >= height or x >= width:
print self.name,"fell off. lol!"
return True
# Remove the previous if statement and uncomment the following block
# to enable bouncing off walls.
#if x <= 0 or x >= width:
#self.angle = 1.0 - self.angle
#return False
#if y <= 0 or y >= height:
#self.angle = 2.0 - self.angle
#return False
if screen.get_at((x,y)) != (0,0,0,255):
print self.name,"collided!"
return True
def notify(self, key):
if key in self.controls.values():
self.turn(key)
return True
return False
def getKeys(self):
return self.controls.values()
class Game:
players = []
def __init__(self, players):
self.players = players
def play(self):
while True:
if pygame.event.peek(QUIT):
return
if pygame.event.peek(KEYDOWN):
pressed = pygame.key.get_pressed()
# Do we want to quit?
if pressed[K_ESCAPE]:
return False
# Which players do we need to notify?
for p in self.players:
[p.notify(key) for key in p.getKeys() if pressed[key]]
for p in self.players:
if p.loser:
self.players.remove(p)
if len(self.players) == 1:
return self.players.pop() # We have our winner
p.draw(screen)
pygame.display.flip()
#clock.tick(60)
time.sleep(0.012)
def main():
#Create The Backgound
background = pygame.Surface(screen.get_size())
background = background.convert()
while True:
# Clear background
background.fill((0,0,0,255))
screen.blit(background, (0, 0))
pygame.display.flip()
# Create new players
players = [Player( {'name': "Red Player",
'color': (255,0,0,255),
'controls': {'left':K_LEFT, 'right':K_RIGHT} },
screen ),
Player( {'name': "Blue Player",
'color': (0,0,255,255),
'controls': {'left':K_a, 'right':K_s} },
screen ),
]
g = Game(players)
winner = g.play()
if winner:
print winner.color, " wins!"
font = pygame.font.Font(None, 36)
text = font.render(winner.name + " WINS!", 1, winner.color)
textpos = text.get_rect(centerx=background.get_width()/2)
background.blit(text, textpos)
screen.blit(background, (0, 0))
pygame.display.flip()
time.sleep(2)
else:
return
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.