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!阅读原文】。感谢您的支持!

您可能感兴趣的

Creating a rectangular grid in Pygame :) This is my first question here and my english isn't that good, so please bare with me. Well, I need to create clickable a 8 by 8 grid in pygame. ...
Pygame or App Development using Kivy or XCode? Sunil writes: I would, first of all, like to thank you for the amazing book you have written "Python for Kids" It's really helped my nephew lear...
Tips And Tricks for Shipping a PyGame App on the M... I’vewritten and spoken at some length about shipping software in the abstract. Sometimes I’ve even had the occasionalconcrete tidbit, but that adv...
‘Unable to import namesets’ I'm a beginner and so far I have about 3 hours invested installing pygame( ie getting the python interpreter to accept 'from livewires import games, c...
Building Games With Python 3 and Pygame: Part 1 Overview A lot of developers get into software development because they want to build games. Not everybody can be a professional game developer, but...