简体   繁体   中英

Moving ball in Pong Game - Pygame

I'm new to Pygame, and I just started working on a Pong Game. The following code hasn't been finished yet, but for some reason, the ball won't move. It only works if I set the "ball_draw = pygame..." inside the "draw_ball" method, instead of the initializing method. However, if I do that, I can't use the "wallCollision" method in the class because the "ball_draw" would be a local variable and not an attribute of the whole class and therefore, not accessible in other methods. How can I fix this? I appreciate your help.

import pygame, sys
pygame.init()
clock = pygame.time.Clock()

screen_width = 1280
screen_height = 960
bgColor = pygame.Color("grey12")
lightGrey = (200, 200, 200)

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pong Game")

class BALL:
    def __init__(self):
        self.ball_width = 30
        self.ball_height = 30
        self.ball_x = screen_width/2 - self.ball_width
        self.ball_y = screen_height/2 - self.ball_height
        self.ball_speed_x = 7
        self.ball_speed_y = 7
        self.ball_draw = pygame.Rect(self.ball_x, self.ball_y, self.ball_width, self.ball_height)
    def draw_ball(self):
        pygame.draw.ellipse(screen, lightGrey, self.ball_draw)
    def move_ball(self):
        self.ball_x += self.ball_speed_x
        self.ball_y += self.ball_speed_y
    def wallCollision(self):
        if self.ball_draw.bottom >= screen_height:
            self.ball_speed_y *= -1
        if self.ball_draw.top <= 0:
            self.ball_speed_y *= -1
        if self.ball_draw.left <= 0:
            self.ball_speed_x *= -1
        if self.ball_draw.right >= screen_width:
            self.ball_speed_x *= -1

class PLAYER1:
    def __init__(self):
        self.player1_width = 10
        self.player1_height = 140
        self.player1_x = screen_width - 20
        self.player1_y = screen_height/2 - 70
    def draw_player1(self):
        player1_draw = pygame.Rect(self.player1_x, self.player1_y, self.player1_width, self.player1_height)
        pygame.draw.rect(screen, lightGrey, player1_draw)
    def move_player1(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP] and self.player1_y >= 0:
            self.player1_y -= 5
        if keys[pygame.K_DOWN] and self.player1_y <= screen_height:
            self.player1_y += 5

ball = BALL()
player1 = PLAYER1()

while True:

    # Checking for events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Drawing on screen
    screen.fill(bgColor)
    ball.draw_ball()
    player1.draw_player1()

    # Movement and Collisions
    ball.move_ball()
    player1.move_player1()
    ball.wallCollision()

    # Updating screen
    pygame.display.flip()
    # Defining 60 frames per second (FPS)
    clock.tick(60)

You use the rectangle attribute self.ball_draw to draw the ball. Hence, if you move the ball and change the self.ball_x and self.ball_y attributes, you need to update the rectangle:

class BALL:
    # [...]

    def draw_ball(self):
        pygame.draw.ellipse(screen, lightGrey, self.ball_draw)

    def move_ball(self):
        self.ball_x += self.ball_speed_x
        self.ball_y += self.ball_speed_y
        self.ball_draw.topleft = (self.ball_x, self.ball_y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM