综合编程

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

微信扫一扫,分享到朋友圈

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

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!

Microsoft Azure goes down, time for a timely “I told you so” post from Ben

上一篇

How to Use Facebook Groups for Business Growth

下一篇

您也可能喜欢

评论已经被关闭。

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

长按储存图像,分享给朋友