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):
    size=Surface.get_size()
    for item in [0,1]:
        pos[item]=pos[item]+speed[item]
        if pos[item]size[item]:
            speed[item]=-speed[item]

# Open a brand-new window

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

# Set up white color for drawing balls

white=pygame.Color('white')

# Now, we start to draw two balls

pos1=(100,200)
pos2=(400,200)
rad=10
ball1=pygame.draw.circle(Surface,white,pos1,rad,0)
ball2=pygame.draw.circle(Surface,white,pos2,rad,0)
pygame.display.update()

# Now, define speed
speed1=(2,-2)
speed2=(-2,2)

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

# Now, we move the ball
ball1move=ball_move(Surface,white,pos1,rad,speed1)
ball2move=ball_move(Surface,white,pos2,rad,speed2)
pygame.draw.circle(Surface,white,pos1,rad,0,0)
pygame.draw.circle(Surface,white,pos2,rad,0,0)
surface.fill(pygame.Color('black'))

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
    pygame.display.update()

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)

pygame.init()
screen_size = (500, 400)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('Pong')
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
    screen.fill(pygame.Color('black'))
    pygame.draw.circle(screen, pygame.Color('white'), pos1, rad)
    pygame.draw.circle(screen, pygame.Color('white'), pos2, rad)
    pygame.display.update()
Hello, buddy!

责编内容by:Hello, buddy! (源链)。感谢您的支持!

您可能感兴趣的

Windows7安装Pygame软件 1. 工具包下载地址: Python下载地址: https://www.python.org/downloads/ pip下载地址: ...
使用Python快速写一个小游戏 引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏、后台等方面,python也大放异彩,本篇博文将按照正规的项目开发流程,...
Why can not the balls move in my code in Pygame wh... I am a beginner in Pygame. I have coded a function for moving two balls in di...
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 ...
Rene Dudfield: pygame on pypy usable Hi, TLDR; I'm at the pypy sprint, and working through the remaining pygame...