def oneSecTimer(timer): if (pygame.time.get_ticks() >= timer + 1000): timer = pygame.time.get_ticks() if (len(globals.OTTO) < MAX_OTTOS): if (globals.OTTOTIMER > 0): globals.OTTOTIMER -= 1 else: globals.OTTOTIMER = otto.ottoTimerReload #make otto otto.Class_Otto() sounds.playSound(sounds.ottoAliveSound) return timer
def collide(self, victim): if victim not in globals.PLAYER.sprites(): if (globals.SOUNDS_ON == True): #depending on victim play diff sound if victim in globals.BULLETS.sprites(): sounds.playSound(sounds.robotShotSound) for a in globals.PLAYER.sprites(): if victim.shooter_obj == a: globals.SCORE += 1 else: sounds.playSound(sounds.robotExplodeSound) if globals.android: globals.android.vibrate(2) self.killState = True
def startNewLevel(): sounds.playSound(sounds.nextLevelSound) globals.OTTOTIMER = otto.ottoTimerReload globals.LEVEL += 1 # make level wrap back to zero if globals.LEVEL > maxLevels: globals.LEVEL = 0 #destroy all the objects for a in globals.OBJECTS: a.kill() #start a new level pList = misc.distPoints(globals.SCREENSIZE, globals.NUM_OF_ROBOTS / 4, 4) player.Class_Player(player.player_start_pos, [0,0]) for a in range(globals.NUM_OF_ROBOTS): robots.Class_Robot(pList[a], [0,0]) #level up robots for a in globals.ROBOTS.sprites(): a.levelUp(globals.LEVEL) maze.Class_Maze(globals.SCREENSIZE)
def __init__(self, src_obj, speed, direction, \ list_colors = bullet_color_list_pts, \ size = bullet_pixel_size,\ list_polygon_pts = bullet_polygon_list_pts,\ groups = []): self.blitImage = None if (direction == movement.dirEnum.NONE): #if a direction is not indicated dont fire # kill yourself self.killState = True return self.size = copy.deepcopy(size) #needed deepcopy here... guess if its more than one deep have to deepcopy self.list_polygon_pts = copy.deepcopy(list_polygon_pts) self.list_colors = copy.deepcopy(list_colors) # keep a reference to obj shooter so when bullet is done # we can indicate bullet has been removed self.shooter_obj = src_obj # place bullet with direction vector from source object center # if the direction is purely left or right then dont modify y # make x = 1/2 src obj x + 1/2 bullet width # if the direction is purely up or down then make x = 1/4 of src obj # width, make y = 1/2 src obj height + 1/2 bullet height # if the direction is diagonal then make y = 1/4 src obj height + # plus 1/2 bullet width # make x = 1/2 src obj width + 1/2 bullet width # Also: rotate sprite image depending on orientation # it defaults to up - so if direction is up or down # dont worry about it # Transformation equations: # Px' = Px * cos theta - Py * sin theta # Py' = Py * cos theta + Px * sin theta pos = copy.deepcopy(src_obj.pos) angle = 0.0 if (direction == movement.dirEnum.LEFT) | (direction == movement.dirEnum.RIGHT): angle = math.pi / 2.0 #90 deg to make it horizontal if (direction == movement.dirEnum.LEFT): sign = [-1.0, 1.0] else: sign = [1.0, 1.0] pos[0] += sign[0] * (0.5 * src_obj.size[0] + 0.5 * size[0]) elif (direction == movement.dirEnum.UP) | (direction == movement.dirEnum.DOWN): if (direction == movement.dirEnum.UP): sign = [1.0, -1.0] else: sign = [1.0, 1.0] pos[0] += sign[0] * (0.25 * src_obj.size[0]) pos[1] += sign[1] * (0.5 * src_obj.size[1] + 0.5 * size[1]) else: if (direction == movement.dirEnum.UPLEFT): sign = [-1.0, -1.0] angle = -math.pi / 4.0 elif (direction == movement.dirEnum.UPRIGHT): sign = [1.0, -1.0] angle = math.pi / 4.0 elif (direction == movement.dirEnum.DOWNLEFT): sign = [-1.0, 1.0] angle = math.pi / 4.0 elif (direction == movement.dirEnum.DOWNRIGHT): sign = [1.0, 1.0] angle = -math.pi / 4.0 pos[0] += sign[0] * (0.5 * src_obj.size[0] + 0.5 * size[0]) pos[1] += sign[1] * (0.25 * src_obj.size[1] + 0.5 * size[1]) misc.rotate(angle, self.list_polygon_pts) # put in groups object.Class_Obj.__init__( self, pos, speed, groups + [globals.BULLETS, globals.COLLIDABLE]) # movement direction never changes after creation - so no one should call # movement direction except for object itself self.updateMovement(direction) # play sound if src_obj in globals.ROBOTS: sounds.playSound(sounds.robotGunSound) else: sounds.playSound(sounds.playerGunSound)
def collide(self, victim): #indicate to shooter that bullet is gone self.shooter_obj.bullets -= 1 if victim in globals.BULLETS.sprites(): sounds.playSound(sounds.bulletClashSound) self.killState = True
def collide(self, victim): if (globals.SOUNDS_ON == True): sounds.playSound(sounds.playerDeathSound) self.killState = True
def main(): keybo = Class_ProcessKeybo() keybo.collisionOn = True flags = DOUBLEBUF bpp = 24 screen = pygame.display.set_mode(globals.SCREENSIZE, flags, bpp) walls.loadImages() screen.fill(globals.SCREEN_BACKCOLOR) pygame.display.set_caption("Pyzerk") clock = pygame.time.Clock() olddirtyrects = [] init = True genTickTimer = pygame.time.get_ticks() globals.OTTOTIMER = otto.ottoTimerReload # Map the back button to the escape key. if globals.android: globals.android.init() globals.android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE) #create a container for generic vars mainContainerC = Class_Container() #setup soundtrack if (globals.SOUNDS_ON == True): sounds.playSound(sounds.welcomeSound) sounds.playSound(sounds.robotWalkSound) mixer.Channel(sounds.SOUNDTRACK_CHAN).play(sounds.soundTrack0Sound, -1) #main loop while keybo.running == True: globals.FPS = 1000.0 / clock.tick(globals.FRAME_RATE_SETTING) if init == True: init = False #startNewLevel() #show menu instead of starting game text.Class_Text((0,0), "First Menu Item", globals.WHITE) # Android-specific: if globals.android: if globals.android.check_pause(): globals.android.wait_for_resume() #keyboard processing keybo.run(globals.SCREENSIZE, screen, globals.SCREEN_BACKCOLOR, mainContainerC) #update object movement updateMovement(mainContainerC) #look for collisions detCollisions(keybo) #tick off here once per second genTickTimer = oneSecTimer(genTickTimer) #update objects for a in globals.OBJECTS: a.update() #blank the screen screen.fill(globals.SCREEN_BACKCOLOR) dirtyrects = [] for a in globals.OBJECTS.sprites(): dirtyrects += a.draw(screen) #draw the text for a in globals.TEXT.sprites(): a.update(str(globals.SCORE)) dirtyrects += a.draw(screen) #update the display pass in the rectangles to update for each object pygame.display.update(dirtyrects + olddirtyrects) olddirtyrects = dirtyrects #check to see if all robots destroyed or player destroyed to go to next level if (globals.MENUON == False): if ((len(globals.PLAYER.sprites()) == 0) | (len(globals.ROBOTS.sprites()) == 0)): startNewLevel()
def mainLoop(socket): global led_status, auto_status, opencv_mode, findline_mode, speech_mode, \ auto_mode, command, ap_status, turn_status, blinkThreadStatus BUFSIZ = 1024 #Define buffer size while True: command = socket.recv(BUFSIZ).decode() print("mainLoop: received %s" % command) if not command: continue # strip end-of-cmd off if END_OF_CMD in command: command = command[:-len(END_OF_CMD)] # TODO: add code to handle receiving multiple commands delimited by end-of-cmd if SOUND in command: sounds.playSound(command[len(SOUND):]) elif SPEAK in command: speak.say(command[len(SPEAK):]) elif VOLUME in command: speak.changeVolume(command[len(VOLUME):]) elif RATE in command: speak.changeRate(command[len(RATE):]) elif SHAKE in command: servos.shakeHead() elif NOD in command: servos.nodHead() elif 'exit' in command: os.system("sudo shutdown -h now\n") elif 'quit' in command: print('shutting down') return elif 'spdset' in command: global spd_adj spd_adj = float((str(command))[7:]) #Speed Adjustment print("speed adjustment set to %s" % str(spd_adj)) elif 'scan' in command: dis_can = servos.scan() # Start Scanning str_list_1 = dis_can # Divide the list to make it samller to send str_index = ' ' # Separate the values by space str_send_1 = str_index.join(str_list_1) + ' ' socket.sendall((str(str_send_1)).encode()) # Send Data socket.send( 'finished'.encode() ) # Sending 'finished' tell the client to stop receiving the list of dis_can elif 'EC1set' in command: # pitch Adjustment newValue = int((str(command))[7:]) config.exportConfig('pitch_middle', newValue) servos.HEAD_PITCH_MIDDLE = newValue servos.headPitch(newValue) elif 'LDMset' in command: # look down max Adjustment newValue = int((str(command))[7:]) config.exportConfig('look_down_max', newValue) servos.HEAD_PITCH_DOWN_MAX = newValue servos.headPitch(newValue) elif 'LUMset' in command: # look up max Adjustment newValue = int((str(command))[7:]) config.exportConfig('look_up_max', newValue) servos.HEAD_PITCH_UP_MAX = newValue servos.headPitch(newValue) elif 'EC2set' in command: # yaw Adjustment newValue = int((str(command))[7:]) config.exportConfig('yaw_middle', newValue) servos.HEAD_YAW_MIDDLE = newValue servos.headYaw(newValue) elif 'LLMset' in command: # look left max Adjustment newValue = int((str(command))[7:]) config.exportConfig('look_left_max', newValue) servos.HEAD_YAW_LEFT_MAX = newValue servos.headYaw(newValue) elif 'LRMset' in command: # look right max Adjustment newValue = int((str(command))[7:]) config.exportConfig('look_right_max', newValue) servos.HEAD_YAW_RIGHT_MAX = newValue servos.headYaw(newValue) elif 'STEERINGset' in command: #Motor Steering center Adjustment newValue = int((str(command))[12:]) config.exportConfig('turn_middle', newValue) servos.STEERING_MIDDLE = newValue servos.steerMiddle() elif 'SLMset' in command: # Steering left max Adjustment newValue = int((str(command))[7:]) config.exportConfig('turn_left_max', newValue) servos.STEERING_LEFT_MAX = newValue servos.steerFullLeft() elif 'SRMset' in command: # Steering right max Adjustment newValue = int((str(command))[7:]) config.exportConfig('turn_right_max', newValue) servos.STEERING_RIGHT_MAX = newValue servos.steerFullRight() elif 'EM1set' in command: #Motor A Speed Adjustment newValue = int((str(command))[7:]) config.exportConfig('E_M1', newValue) elif 'EM2set' in command: #Motor B Speed Adjustment newValue = int((str(command))[7:]) config.exportConfig('E_M2', newValue) elif 'stop' in command: #When server receive "stop" from client,car stops moving socket.send('9'.encode()) motor.motorStop() #setup() if led_status == 0: headlights.turn(headlights.BOTH, headlights.OFF) colorWipe(ledStrip, rpi_ws281x.Color(0, 0, 0)) elif 'lightsON' in command: #Turn on the LEDs headlights.turn(headlights.BOTH, headlights.WHITE) led_status = 1 socket.send('lightsON'.encode()) elif 'lightsOFF' in command: #Turn off the LEDs headlights.turn(headlights.BOTH, headlights.OFF) led_status = 0 socket.send('lightsOFF'.encode()) elif 'SteerLeft' in command: #Turn more to the left headlights.turn(headlights.RIGHT, headlights.OFF) headlights.turn(headlights.LEFT, headlights.YELLOW) servos.turnSteering(servos.LEFT) turn_status = LEFT_TURN elif 'SteerRight' in command: #Turn more to the Right headlights.turn(headlights.LEFT, headlights.OFF) headlights.turn(headlights.RIGHT, headlights.YELLOW) servos.turnSteering(servos.RIGHT) turn_status = RIGHT_TURN elif 'middle' in command: #Go straight headlights.turn(headlights.BOTH, headlights.BLUE) turn_status = NO_TURN servos.steerMiddle() elif 'Left' in command: #Turn hard left headlights.turn(headlights.RIGHT, headlights.OFF) headlights.turn(headlights.LEFT, headlights.YELLOW) servos.steerFullLeft() turn_status = LEFT_TURN socket.send('3'.encode()) elif 'Right' in command: #Turn hard right headlights.turn(headlights.LEFT, headlights.OFF) headlights.turn(headlights.RIGHT, headlights.YELLOW) servos.steerFullRight() turn_status = RIGHT_TURN socket.send('4'.encode()) elif 'backward' in command: #When server receive "backward" from client,car moves backward socket.send('2'.encode()) motor.move(motor.BACKWARD, right_spd * spd_adj) elif 'forward' in command: #When server receive "forward" from client,car moves forward socket.send('1'.encode()) motor.move(motor.FORWARD, right_spd * spd_adj) elif 'l_up' in command: #Camera look up servos.changePitch(servos.UP) socket.send('5'.encode()) elif 'l_do' in command: #Camera look down servos.changePitch(servos.DOWN) socket.send('6'.encode()) elif 'l_le' in command: #Camera look left servos.changeYaw(servos.LEFT) socket.send('7'.encode()) elif 'l_ri' in command: #Camera look right servos.changeYaw(servos.RIGHT) socket.send('8'.encode()) elif 'ahead' in command: #Camera look ahead servos.lookAhead() elif 'Off' in command: #When server receive "Off" from client, Auto Mode switches off opencv_mode = 0 findline_mode = 0 speech_mode = 0 auto_mode = 0 auto_status = 0 time.sleep(.3) # wait for threads to detect the off state dis_scan = 1 socket.send('auto_status_off'.encode()) motor.motorStop() headlights.turn(headlights.BOTH, headlights.OFF) servos.steerMiddle() turn_status = NO_TURN elif 'auto' in command: #When server receive "auto" from client,start Auto Mode if auto_status == 0: socket.send('0'.encode()) auto_status = 1 auto_mode = 1 dis_scan = 0 elif 'opencv' in command: #When server receive "auto" from client,start Auto Mode if auto_status == 0: auto_status = 1 opencv_mode = 1 socket.send('oncvon'.encode()) elif 'findline' in command: #Find line mode start if auto_status == 0: socket.send('findline'.encode()) auto_status = 1 findline_mode = 1 elif 'voice_3' in command: #Speech recognition mode start if auto_status == 0: auto_status = 1 speech_mode = 1 socket.send('voice_3'.encode())