Jump to content

? servers

? players online

kafka

Members
  • Posts

    9
  • Joined

About kafka

kafka's Achievements

  1. I agree that pygame is a bit overkill, but there are built in functions and quite a bit more tutorials on simple games -- turtle is mainly used for graphics, but the OP says it's a school project so I don't think they can use anything else. I think ASCII pong would be much more of a challenge, especially for a beginner. The play area is defined with Screen().setup(500, 500) and the size can be retrieved with turtle.screensize(). I agree that a player class is a good idea, and getting the procedural logic out of the main loop is a good idea, but you're not going to be able to change the complex conditional statements.
  2. Necroing this thread since it's one of the only actual programming threa I'll start with a little advice before getting into the code. I'm not sure I would start with turtle for a pong game. Perhaps as a pong animation, but controls and collision are going to be expensive and not very practical. If you're interested in applying coding to games, I recommend taking a look at pygame(probably one of the largest python game libraries). If you cannot (this looks like a school project to me) use external libraries take a look at tkinter, python's defacto GUI package (turtle is actually built with tkinter). >And to have a border for the paddles to stay in since they can keep moving off the screen to the end of the world. To bind the paddles to the box check for the position of player1 and player2's bounding box with player1.rect and check against the screen's rectangle by calling get_rect. Then you can setup some logic to check for the paddle's location against the bounding area and change the dx/dy velocity. I would probably wrap the player in a class, but this'll do. def checkBoundingRects(player, screen): if player.right > screen.right or player.left or player.bottom > screen.bottom or player.top return False return True >trying to get the ball and the paddles to collide If we're clever we may be able to fix this issue by modifying the code above. For the first question (bounding box) we're checking "are we inside this box", this next question is also "are we inside the box". The function above can be refactored and applied to this question as well. def checkCollision(actor, bounding): if actor.right > bounding.right or actor.left or actor.bottom > bounding.bottom or actor.top return False return True Now we can call the function for checking the bounding area: touching = checkCollision(player1, screen.get_rect()) If touching is true we know the player is touching the bounding box OR outside of it already. collision = not checkCollision(ball, player1) *We would also need to do this with player2 and possibly the bounding rectangle. If collision is set, we know the ball is either inside the paddle, or at least touching it. The signs in my logic may be a little flawed here as I wrote some quick unit tests to help me visualize it, but didn't boot up tkinter. Hope you did well on this project!
×
×
  • Create New...