SHARE
TWEET

Untitled

a guest Apr 25th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. CSC 119 Intro to Programming Final Project
  3. Author: Marco Madera
  4. Created on: 4/21/2019 5:12PM
  5. '''
  6.  
  7. # imports
  8. import turtle
  9. import time
  10. import random
  11.  
  12.  
  13.  
  14. # screen setup
  15. scrn = turtle.Screen()
  16. scrn.title("Versus! by Marco Madera")
  17. scrn.bgcolor("gray")  # screen's background color
  18. scrn.setup(width=800, height=500)  # screen's dimensions
  19. scrn.tracer(0)  # disables screen's updates, allows for faster output
  20.  
  21. # players
  22. class OnePlayer(turtle.Turtle):
  23.     def __init__(self, color):
  24.         super().__init__(shape="triangle")
  25.         self.speed(0)  #fastest animation speed
  26.         self.color(color)
  27.         self.penup()
  28.         self.goto(200,0)
  29.         self.direction = "stop"
  30.         self._delay(0)
  31.  
  32.  
  33.     def move(self):
  34.     # move function allows for object to move accordingly
  35.         if self.direction == "up":
  36.             self.setheading(90)
  37.             y = self.ycor()
  38.             self.sety(y + 15)
  39.         elif self.direction == "down":
  40.             self.setheading(270)
  41.             y = self.ycor()
  42.             self.sety(y - 15)
  43.         elif self.direction == "right":
  44.             self.setheading(0)
  45.             x = self.xcor()
  46.             self.setx(x + 15)
  47.         elif self.direction == "left":
  48.             self.setheading(180)
  49.             x = self.xcor()
  50.             self.setx(x - 15)
  51.  
  52.     # functions for the players' movement
  53.     def OneUp():
  54.         player1.direction = "up"
  55.  
  56.     def OneDown():
  57.         player1.direction = "down"
  58.  
  59.     def OneRight():
  60.         player1.direction = "right"
  61.  
  62.     def OneLeft():
  63.         player1.direction = "left"
  64.  
  65.     scrn.listen()  # allows screen to listen for keyboard keys
  66.     # key pairings for player 1
  67.     scrn.onkeypress(OneUp, "i")
  68.     scrn.onkeypress(OneDown, "k")
  69.     scrn.onkeypress(OneRight, "l")
  70.     scrn.onkeypress(OneLeft, "j")
  71.  
  72.     # screen border collision will cause the game to stop and that player to be sent to the middle
  73.     def Game_Over(self):
  74.         if self.xcor() < -390 or self.xcor() > 390 \
  75.             or self.ycor() < -240 or self.ycor() > 240:
  76.             time.sleep(2)
  77.             self.goto(200, 0)
  78.             self.direction = "stop"
  79.  
  80. class TwoPlayer(OnePlayer):
  81.     def __init__(self, color):
  82.         super().__init__(color=color)
  83.         self.goto(-200, 0)
  84.  
  85.     # functions for the players' movements
  86.     def TwoUp():
  87.         player2.direction = "up"
  88.  
  89.     def TwoDown():
  90.         player2.direction = "down"
  91.  
  92.     def TwoRight():
  93.         player2.direction = "right"
  94.  
  95.     def TwoLeft():
  96.         player2.direction = "left"
  97.  
  98.     # pairs a keyboard key to a certain action using movement functions
  99.     scrn.listen()  # allows screen to listen for keyboard keys
  100.     #key pairings for player 2
  101.     scrn.onkeypress(TwoUp, "w")
  102.     scrn.onkeypress(TwoDown, "s")
  103.     scrn.onkeypress(TwoRight, "d")
  104.     scrn.onkeypress(TwoLeft, "a")
  105.  
  106.     def Game_Over(self):
  107.         if self.xcor() < -390 or self.xcor() > 390 \
  108.             or self.ycor() < -240 or self.ycor() > 240:
  109.             time.sleep(2)
  110.             self.goto(-200, 0)
  111.             self.direction = "stop"
  112.  
  113.  
  114. # reward and penalty
  115. class Reward(turtle.Turtle):
  116.     def __init__(self):
  117.         super().__init__()
  118.         self.penup()
  119.         self.shape("circle")
  120.         self.color("green")
  121.         self.goto(100, 200)
  122.  
  123. class Penalty(Reward):
  124.     def __init__(self):
  125.         super().__init__()
  126.         self.color("red")
  127.         self.goto(-200, -100)
  128.  
  129.  
  130. # instantiating players
  131. player1 = OnePlayer("black")
  132. player2 = TwoPlayer("white")
  133.  
  134. # instantiating food (power up and penalty)
  135. reward = Reward()
  136. penalty = Penalty()
  137.  
  138.  
  139.  
  140. # turtle objects' size is 20px, so if distance to pwr/penalty
  141. # is below that, the pwr/penalty will move (touched)
  142. def oneScoring(p1_score_obj, score):
  143.    
  144.     p1_score_obj.clear()
  145.  
  146.     p1_score_obj.speed(0)
  147.     p1_score_obj.shape("circle")
  148.     p1_score_obj.color("black")
  149.     p1_score_obj.penup()
  150.     p1_score_obj.hideturtle()
  151.     p1_score_obj.goto(300, 200)
  152.     p1_score_obj.write("Player 1: 0", align = "center", font = ("Arial", 24, "normal"))
  153.  
  154.  
  155.     if player1.distance(reward) < 20:
  156.         reward.goto(random.randint(-390, 390), random.randint(-240, 240))
  157.         score += 1
  158.         p1_score_obj.write("Player 1: {}".format(score), \
  159.             align = "center", font = ("Arial", 24, "normal"))
  160.  
  161.     elif player1.distance(penalty) < 20:
  162.         penalty.goto(random.randint(-390, 390), random.randint(-200, 240))
  163.         score -= 1
  164.         p1_score_obj.write("Player 1: {}".format(score), \
  165.             align = "center", font = ("Arial", 24, "normal"))
  166.  
  167. def twoScoring(p2_score_obj, score):
  168.  
  169.     p2_score_obj.clear()
  170.  
  171.     #p2_score_obj.speed(0)
  172.     #p2_score_obj.shape("circle")
  173.     p2_score_obj.color("white")
  174.     p2_score_obj.penup()
  175.     p2_score_obj.hideturtle()
  176.     p2_score_obj.goto(-300, 200)
  177.     p2_score_obj.write("Player 2: {}".format(score), align = "center", font = ("Arial", 24, "normal"))
  178.  
  179.    
  180.     if player2.distance(reward) < 20:
  181.         reward.goto(random.randint(-390, 390), random.randint(-240, 240))
  182.         score += 1
  183.         p2_score_obj.write("Player 2: {}".format(score), \
  184.             align = "center", font = ("Arial", 24, "normal"))
  185.  
  186.     if player2.distance(penalty) < 20:
  187.         penalty.goto(random.randint(-390, 390), random.randint(-200, 240))
  188.         score -= 1
  189.         p2_score_obj.write("Player 2: {}".format(score), \
  190.             align = "center", font = ("Arial", 24, "normal"))
  191.    
  192.  
  193.  
  194.  
  195. def main():
  196.  
  197.     # constants
  198.     delay = 0.05
  199.    
  200.     # initial scores
  201.     p1_score = 0
  202.     p2_score = 0
  203.  
  204.     while True:
  205.         player1.move()
  206.         player2.move()
  207.         #move(player1)
  208.         #move(player2)
  209.  
  210.         p1_score_turtle = turtle.Turtle()
  211.         p2_score_turtle = turtle.Turtle()
  212.         oneScoring(p1_score_turtle, p1_score)
  213.         twoScoring(p2_score_turtle, p2_score)
  214.        
  215.  
  216.         player1.Game_Over()
  217.         player2.Game_Over()
  218.         #Game_Over(player1)
  219.         #Game_Over(player2)
  220.  
  221.         time.sleep(.01)
  222.  
  223.         scrn.update()
  224.  
  225.     scrn.mainloop()
  226.  
  227. if __name__ == "__main__":
  228.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top