Why can not the balls move in my code in Pygame when I code ‘Pong’? Can anyone help m…

综合编程 2018-06-20 阅读原文

I am a beginner in Pygame. I have coded a function for moving two balls in different direction and I follow the instructions coding it but it seems to be not working. I can draw two balls in screen but they will not move. I fixed it for almost 1 hour but no idea why balls aren't moving.

So, Can someone helps me check my code and just give me some hints. I will really appreciate anyone who helps me!

My code shows below

import pygame,sys,time
from pygame.locals import *

# User define function
def ball_move(Surface,white,pos,rad,speed):
    for item in [0,1]:
        if pos[item]size[item]:

# Open a brand-new window

Screen_size = (500,400)
Title = ('Pong')
Frame_Delay = 0.01
Surface= pygame.display.set_mode(Screen_size,0,0)

# Set up white color for drawing balls


# Now, we start to draw two balls


# Now, define speed

# Now, we define a loop
while ball1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:

# Now, we move the ball

Part of saulspatz answer is correct, part is incorrect. You don't have to use sprites if you dont want to. pygame.draw
is not pretty but perfectly usable. The main problem does seem to be your understanding of what to do in your event loop. All this should go in it:

while running:
    # Handdle your events
    # update your state
    # draw to your display

Also I notice in your unreachable code after the loop you are filling after your draws. Remember whether you fill
, blit
, or draw
the latest thing goes over the rest. So for your example:

import pygame ,sys, time
from pygame.locals import *

# User define function
def ball_move(surface, pos, rad, speed):
    def _add(l_pos, l_speed, l_size):
        l_pos += l_speed
        if l_pos = l_size - rad:
            l_speed = -l_speed
        return l_pos, l_speed
    size = surface.get_size()
    pos_x, speed_x = _add(pos[0], speed[0], size[0])
    pos_y, speed_y = _add(pos[1], speed[1], size[1])
    return (pos_x, pos_y), (speed_x, speed_y)

screen_size = (500, 400)
screen = pygame.display.set_mode(screen_size)
running = True

pos1 = (100, 200)
pos2 = (400, 200)
speed1 = (2, -2)
speed2 = (-2, 2)
rad = 10

while running:
    # Handdle your events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # update your state
    pos1, speed1 = ball_move(screen, pos1, rad, speed1)
    pos2, speed2 = ball_move(screen, pos2, rad, speed2)

    # draw to your display
    pygame.draw.circle(screen, pygame.Color('white'), pos1, rad)
    pygame.draw.circle(screen, pygame.Color('white'), pos2, rad)
Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!


Rene Dudfield: pygame on pypy usable Hi, TLDR; I'm at the pypy sprint, and working through the remaining pygame-on-pypy-cpyext issues. https://yo...
Easier than ever to contribute to pygame Easier than ever to contribute to pygame — 15 Aug, 2018 We have a new page on how tocontribute to pygame. It...
Unbound local error with global variable I am trying to figure out why I get an UnboundLocalError in my pygame application, Table Wars. Here is a summary of what...
Build a game framework with Python using the Pygam... In my first article in this series , I explained how to use Python to create a simple, text-based dice game. This time,...
gamingdirectional: Pygame Music player demo In this article we are going to play the background music with the help of the pygame.mixer_music module. We will first...