def setPos(self, newPos=(0, 0)): """Sets the current position of the mouse, in the same units as the :class:`~visual.Window`. (0,0) is the center. :Parameters: newPos : (x,y) or [x,y] the new position on the screen """ newPosPix = self._windowUnits2pix(numpy.array(newPos)) if usePygame: newPosPix[1] = self.win.size[1] / 2 - newPosPix[1] newPosPix[0] = self.win.size[0] / 2 + newPosPix[0] mouse.set_pos(newPosPix) else: if hasattr(self.win.winHandle, 'set_mouse_position'): if self.win.useRetina: newPosPix = numpy.array(self.win.size) / 4 + newPosPix / 2 else: newPosPix = numpy.array(self.win.size) / 2 + newPosPix x, y = int(newPosPix[0]), int(newPosPix[1]) self.win.winHandle.set_mouse_position(x, y) self.win.winHandle._mouse_x = x self.win.winHandle._mouse_y = y else: msg = 'mouse position could not be set (pyglet %s)' logging.error(msg % pyglet.version)
def turnCamera(self, firstStage: bool): """ Handles the Turning of the Camera Parameters ---------- firstStage : bool A bool which represents whether the Y or X part os the camera should be turned. Returns ------- None """ if firstStage: currentMousePosition = Vector2(*get_pos()) set_pos(self.displayCentre) self.deltaVector = (currentMousePosition - self.displayCentre) * self.sensitivity self.upDownAngle += self.deltaVector.Y self.leftRightAngle += self.deltaVector.X if self.upDownAngle < -80: self.upDownAngle = -80 elif self.upDownAngle > 80: self.upDownAngle = 80 self.updateLookVector() glRotatef(self.upDownAngle, 1, 0, 0) else: glRotatef(self.deltaVector.X, 0.0, 1.0, 0.0)
def _mouse(self, pos = None): if pos is None: pos = mouse.get_pos() else: mouse.set_pos(pos) tile = self._screentile(pos) if not (0 <= tile[0] < self.dimensions[0] and 0 <= tile[1] < self.dimensions[1]): tile = None return pos, tile
def setPos(self,newPos=(0,0)): """Sets the current position of the mouse (pygame only), in the same units as the :class:`~visual.Window` (0,0) is at centre :Parameters: newPos : (x,y) or [x,y] the new position on the screen """ newPosPix = self._windowUnits2pix(numpy.array(newPos)) if usePygame: newPosPix[1] = self.win.size[1]/2-newPosPix[1] newPosPix[0] = self.win.size[0]/2+newPosPix[0] mouse.set_pos(newPosPix) else: print "pyglet does not support setting the mouse position yet"
def setPos(self,newPos=(0,0)): """Sets the current position of the mouse, in the same units as the :class:`~visual.Window`. (0,0) is the center. :Parameters: newPos : (x,y) or [x,y] the new position on the screen """ newPosPix = self._windowUnits2pix(numpy.array(newPos)) if usePygame: newPosPix[1] = self.win.size[1]/2-newPosPix[1] newPosPix[0] = self.win.size[0]/2+newPosPix[0] mouse.set_pos(newPosPix) else: if hasattr(self.win.winHandle, 'set_mouse_position'): newPosPix = self.win.size / 2 + newPosPix x, y = int(newPosPix[0]), int(newPosPix[1]) self.win.winHandle.set_mouse_position(x, y) else: logging.error('mouse position could not be set (pyglet %s)' % pyglet.version)
class Mouse(object): def __init__(self): self._mscalex = 1.0 self._mscaley = 1.0 self._cursor = None self._sprite = None def get_position(self): mx, my = mouse.get_pos() return mx * self._mscalex, my * self._mscaley def set_position(self, (x, y)): mouse.set_pos((x / self._mscalex, y / self._mscaley))
def rotar_jugador(self, mouse_x, mouse_y): #self.ang = math.atan2(-(mouse_y - self.y ), (mouse_x - self.x + 0.0000001)) self.ang = math.atan2(-(mouse_y - (600 / 2)), (mouse_x - (800 / 2) + 0.0000001)) if mouse.get_pos()[0] > ((800 / 2) + 50): mx = (800 / 2) + 50 mouse.set_pos(mx, mouse.get_pos()[1]) elif mouse.get_pos()[0] < ((800 / 2) - 50): mx = (800 / 2) - 50 mouse.set_pos(mx, mouse.get_pos()[1]) if mouse.get_pos()[1] > ((600 / 2) + 50): my = (600 / 2) + 50 mouse.set_pos(mouse.get_pos()[0], my) elif mouse.get_pos()[1] < ((600 / 2) - 50): my = (600 / 2) - 50 mouse.set_pos(mouse.get_pos()[0], my) self.ang = self.ang * (180 / math.pi) self.img = comunes.rot_center(self.img_ori, self.ang + 90 + 180)
def set_angulo(self,radio,angulo): centro = [(self.camara.screen_ancho / 2), (self.camara.screen_alto / 2)] m_x,m_y=comunes.avance_ang_to_deltaXY(radio,angulo) mouse.set_pos(m_x+centro[0], m_y+centro[1]) self.angulo=angulo
def go(): # START # Fairly obvious, initialize the pygame library pygame.init() # Create the world in which all physical things reside. It is our coordinate system and universe. world = World(vector((20.0, 20.0, 2000.0)), vector((-10.0, 0.0, 10.0))) # Create startup cameras and make the world visible to them eyecam = Camera() eyecam.registerObject(world) scopecam = Camera() scopecam.registerObject(world) # Setup the program window (just a special case of a View) screen = Screen(eyecam, vector((1000,800,0)), -1.0) screen.display = pygame.display.set_mode((1000, 800)) # Setup the display surfaces to be drawn to screen and list them main_dim = vector((1000,800,0.0)) scope_dim = vector((240,240,0.0)) mainview = View(eyecam, main_dim, zero_vector, 1.0) scopeview = Scope(scopecam, scope_dim, (main_dim - scope_dim) / 2.0, 4.0) views = [ mainview, scopeview ] # Hide the cursor mouse.set_visible(False) # Let's set up some useful variables now = pygame.time.get_ticks() # The current program time shot_fired = False # chase_bullet = False hide_scope = True lock_mouse = False power = 100.0 #mainview.reposition(eyecam) scopeview.setPower(power) timescale = 1.0 tot_mag = 0.0 tot_dur = 0.0 frames = 0 if lock_mouse: mouse.set_pos((scope_dim / 2.0).xy()) while 1: last_t = now now = pygame.time.get_ticks() t_length = float(now - last_t) if world.has_hit: shot_end = now #break if world.bullet.position.z() > world.target.position.z() + 1000.0: #world.bullet.position = vector((world.bullet.position.x(), world.bullet.position.y(), world.target.position.z() - 0.01)) shot_end = now break #pass for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if event.dict['button'] == 4: power += 1.0 if event.dict['button'] == 5: power -= 1.0 power = scopeview.setPower(power) print power if event.type == pygame.QUIT: sys.exit() sx, sy = (0.5 * mainview.dimensions).xy() if lock_mouse: mx, my = mouse.get_rel() mouse.set_pos(sx, sy) r = (1.0 / (10000 * power))* (vector((sx - mx, sy - my, 0))) scopecam.rotation += r eyecam.rotation += r else: mx, my = mouse.get_pos() r = (1.0 / (1000 * power))* (vector((sx - mx, sy - my, 0))) scopecam.rotation = r eyecam.rotation = r world.bullet.rotation = scopecam.rotation scopeview.reposition(scopeview.camera) mainview.reposition(mainview.camera) b1, b2, b3 = mouse.get_pressed() if b1 and not shot_fired: shot_fired = True shot_start = now world.bullet.velocity = rotate(vector((0.0,0.0,800.0)), (-world.bullet.rotation - scopeview.tweaks)) print 'Start Velocity: ' + str(world.bullet.velocity.magnitude()) #mainview.camera.rotation = zero_vector mainview.setPower(50.0) #scopeview.reposition(view.camera) #scopeview.overlay = [] # hide scope world.movers += [world.bullet] # Move everything. The world does most of the work once we tell it to, # we just need to handle the less tangible stuff. world.moveObjects(timescale * (t_length / 1000.0)) # Bullet chasing if shot_fired: #scopecam.rotation = world.bullet.rotation if hide_scope: scopeview.visible = False bp = world.bullet.position lbp = world.bullet.last_position diff = bp - lbp mag = diff.magnitude() magxz = sqrt(diff.x()**2 + diff.z()**2) frames += 1 tot_mag += mag tot_dur += t_length * timescale if chase_bullet and mag > 0.0: h = arctan2(diff.x(), diff.z()) e = arcsin(diff.y() / mag) eyecam.rotation = vector((h, -e, 0)) eyecam.position = bp & vector((1,1,1)) mainview.setPower(10.0) mainview.reposition(mainview.camera) #print eyecam.position #scopeview.reposition(eyecam) # Grab each view's camera image, then draw them to the program window in sequence for view in views: view.updateDisplay() view.draw(screen.display) # fps = 1000.0 / (t_length + (1 if t_length == 0 else 0)) screen.drawOverlay((fps,)) pygame.display.flip() for view in views: view.updateDisplay() view.draw(screen.display, (screen.dimensions - view.dimensions) / 2.0) pygame.display.flip() print "Flight time: " + str(tot_dur / 1000.0) + " s" print "Impact energy: " + str(.5 * world.bullet.mass * world.bullet.velocity.magnitude() ** 2) + " J" print 'End Position' print world.bullet.position print 'End Velocity: ' + str(world.bullet.velocity.magnitude()) print "Average frame time: " + str((tot_dur / 1000.0) / float(frames)) print "Average FPS: " + str(float(frames) / tot_dur * 1000) print "Average distance per frame: " + str(tot_mag / float(frames)) pygame.time.delay(1000) pygame.display.quit() pygame.quit()
def graph_loop(mass_lower_limit=0.0, mass_upper_limit=0.0, radius_lower_limit=0.0, radius_upper_limit=0.0, is_gas_drwaf=False): m_lo_l = mass_lower_limit m_hi_l = mass_upper_limit r_lo_l = radius_lower_limit r_hi_l = radius_upper_limit fondo = display.set_mode((witdh, height), SCALED) rect = Rect(60, 2, 529, 476) lineas = LayeredUpdates() linea_h = Linea(rect, rect.x, rect.centery, rect.w, 1, lineas) linea_v = Linea(rect, rect.centerx, rect.y, 1, rect.h, lineas) punto = Punto(rect, rect.centerx, rect.centery, lineas) data = {} if any([ mass_lower_limit < 0, mass_upper_limit < 0, radius_lower_limit < 0, radius_upper_limit < 0 ]): raise ValueError() lim_mass_a = int(find_and_interpolate(m_lo_l, mass_keys, exes)) if m_lo_l else 0 lim_mass_b = int(find_and_interpolate(m_hi_l, mass_keys, exes)) if m_hi_l else 0 lim_radius_a = int(find_and_interpolate(r_lo_l, radius_keys, yes)) if r_lo_l else 0 lim_radius_b = int(find_and_interpolate(r_hi_l, radius_keys, yes)) if r_hi_l else 0 move_x, move_y = True, True lockx, locky = False, False mass_value = 0 radius_value = 0 mass_color = negro radius_color = negro mouse.set_pos(rect.center) event.clear() done = False composition_text_comp = None while not done: for e in event.get(): if e.type == QUIT: py_quit() if __name__ == '__main__': sys.exit() elif e.type == MOUSEBUTTONDOWN: if e.button == 1: if (not lockx) or (not locky): if (not lockx) and (not move_x): lockx = True elif (not locky) and (not move_y): locky = True elif not punto.disabled: data['mass'] = round(mass_value, 3) data['radius'] = round(radius_value, 3) data['gravity'] = round( mass_value / (radius_value**2), 3) data['density'] = round( mass_value / (radius_value**3), 3) done = True else: data = {} done = True elif e.button == 3: if lockx: lockx = False move_x = not lockx if locky: locky = False move_y = not locky elif e.type == MOUSEMOTION: px, py = e.pos if move_y: linea_h.move_y(py) punto.move_y(py) if move_x: linea_v.move_x(px) punto.move_x(px) point_x, point_y = punto.rect.center if rect.collidepoint(point_x, point_y) and (move_x or move_y): if mascara.get_at((point_x, point_y)): punto.select() for name in _lineas: if [point_x, point_y] in _lineas[name]: composition_text_comp = name break else: punto.deselect() elif e.type == KEYDOWN: if e.key == K_ESCAPE: py_quit() sys.exit() if e.key == K_LSHIFT: move_x = False elif e.key == K_LCTRL: move_y = False elif e.key == K_SPACE: data['mass'] = round(mass_value, 3) data['radius'] = round(radius_value, 3) data['gravity'] = round(mass_value / (radius_value**2), 3) data['density'] = round(mass_value / (radius_value**3), 3) done = True elif e.type == KEYUP: if e.key == K_LSHIFT: if not lockx: move_x = True elif e.key == K_LCTRL: if not locky: move_y = True px, py = punto.rect.center alto, bajo = 0, 0 if rect.collidepoint((px, py)): for _y in reversed(range(0, py)): if mascara.get_at((px, _y)): alto = _y break for _y in range(py, 476): if mascara.get_at((px, _y)): bajo = _y break a, b = 0, 0 # creo que esto se puede escribir con oneliners. for name in _lineas: if [px, alto] in _lineas[name]: a = name break for name in _lineas: if [px, bajo] in _lineas[name]: b = name break if a and b: c = composiciones silicates = interpolate(py, alto, bajo, c[a]['silicates'], c[b]['silicates']) hydrogen = interpolate(py, alto, bajo, c[a]['hydrogen'], c[b]['hydrogen']) helium = interpolate(py, alto, bajo, c[a]['helium'], c[b]['helium']) iron = interpolate(py, alto, bajo, c[a]['iron'], c[b]['iron']) water_ice = interpolate(py, alto, bajo, c[a]['water ice'], c[b]['water ice']) values = [ i for i in [silicates, hydrogen, helium, iron, water_ice] if i != 0 ] # sólo mostramos los valores mayores a 0% keys, compo = [], [] if silicates: compo.append(str(round(silicates, 2)) + '% silicates') keys.append('silicates') if hydrogen: compo.append(str(round(hydrogen, 2)) + '% hydrogen') keys.append('hydrogen') if helium: compo.append(str(round(helium, 2)) + '% helium') keys.append('helium') if iron: compo.append(str(round(iron, 2)) + '% iron') keys.append('iron') if water_ice: compo.append(str(round(water_ice, 2)) + '% water ice') keys.append('water ice') composition_text_comp = ', '.join(compo) data['composition'] = dict(zip(keys, values)) if hydrogen or helium: data['clase'] = 'Gas Dwarf' data['albedo'] = 30 else: data['clase'] = 'Terrestial Planet' data['albedo'] = 25 else: data = {} mass_value = find_and_interpolate(linea_v.rect.x, exes, mass_keys) radius_value = find_and_interpolate_flipped(linea_h.rect.y, yes, radius_keys) block = Surface(rect.size, SRCALPHA) block_mask = mask.from_surface(block) if any([lim_mass_b, lim_mass_a, lim_radius_a, lim_radius_b]): block_rect = block.get_rect(topleft=rect.topleft) alpha = 150 if lim_mass_a: block.fill([0] * 3 + [alpha], (0, rect.y - 2, lim_mass_a - rect.x, rect.h)) if lim_mass_b: block.fill([0] * 3 + [alpha], (lim_mass_b - rect.x, rect.y - 2, rect.w, rect.h)) if lim_radius_a: block.fill([0] * 3 + [alpha], (0, lim_radius_a, rect.w, rect.h - lim_radius_a)) if lim_radius_b: block.fill([0] * 3 + [alpha], (0, rect.y - 2, rect.w, lim_radius_b)) if is_gas_drwaf: block.blit(gas_drawf, (0, 0)) block_mask = mask.from_surface(block) point_x, point_y = punto.rect.center if block_rect.collidepoint((point_x, point_y)): if block_mask.get_at((point_x - rect.x, point_y - rect.y)): punto.disable() radius_color = rojo mass_color = rojo else: punto.enable() mass_color = negro radius_color = negro mass_text = 'Mass:' + str(round(mass_value, 3)) radius_text = 'Radius:' + str(round(radius_value, 3)) gravity_text = 'Density:' + str( round(mass_value / (radius_value**3), 3)) density_text = 'Gravity:' + str( round(mass_value / (radius_value**2), 3)) if not done: fondo.fill(blanco) fondo.blit(graph, (0, 0)) if block_mask.count() != 0: fondo.blit(block, rect) if punto.disabled: fondo.blit(texto3, rectT3) else: fondo.blit(fuente1.render(mass_text, True, mass_color), (5, rect.bottom + 43)) fondo.blit(fuente1.render(radius_text, True, radius_color), (140, rect.bottom + 43)) fondo.blit(fuente1.render(density_text, True, negro), (130 * 2 - 5, rect.bottom + 43)) fondo.blit(fuente1.render(gravity_text, True, negro), (140 * 3, rect.bottom + 43)) if composition_text_comp is not None: composition_text = 'Composition:' + composition_text_comp fondo.blit( fuente1.render(composition_text, True, negro, blanco), (5, rect.bottom + 64)) fondo.blit(texto1, rectT1) fondo.blit(texto2, rectT2) punto.update() lineas.update() lineas.draw(fondo) display.update() display.quit() return data
def main(): # speed of the snake speed = 10 # loops game_over = False game_close = False snakeX = width / 2 snakeY = height / 2 # get a random image imageNumber = random.randrange(1, 5) fruitImage = pygame.image.load("SnekFiles\\fruit" + str(imageNumber) + ".png") snakeX_change = 0 snakeY_change = 0 snake_List = [] snakeLength = 1 # random position for the food to spawn foodx = round(random.randrange(0, width - snakeSize) / 10.0) * 10.0 foody = round(random.randrange(0, height - snakeSize) / 10.0) * 10.0 while not game_over: # when game ends while game_close == True: window.fill(black) message("You Lost! Press SPACE to Play Again or ESCAPE for Menu.", red) Your_score(snakeLength - 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: P1score = (snakeLength - 1)*score_multiplier score(p1name, P1score) mouse.set_pos(500,400) menu() if event.key == pygame.K_SPACE: main() # code for snake moving for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: snakeX_change = -snakeSize snakeY_change = 0 elif event.key == pygame.K_RIGHT: snakeX_change = snakeSize snakeY_change = 0 elif event.key == pygame.K_UP: snakeY_change = -snakeSize snakeX_change = 0 elif event.key == pygame.K_DOWN: snakeY_change = snakeSize snakeX_change = 0 # if the snake hits the borders you lose if snakeX >= width or snakeX < 0 or snakeY >= height or snakeY < 0: game_close = True snakeX += snakeX_change snakeY += snakeY_change window.blit(map,(0,0)) window.blit(fruitImage, (foodx, foody)) snake_Head = [] snake_Head.append(snakeX) snake_Head.append(snakeY) snake_List.append(snake_Head) # if snake hits itself if len(snake_List) > snakeLength: del snake_List[0] for x in snake_List[:-1]: if x == snake_Head: game_close = True snake(snakeSize, snake_List) Your_score(snakeLength - 1) pygame.display.update() # if snake hits the food then it grows and speeds up if snakeX == foodx and snakeY == foody: foodx = round(random.randrange(0, width - snakeSize) / 10.0) * 10.0 foody = round(random.randrange(0, height - snakeSize) / 10.0) * 10.0 pygame.mixer.init() pygame.mixer.music.load("SnekFiles\\EatSound.mp3") pygame.mixer.music.play(1) pygame.mixer.music.set_volume(0.4) snakeLength += 1 speed += speed_increase imageNumber = random.randrange(1, 5) fruitImage = pygame.image.load("SnekFiles\\fruit" + str(imageNumber) + ".png") clock.tick(speed) pygame.quit() quit()
def menu(): pygame.init() global speed_increase global speed global snakeSize global mx global my global run global click global score_multiplier mouse.set_pos(500,400) pygame.mixer.music.load("SnekFiles\SnekMenu.mp3") pygame.mixer.music.play(99999) pygame.mixer.music.set_volume(0.3) x = True time.sleep(0.5) # loop while x == True: # stuff for buttons in menu button_1 = pygame.Rect(100,600,200,50) button_2 = pygame.Rect(400,600,200,50) button_3 = pygame.Rect(700,600,200,50) button_4 = pygame.Rect(100,692,200,50) button_5 = pygame.Rect(400,692,200,50) button_6 = pygame.Rect(700,692,200,50) mx, my = pygame.mouse.get_pos() window.fill(white) # rectangles for buttons pygame.draw.rect(window, (255,0,0), button_1) pygame.draw.rect(window, (255,0,0), button_2) pygame.draw.rect(window, (255,0,0), button_3) pygame.draw.rect(window, (255,0,0), button_4) pygame.draw.rect(window, (255,0,0), button_5) pygame.draw.rect(window, (255,0,0), button_6) # words text1 = TITLE_FONT.render("PySnek", 1, black) window.blit(text1, (int(width/2 - (text1.get_width()/2)), 20)) text2 = WORD_FONT.render("Instructions:", 1, black) window.blit(text2, (int(width/2 - text2.get_width()/2), 90)) text2_0 = WORD_FONT.render("Move the Snake using your arrow keys", 1, black) window.blit(text2_0, (int(width/2 - text2_0.get_width()/2), 140)) text2_1 = WORD_FONT.render("so that you eat the fruits.", 1, black) window.blit(text2_1, (int(width/2 - text2_1.get_width()/2), 190)) text2_2 = WORD_FONT.render("When you eat a fruit your snake", 1, black) window.blit(text2_2, (int(width/2 - text2_2.get_width()/2), 240)) text2_3 = WORD_FONT.render("your snake will grow and speed up.", 1, black) window.blit(text2_3, (int(width/2 - text2_3.get_width()/2), 290)) text2_4 = WORD_FONT.render("If you run into your tail, you lose.", 1, black) window.blit(text2_4, (int(width/2 - text2_4.get_width()/2), 340)) text3 = WORD_FONT.render("Easy", 1, black) window.blit (text3, (int(width/5 - text3.get_width()/2), 605)) text4 = WORD_FONT.render("Medium", 1, black) window.blit(text4, (int(width/2 - text4.get_width()/2), 605)) text5 = WORD_FONT.render("Hard", 1, black) window.blit(text5, (int(width/1.25 - text5.get_width()/2), 605)) text6 = WORD_FONT.render("Scores", 1, black) window.blit(text6, (int(width/5 - text6.get_width()/2), 700)) text7 = WORD_FONT.render("Maps", 1, black) window.blit(text7, (int(width/2 - text7.get_width()/2), 700)) text8 = WORD_FONT.render("Quit", 1, black) window.blit(text8, (int(width/1.255 - text8.get_width()/2), 700)) pygame.display.update() time.sleep(.5) for event in pygame.event.get(): # to quit game using the X if event.type == pygame.QUIT: run = False sys.quit() break # checks if you are clicking with your mouse or not if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: click = True # code behind clicking the buttons if button_1.collidepoint((mx,my)): if click: speed_increase = 1 score_multiplier = 1 pygame.mixer.music.stop() x = False main() pass if button_2.collidepoint((mx,my)): if click: speed_increase = 3 score_multiplier = 1.25 pygame.mixer.music.stop() x = False main() pass if button_3.collidepoint((mx,my)): if click: speed_increase = 5 score_multiplier = 1.5 pygame.mixer.music.stop() x = False main() pass if button_4.collidepoint((mx,my)): if click: pygame.quit() leaderboard() pass if button_5.collidepoint((mx,my)): if click: pygame.mixer.music.stop() chooseMap() pass if button_6.collidepoint((mx,my)): if click: pygame.quit() quit() pass click = False
elif rhombus.collidepoint(mouse_pos): grab = rhombus other = triangle elif triangle.collidepoint(mouse_pos): grab = triangle other = rhombus Y_triangle = triangle.project((0, 1)) Y_rhombus = rhombus.project((0, 1)) draw.line(SCREEN, (255, 0, 0), (2, Y_triangle[0]), (2, Y_triangle[1]), 2) draw.line(SCREEN, (0, 0, 255), (7, Y_rhombus[0]), (7, Y_rhombus[1]), 2) X_triangle = triangle.project((1, 0)) X_rhombus = rhombus.project((1, 0)) draw.line(SCREEN, (255, 0, 0), (X_triangle[0], 2), (X_triangle[1], 2), 2) draw.line(SCREEN, (0, 0, 255), (X_rhombus[0], 7), (X_rhombus[1], 7), 2) draw.circle(SCREEN, (255, 255, 255), triangle.C, 3) draw.circle(SCREEN, (255, 255, 255), rhombus.C, 3) if grab: olap_axis = grab.collidepoly(other) if not olap_axis is False: MTD = -min(dict((_dot(a, a), a) for a in olap_axis).items())[1] MTD = [round_away(x) for x in MTD] grab.move_ip(*MTD) mouse.set_pos(grab.C) else: grab.C = mouse_pos display.update()
def _synch(self): self.ignoreMove = True set_pos(self.cursor.pos) # this generates an unnecessary move-event
if key == pygame.K_SPACE: fly = True fall = False if key == pygame.K_LSHIFT: fall = True fly = False if key == pygame.K_l: if lines: lines = False else: lines = True if key == pygame.K_ESCAPE: if pause: pause = False mouse.set_visible(False) mouse.set_pos(300, 300) else: pause = True mouse.set_visible(True) if key == pygame.K_p: if perspective: perspective = False else: perspective = True if event.type == pygame.KEYUP: key = event.key if key == pygame.K_RIGHT: right = False if key == pygame.K_LEFT: left = False if key == pygame.K_UP:
def MENU(menu): mouse.set_pos(100, 100) while menu: #creating the buttons button_1 = pygame.Rect(100, 600, 200, 50) button_2 = pygame.Rect(400, 600, 200, 50) button_3 = pygame.Rect(700, 600, 200, 50) #setting parameters for buttons button_4 = pygame.Rect(150, 695, 365, 50) button_5 = pygame.Rect(540, 695, 250, 50) mx, my = pygame.mouse.get_pos() screen.fill(white) # rectangles for buttons pygame.draw.rect(screen, (purp), button_1) pygame.draw.rect(screen, (purp), button_2) pygame.draw.rect(screen, (purp), button_3) # putting the buttons on screen pygame.draw.rect(screen, (purp), button_4) pygame.draw.rect(screen, (purp), button_5) text1 = TITLE_FONT.render("CLICKITY CLICK CLICK", 1, black) screen.blit(text1, (int(width / 2 - (text1.get_width() / 2)), 100)) text2 = INS_FONT.render("HOW TO PLAY:", 1, black) screen.blit(text2, (int(width / 2 - text2.get_width() / 2), 190)) text2_0 = INS_FONT.render( "Click the screen as many times as possible!", 1, black) screen.blit(text2_0, (int(width / 2 - text2_0.get_width() / 2), 240)) text2_1 = INS_FONT.render("The amount of time you have to click", 1, black) screen.blit(text2_1, (int(width / 2 - text2_1.get_width() / 2), 290)) text2_2 = INS_FONT.render("is determined by the level you pick.", 1, black) screen.blit(text2_2, (int(width / 2 - text2_2.get_width() / 2), 340)) text2_3 = INS_FONT.render( "The higher the difficulty, the less time you have.", 1, black) # CREATING THE MENU AND INSTRUCTIONS screen.blit(text2_3, (int(width / 2 - text2_3.get_width() / 2), 390)) text2_4 = INS_FONT.render( 'The score records are kept on the file, "SCORE_RECORDS_CLICK".', 1, black) screen.blit(text2_4, (int(width / 2 - text2_4.get_width() / 2), 440)) text2_5 = INS_FONT.render('[Auto-Clickers are NOT permitted]', 1, black) screen.blit(text2_5, (int(width / 2 - text2_5.get_width() / 2), 490)) textWarn = LETTER_FONT.render('**SEIZURE WARNING - FLASHING COLORS**', 1, (255, 0, 0)) screen.blit(textWarn, (int(width / 2 - textWarn.get_width() / 2), 550)) text3 = WORD_FONT.render("EASY", 1, black) screen.blit(text3, (int(width / 5 - text3.get_width() / 2), 605)) text4 = WORD_FONT.render("MEDIUM", 1, black) screen.blit( text4, (int(width / 2 - text4.get_width() / 2), 605)) # button text text5 = WORD_FONT.render("HARD", 1, black) screen.blit(text5, (int(width / 1.25 - text5.get_width() / 2), 605)) text6 = WORD_FONT.render("LEADERBOARD", 1, black) screen.blit(text6, (int(width / 3 - text6.get_width() / 2), 700)) text7 = WORD_FONT.render("[ESC] EXIT", 1, black) screen.blit(text7, (int(width / 1.5 - text7.get_width() / 2), 700)) cursorpic = pygame.image.load('clicker\cursor.png') screen.blit(cursorpic, (725, 120)) leftlook = pygame.image.load('clicker\ok.png') #adding pics to menu screen.blit(leftlook, (100, 280)) rightlook = pygame.image.load('clicker\chad.png') screen.blit(rightlook, (730, 280)) top = pygame.image.load('clicker\shrek.png') screen.blit(top, (int(width / 2 - top.get_width() / 2), 25)) by = INS_FONT.render('A GAME BY KYRA CRAWFORD', 1, black) #name and logo image on bottom left corner screen.blit(by, (65, 780)) bottom = pygame.image.load('clicker\dog2.png') screen.blit(bottom, (0, 750)) pygame.display.update() event = pygame.event.poll() # code behind clicking the buttons #checking if the left mouse is clicked if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: click = True if button_1.collidepoint((mx, my)): if click: screen.fill(black) pygame.display.update() menu = False mainEasy() #starts the game with the presets of easy mode pass if button_2.collidepoint((mx, my)): if click: screen.fill(black) pygame.display.update() menu = False mainMedium() #starts the game with the presets of med mode pass if button_3.collidepoint((mx, my)): if click: screen.fill(black) pygame.display.update() menu = False mainHard() #starts the game with the presets of hard mode pass if button_4.collidepoint((mx, my)): if click: pygame.quit() #exit the program leaderboard() pass if button_5.collidepoint((mx, my)): if click: pygame.quit() keyBoardKey = pygame.key.get_pressed() if keyBoardKey[pygame.K_ESCAPE]: pygame.quit() #key event for exiting the game if event.type == pygame.QUIT: run = False sys.exit() click = False #closing clikc loop
timeleft = timeleft result = 0 while True: if result == 0: timestart = int(time.time()) show_time(timeleft) if result >= 0 and timeleft > 0: timeleft = (int(leveltime)) - (int(time.time()) - timestart) show_time(timeleft) elif result >= 0 and timeleft <= 0: show_result('You got this many clicks: ' + str(result) + '. GOOD JOB!') nice = pygame.image.load('clicker/nicework.png') screen.blit(nice, (int(width / 2 - nice.get_width() / 2), 270)) pygame.display.update() time.sleep(3) SCORE(result) mouse.set_pos(100, 100) #set mouse position to avoid click error run = False MENU(True) break event = pygame.event.poll() if event.type == pygame.QUIT: run = False sys.exit() elif pygame.mouse.get_pressed( )[0] and event.type == pygame.MOUSEBUTTONDOWN: result += 1 show_result(result) run = False
def mouseLookOff(self): self.root.capture_mouse(None) if self.startingMousePosition: mouse.set_pos(*self.startingMousePosition) self.startingMousePosition = None
elif triangle.collidepoint(mouse_pos): grab = triangle other = rhombus Y_triangle = triangle.project((0, 1)) Y_rhombus = rhombus.project((0, 1)) draw.line(SCREEN, (255, 0, 0), (2, Y_triangle[0]), (2, Y_triangle[1]), 2) draw.line(SCREEN, (0, 0, 255), (7, Y_rhombus[0]), (7, Y_rhombus[1]), 2) X_triangle = triangle.project((1, 0)) X_rhombus = rhombus.project((1, 0)) draw.line(SCREEN, (255, 0, 0), (X_triangle[0], 2), (X_triangle[1], 2), 2) draw.line(SCREEN, (0, 0, 255), (X_rhombus[0], 7), (X_rhombus[1], 7), 2) draw.circle(SCREEN, (255, 255, 255), triangle.C.astype(int), 3) draw.circle(SCREEN, (255, 255, 255), rhombus.C.astype(int), 3) if grab: olap_axis = grab.collidepoly(other) if not olap_axis is False: MTD = -min(dict((_dot(a, a), a) for a in olap_axis).items())[1] MTD = [round_away(x) for x in MTD] grab.move_ip(*MTD) mouse.set_pos(grab.C) else: grab.C = mouse_pos display.update()
def button_resume_click(self): mouse.set_pos((self.master.surface.get_size()[0], 0)) self.game_state.pause = False pygame_widgets.delayed_call(self.button_resume.set, appearance='normal') self.disconnect()
def init(cls): cls.blocks = LayeredUpdates() mouse.set_pos((320, 240)) display.set_mode((640, 480)) cls.rect_selection = Rect(0, 0, 0, 0) os.environ['SDL_VIDEO_CENTERED'] = "{!s},{!s}".format(0, 0)
def go(): # START # Fairly obvious, initialize the pygame library pygame.init() # Create the world in which all physical things reside. It is our coordinate system and universe. world = World(vector((20.0, 20.0, 2000.0)), vector((-10.0, 0.0, 10.0))) # Create startup cameras and make the world visible to them eyecam = Camera() eyecam.registerObject(world) scopecam = Camera() scopecam.registerObject(world) # Setup the program window (just a special case of a View) screen = Screen(eyecam, vector((1000, 800, 0)), -1.0) screen.display = pygame.display.set_mode((1000, 800)) # Setup the display surfaces to be drawn to screen and list them main_dim = vector((1000, 800, 0.0)) scope_dim = vector((240, 240, 0.0)) mainview = View(eyecam, main_dim, zero_vector, 1.0) scopeview = Scope(scopecam, scope_dim, (main_dim - scope_dim) / 2.0, 4.0) views = [mainview, scopeview] # Hide the cursor mouse.set_visible(False) # Let's set up some useful variables now = pygame.time.get_ticks() # The current program time shot_fired = False # chase_bullet = False hide_scope = True lock_mouse = False power = 100.0 #mainview.reposition(eyecam) scopeview.setPower(power) timescale = 1.0 tot_mag = 0.0 tot_dur = 0.0 frames = 0 if lock_mouse: mouse.set_pos((scope_dim / 2.0).xy()) while 1: last_t = now now = pygame.time.get_ticks() t_length = float(now - last_t) if world.has_hit: shot_end = now #break if world.bullet.position.z() > world.target.position.z() + 1000.0: #world.bullet.position = vector((world.bullet.position.x(), world.bullet.position.y(), world.target.position.z() - 0.01)) shot_end = now break #pass for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if event.dict['button'] == 4: power += 1.0 if event.dict['button'] == 5: power -= 1.0 power = scopeview.setPower(power) print power if event.type == pygame.QUIT: sys.exit() sx, sy = (0.5 * mainview.dimensions).xy() if lock_mouse: mx, my = mouse.get_rel() mouse.set_pos(sx, sy) r = (1.0 / (10000 * power)) * (vector((sx - mx, sy - my, 0))) scopecam.rotation += r eyecam.rotation += r else: mx, my = mouse.get_pos() r = (1.0 / (1000 * power)) * (vector((sx - mx, sy - my, 0))) scopecam.rotation = r eyecam.rotation = r world.bullet.rotation = scopecam.rotation scopeview.reposition(scopeview.camera) mainview.reposition(mainview.camera) b1, b2, b3 = mouse.get_pressed() if b1 and not shot_fired: shot_fired = True shot_start = now world.bullet.velocity = rotate(vector( (0.0, 0.0, 800.0)), (-world.bullet.rotation - scopeview.tweaks)) print 'Start Velocity: ' + str(world.bullet.velocity.magnitude()) #mainview.camera.rotation = zero_vector mainview.setPower(50.0) #scopeview.reposition(view.camera) #scopeview.overlay = [] # hide scope world.movers += [world.bullet] # Move everything. The world does most of the work once we tell it to, # we just need to handle the less tangible stuff. world.moveObjects(timescale * (t_length / 1000.0)) # Bullet chasing if shot_fired: #scopecam.rotation = world.bullet.rotation if hide_scope: scopeview.visible = False bp = world.bullet.position lbp = world.bullet.last_position diff = bp - lbp mag = diff.magnitude() magxz = sqrt(diff.x()**2 + diff.z()**2) frames += 1 tot_mag += mag tot_dur += t_length * timescale if chase_bullet and mag > 0.0: h = arctan2(diff.x(), diff.z()) e = arcsin(diff.y() / mag) eyecam.rotation = vector((h, -e, 0)) eyecam.position = bp & vector((1, 1, 1)) mainview.setPower(10.0) mainview.reposition(mainview.camera) #print eyecam.position #scopeview.reposition(eyecam) # Grab each view's camera image, then draw them to the program window in sequence for view in views: view.updateDisplay() view.draw(screen.display) # fps = 1000.0 / (t_length + (1 if t_length == 0 else 0)) screen.drawOverlay((fps, )) pygame.display.flip() for view in views: view.updateDisplay() view.draw(screen.display, (screen.dimensions - view.dimensions) / 2.0) pygame.display.flip() print "Flight time: " + str(tot_dur / 1000.0) + " s" print "Impact energy: " + str( .5 * world.bullet.mass * world.bullet.velocity.magnitude()**2) + " J" print 'End Position' print world.bullet.position print 'End Velocity: ' + str(world.bullet.velocity.magnitude()) print "Average frame time: " + str((tot_dur / 1000.0) / float(frames)) print "Average FPS: " + str(float(frames) / tot_dur * 1000) print "Average distance per frame: " + str(tot_mag / float(frames)) pygame.time.delay(1000) pygame.display.quit() pygame.quit()