def reset(self): # Put paddle in rest position, dependent on player index Sprite.reset(self, x=(-orthoWidth / 2 + self.width) + (orthoWidth - self.width) * Math.random(), y=(-orthoHeight / 2 + self.height) + (orthoHeight - self.height) * Math.random()) '''
def speedUp(self, bat): factor = 1 + 0.15 * ( 1 - Math.abs(self.y - bat.y) / (bat.height // 2) )**2 # Speed will increase more if paddle hit near centre if Math.abs(self.vX) < 3 * self.speed: self.vX *= factor self.vY *= factor
def update_squares(self): if self.pause: for square in self.stimuli: if square.image.fill != self.canvas.backgroundColor: square.image.fill = self.canvas.backgroundColor else: self.delta_exp_timer = (self.time - self.start_exp_timer) if self.delta_exp_timer <= 1000: if self.trial_set != True: for square in self.stimuli: square.reset() red = 255 * Math.random() green = 255 * Math.random() blue = 255 * Math.random() color = f'rgb({red},{green},{blue})' square.image.fill = color if square == self.stimuli[0]: self.target_color = square.image.fill self.trial_set = True if 1000 < self.delta_exp_timer <= 1250: for square in self.stimuli: if square.image.fill != self.canvas.backgroundColor: square.image.fill = self.canvas.backgroundColor if 1250 < self.delta_exp_timer <= 2000: if self.target_presented != True: for square in self.stimuli: if square.index > 0: square.image.fill = self.canvas.backgroundColor else: square.image.fill = self.target_color self.target_presented = True if 2000 < self.delta_exp_timer <= 2500: for square in self.stimuli: if square.image.fill != self.canvas.backgroundColor: square.image.fill = self.canvas.backgroundColor if 2500 < self.delta_exp_timer: self.start_exp_timer = self.time self.trial_set = False self.target_presented = False self.isi_presented = False self.all_presented = False
def reset( self ): # Launch according to service direction with random angle offset from horizontal angle = ( self.game.serviceIndex * Math.PI # Service direction + (1 if Math.random() > 0.5 else -1) * Math.random() * Math.atan(fieldHeight / orthoWidth)) Sprite.reset(self, vX=self.speed * Math.cos(angle), vY=self.speed * Math.sin(angle))
def interact (self): # Paddles and ball assumed infinitely thin # Paddle touches wall self.y = Math.max (self.height // 2 - fieldHeight // 2, Math.min (self.y, fieldHeight // 2 - self.height // 2)) # Paddle hits ball if ( (self.y - self.height // 2) < self.game.ball.y < (self.y + self.height // 2) and ( (self.index == 0 and self.game.ball.x < self.x) # On or behind left paddle or (self.index == 1 and self.game.ball.x > self.x) # On or behind right paddle ) ): self.game.ball.x = self.x # Ball may have gone too far already self.game.ball.vX = -self.game.ball.vX # Bounce on paddle self.game.ball.speedUp (self)
def __init__ (self): self.serviceIndex = 1 if Math.random () > 0.5 else 0 # Index of player that has initial service self.pause = True # Start game in paused state self.keyCode = None self.textFrame = document.getElementById ('text_frame') self.canvasFrame = document.getElementById ('canvas_frame') self.buttonsFrame = document.getElementById ('buttons_frame') self.canvas = __new__ (fabric.Canvas ('canvas', {'backgroundColor': 'black', 'originX': 'center', 'originY': 'center'})) self.canvas.onWindowDraw = self.draw # Install draw callback, will be called asynch self.canvas.lineWidth = 2 self.canvas.clear () self.attributes = [] # All attributes will insert themselves here self.paddles = [Paddle (self, index) for index in range (2)] # Pass game as parameter self self.ball = Ball (self) self.scoreboard = Scoreboard (self) window.setInterval (self.update, 10) # Install update callback, time in ms window.setInterval (self.draw, 20) # Install draw callback, time in ms window.addEventListener ('keydown', self.keydown) window.addEventListener ('keyup', self.keyup) self.buttons = [] for key in ('A', 'Z', 'K', 'M', 'space', 'enter'): button = document.getElementById (key) button.addEventListener ('mousedown', (lambda aKey: lambda: self.mouseOrTouch (aKey, True)) (key)) # Returns inner lambda button.addEventListener ('touchstart', (lambda aKey: lambda: self.mouseOrTouch (aKey, True)) (key)) button.addEventListener ('mouseup', (lambda aKey: lambda: self.mouseOrTouch (aKey, False)) (key)) button.addEventListener ('touchend', (lambda aKey: lambda: self.mouseOrTouch (aKey, False)) (key)) button.style.cursor = 'pointer' button.style.userSelect = 'none' self.buttons.append (button) self.time = + __new__ (Date) window.onresize = self.resize self.resize ()
def set(self): Sprite.reset(self, x=orthoWidth * Math.random(), y=orthoHeight * Math.random())
def reset(self): # create a square and put it in a random position Sprite.reset( self, x = (-orthoWidth/2 + self.width) + (orthoWidth-self.width) * Math.random(), y = (-orthoHeight/2 + self.height) + (orthoHeight-self.height) * Math.random() )