How can I make the ball move instead of stretch in pygame?

雨燕双飞 提交于 2021-02-13 05:42:25

问题


I'm building a pong game trying to get better at programming but Im having trouble moving the ball. When the move_right method is called the ellipse stretches to the right instead of moving to the right. I've tried putting the ball variable in the init method but that just makes it not move at all even though the variables should be changing on account of the move_right method. I have also tried setting the x and y positions as parameters in the Ball class,but that just stretches it also. I don't understand why when I run the following code the ball I'm trying to move stretches to the right instead of moves to the right. Can someone explain why this is happening? I have tried everything I can think of but i can't get it to do what I want.

import pygame,sys
import random


class Ball:
    def __init__(self):
        self.size = 30
        self.color = light_grey
        self.x_pos = width/2 -15
        self.y_pos = height/2 -15
        self.speed = 1
        #self.ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
   

    def draw_ball(self):
        ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
        pygame.draw.ellipse(screen,self.color,ball)

    def move_right(self):
        self.x_pos += self.speed

class Player:
    def __init__(self,x_pos,y_pos,width,height):
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.width = width
        self.height = height
        self.color = light_grey

    def draw_player(self):
        player = pygame.Rect(self.x_pos,self.y_pos,self.width,self.height)
        pygame.draw.rect(screen,self.color,player)



class Main:
    def __init__(self):
        self.ball=Ball()
        self.player=Player(width-20,height/2 -70,10,140)
        self.opponent= Player(10,height/2-70,10,140)

    def draw_elements(self):
        self.ball.draw_ball()
        self.player.draw_player()
        self.opponent.draw_player()

    def move_ball(self):
        self.ball.move_right()





pygame.init()
size = 30
clock = pygame.time.Clock()
pygame.display.set_caption("Pong")
width = 1000
height = 600
screen = pygame.display.set_mode((width,height))
bg_color = pygame.Color('grey12')
light_grey = (200,200,200)

main = Main()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#player = pygame.Rect(width-20,height/2 -70,10,140)
#opponent = pygame.Rect(10,height/2-70,10,140)


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    #ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
    #pygame.draw.rect(screen,light_grey,player)
    #pygame.draw.rect(screen,light_grey,opponent)
    #pygame.draw.ellipse(screen,light_grey,ball)
    main.draw_elements()
    main.move_ball()
    main.ball.x_pos += main.ball.speed
    pygame.display.flip()
    clock.tick(60)

回答1:


You have to clear the display in every frame with pygame.Surface.fill:

while True:
    # [...]

    screen.fill(0) # <---

    main.draw_elements()
    main.move_ball()
    main.ball.x_pos += main.ball.speed
    pygame.display.flip()

    # [...]

Everything that is drawn is drawn on the target surface. The entire scene is redraw in each frame. Therefore the display needs to be cleared at the begin of every frame in the application loop. The typical PyGame application loop has to:

  • handle the events by either pygame.event.pump() or pygame.event.get().
  • update the game states and positions of objects dependent on the input events and time (respectively frames)
  • clear the entire display or draw the background
  • draw the entire scene (blit all the objects)
  • update the display by either pygame.display.update() or pygame.display.flip()


来源:https://stackoverflow.com/questions/65494890/how-can-i-make-the-ball-move-instead-of-stretch-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!