0

I'm working on making a modified version of pong for my college class and am having some trouble passing some methods. I believe I have everything set up the way it needs to to be able to do some parameter passing. The code runs fine and stops working just after the while loop in my main function. Any help is appreciated.

    import pygame

    SCR_WID, SCR_HEI = 640, 480
    class Player():
            def __init__(self):
                    self.x, self.y = 16, SCR_HEI/2
                    self.x1, self.y1 = SCR_WID-16, SCR_HEI/2
                    self.speed = 3
                    self.padWid, self.padHei = 8, 64
                    self.score = 0
                    self.scoreFont = pygame.font.Font("imagine_font.ttf", 64)

            def scoring(self):
                    scoreBlit = self.scoreFont.render(str(self.score), 1, (0,0,0))
                    screen.blit(scoreBlit, (32, 16))
                    if self.score == 10:
                            print ("player 1 wins!")
                            exit()

            def scoring1(self):
                    scoreBlit = self.scoreFont.render(str(self.score), 1, (0,0,0))
                    screen.blit(scoreBlit, (SCR_HEI+92, 16))
                    if self.score == 10:
                            print ("Player 2 wins!")
                            exit()

            def movement(self):                 
                    keys = pygame.key.get_pressed()
                    if keys[pygame.K_w]:
                            self.y -= self.speed
                    elif keys[pygame.K_s]:
                            self.y += self.speed

                    if self.y <= 0:
                            self.y = 0
                    elif self.y >= SCR_HEI-64:
                            self.y = SCR_HEI-64

                    keys1 = pygame.key.get_pressed()
                    if keys1[pygame.K_UP]:
                            self.y1 -= self.speed
                    elif keys1[pygame.K_DOWN]:
                            self.y1 += self.speed

                    if self.y1 <= 0:
                            self.y1 = 0
                    elif self.y >= SCR_HEI-64:
                            self.y1 = SCR_HEI-64

            def draw(self):
                    pygame.draw.rect(screen, (0,0,0), (self.x, self.y, self.padWid, self.padHei))
                    pygame.draw.rect(screen, (0,0,0), (self.x1, self.y1, self.padWid, self.padHei))



    class Ball():
            def __init__(self):
                    self.x, self.y = SCR_WID/2, SCR_HEI/2
                    self.x1, self.y1 = SCR_WID/2, SCR_HEI/2
                    self.speed_x = -3
                    self.speed_y = 3
                    self.size = 8

            def movement(self):
                    self.x += self.speed_x
                    self.y += self.speed_y

                    if self.y <= 0:
                            self.speed_y *= -1
                    elif self.y >= SCR_HEI-self.size:
                            self.speed_y *= -1

                    if self.x <= 0:
                            self.__init__()
                            enemy.score += 1
                    elif self.x >= SCR_WID-self.size:
                            self.__init__()
                            self.speed_x = 3
                            player.score += 1
                    for n in range(-self.size, player.padHei):
                            if self.y == player.y + n:
                                    if self.x <= player.x + player.padWid:
                                            self.speed_x *= -1
                                            break
                            n += 1
                    self.x1 += self.speed_x
                    self.y1 += self.speed_y

                    if self.y1 <= 0:
                            self.speed_y *= -1
                    elif self.y1 >= SCR_HEI-self.size:
                            self.speed_y *= -1

                    if self.x1 <= 0:
                            self.__init__()
                            enemy.score += 1
                    elif self.x1 >= SCR_WID-self.size:
                            self.__init__()
                            self.speed_x = 3
                            player.score += 1
                    for n in range(-self.size, enemy.padHei):
                            if self.y1 == enemy.y1 + n:
                                    if self.x1 >= enemy.x1 - enemy.padWid:
                                            self.speed_x *= -1
                                            break
                            n += 1

            def draw(self):
                    pygame.draw.rect(screen, (112,138,144), (self.x, self.y, 8, 8))

    SCR_WID, SCR_HEI = 640, 480
    screen = pygame.display.set_mode((SCR_WID, SCR_HEI))
    pygame.display.set_caption("Justin's pong")
    pygame.font.init()
    clock = pygame.time.Clock()
    FPS = 60
    player = Player() 
    ball = Ball()
    enemy = Player()

    def main():
        while True:
            for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                    print ("Game exited by user")
                                    exit()
        backgroundimage = pygame.image.load("background.png")
        ball.movement()
        player.movement()
        enemy.movement()
        position = (0,0)
        screen.fill((0, 0, 0))
        screen.blit(backgroundimage, position)
        ball.draw()
        player.draw()
        player.scoring()
        enemy.draw()
        enemy.scoring1()
        pygame.display.flip()
        clock.tick(FPS)

    main()
1
  • Dude - indentation! Use 4 spaces, no more, no less. Commented Apr 25, 2015 at 3:56

1 Answer 1

3

Nothing beyond your while loop will be executed until the loop as been completed.

i = 0
while i < 5:
    print(i)
    i += 1
print('Finished!')

Output:

0
1
2
3
4
Finished!
Sign up to request clarification or add additional context in comments.

4 Comments

Also his for loop has a similar problem (not a breaking problem but not doing what he thinks it is either): n += 1 in a for with a range does not alter for n in range:
I understand what you're saying by saying that I need to complete the loop, I'm just not sure I understand how to do that. Wouldn't I have to do something with False to counteract the True or is that way off?
I'm not familiar with the pygame module, but it seems that the while loop isn't even needed. @JustinFarr
@zachgates7 yes, with pygame you need a while True (to keep looping the game until it hits an end game situation). JustinFarr is just mistaken on a number of fundamental issues such as what should be wrapped in the while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.