From 625640a2781db76b3b8e5053d307ef80993646da Mon Sep 17 00:00:00 2001 From: Jamie Boisture Date: Fri, 24 Jul 2009 22:28:49 +0000 Subject: added a robots example and a bounching ball example to data/graphics --- diff --git a/data/graphics/bouncing b/data/graphics/bouncing new file mode 100644 index 0000000..b14afc7 --- /dev/null +++ b/data/graphics/bouncing @@ -0,0 +1,37 @@ +from pippy.gasp import * # import everything from pippy.gasp +import math # import math + +class Mover: + def __init__(self): + self.speed = random_between(1,3) + self.direction = random_between(10,360) + self.rad = random_between(10,20) + self.color = (random_between(0,255),random_between(0,255),random_between(0,255)) + self.x = random_between(self.rad,screen_size()[0]-self.rad) + self.y = random_between(self.rad,screen_size()[1]-self.rad) + self.shape = Circle((self.x,self.y),self.rad,color = self.color,filled = True) + self.move_x = math.cos(math.degrees(self.direction))*self.speed + self.move_y = math.sin(math.degrees(self.direction))*self.speed + + def move(self): + self.x += self.move_x + self.y += self.move_y + self.keep_to_screen() + move_to(self.shape,(self.x,self.y)) + + def keep_to_screen(self): + if self.x < self.rad or self.x > screen_size()[0]-self.rad: + self.move_x = self.move_x * -1 + if self.y < self.rad or self.y > screen_size()[1]-self.rad: + self.move_y = self.move_y * -1 + +num_movers = random_between(10,30) +movers = [] +begin_graphics() +for i in range(num_movers): + movers.append(Mover()) +set_speed(40) +while "enter" not in keys_pressed(): + for mover in movers: mover.move() + update_when("next_tick") +end_graphics() diff --git a/data/graphics/robots b/data/graphics/robots new file mode 100644 index 0000000..71ca553 --- /dev/null +++ b/data/graphics/robots @@ -0,0 +1,141 @@ +from pippy.gasp import * #import everything from pippy.gasp + +class Robot: + #A class used to store information about a robot + pass + + +class Player: + #A class used to store information about a player + pass + +def show_level(level_num): + # shows what level is about to start + level_text = Text("Level "+str(level_num),(screen_size()[0]/2-50,screen_size()[1]/2),size = 30) + sleep(2) + remove_from_screen(level_text) + + +def place_player(robots): + #pick a point of the screen of the player + player = Player + player.x = random_between(0,screen_size()[0]/10-1) + player.y = random_between(0,screen_size()[1]/10-1) + #make sure the player is not placed directly on top of a robot + while player_caught(player,robots): + player.x = random_between(0,screen_size()[0]/10-1) + player.y = random_between(0,screen_size()[1]/10-1) + #put the player on the screen by creating a circle + player.shape = Circle((10*player.x+5, 10*player.y+5), 5, filled=1) + return player + +def move_player(player,robots): + #handles the movement of the player + key = update_when("key_pressed") #waits untill a key is pressed and then returns that key as a string + if "t" == key: + #if "t" is pressed the player teleports + remove_from_screen(player.shape) + player = place_player(robots) + #change the players x and y location + if "q" == key: + if player.x > 0 and player.y < screen_size()[1]/10-1: + player.x -= 1 + player.y += 1 + if "w" == key: + if player.y < screen_size()[1]/10-1: + player.y += 1 + if "e" == key: + if player.x < screen_size()[0]/10-1 and player.y < screen_size()[1]/10-1: + player.x += 1 + player.y += 1 + if "d" == key: + if player.x < screen_size()[0]/10-1: + player.x += 1 + if "c" == key: + if player.x < screen_size()[0]/10-1 and player.y > 0: + player.x += 1 + player.y -= 1 + if "x" == key: + if player.y > 0: + player.y -= 1 + if "z" == key: + if player.x > 0 and player.y > 0: + player.x -= 1 + player.y -= 1 + if "a" == key: + if player.x > 0: + player.x -= 1 + # move the players shape on screen to the new location + move_to(player.shape,(10*player.x+5,10*player.y+5)) + return player + +def place_robots(num_robots): + #create a certain number of robots defined by num_robots + robots = [] + for i in range(num_robots): + robot = Robot() + robot.x = random_between(0,screen_size()[0]/10-1) + robot.y = random_between(0,screen_size()[1]/10-1) + robot.junk = False + robot.shape = Box((10*robot.x,10*robot.y),10,10) + robots.append(robot) + return robots + +def move_robots(robots, player): + # move all the robots towards the player + for robot in robots: + if not robot.junk: + if robot.x > player.x: + robot.x -= 1 + if robot.x < player.x: + robot.x += 1 + if robot.y > player.y: + robot.y -= 1 + if robot.y < player.y: + robot.y += 1 + move_to(robot.shape,(10*robot.x,10*robot.y)) + +def player_caught(player, robots): + #check if the player is caught + for robot in robots: + if robot.x == player.x and robot.y == player.y: + return True + return False + +def robot_crashed(robots): + #check if any of the robots have crashed into each other + for robot1 in robots: + for robot2 in robots: + if robot1.x == robot2.x and robot1.y == robot2.y and robot1!=robot2: + if not robot1.junk: + robot1.junk = True + remove_from_screen(robot1.shape) + robot1.shape = Box((10*robot1.x,10*robot1.y),10,10,filled=True) + +def alive_robots(robots): + # returns True if all the robots are dead + for robot in robots: + if robot.junk == False: return False + return True + + +begin_graphics() #So that you can draw things +level = 1 #the starting level +robot_num = 5 #the number of robots to place on screen +dead = False #set dead to False +while not dead: #run while the player is not dead + clear_screen() #clear the screen at the start of each level + show_level(level) #show what level is about to start + robots = place_robots(robot_num) #create the robots + player = place_player(robots) #create the player + level_complete = False + while not level_complete and not dead: #run this level while the player is not dead and the level is not complete + player = move_player(player, robots) #move the player + move_robots(robots, player) #move all of the robots + dead = player_caught(player,robots) #check if the player had been caught + robot_crashed(robots) #check if any of the robots have crashed + level_complete = alive_robots(robots) # check if there are any robots alive + robot_num += 3 #increase the number of robots for the next level + level+= 1 #increase the number level number by 1 +print "you died on level " + str(level) +end_graphics() # Finished! -- cgit v0.9.1