为什么当我编码 'Pong' 时,球不能在我的代码中移动 Pygame?有人可以帮我检查我的代码吗?
Why balls cannot move in my code in Pygame when I code 'Pong'? Can Someone help me to check my code?
我是 Pygame 的初学者。我已经编写了一个函数,用于在不同方向移动两个球,并且我按照编写它的说明进行操作,但它似乎不起作用。我可以在屏幕上画两个球,但它们不会移动。我修复了将近 1 小时,但不知道为什么球没有移动。
所以,有人可以帮我检查我的代码并给我一些提示吗?我会非常感谢任何帮助我的人!
我的代码如下所示
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]<rad:
speed[item]=-speed[item]
if pos[item]+rad>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'))
你的代码有很多问题。最基本的是你还没有弄清楚事件驱动编程是如何工作的。您需要将移动球的代码放入循环中。
另一个问题是我认为您不想使用 pygame.draw
模块。我已经很久没有写 pygame 脚本了,但我记得,这个模块对于绘制固定对象很有用,比如背景。快速查看文档似乎可以证实这一点。
对于移动物体,我想你需要看看pygame.sprite
模块。即使你让这段代码工作,它也不会移动球。它只会在另一个位置画一个新球。所以你会先有两个球,然后是四个,然后是八个,......精灵实际上移动。对象不仅在新位置绘制,而且在旧位置被擦除。您的代码根本没有解决擦除问题。
希望这对您有所帮助。
saulspatz 的回答部分正确,部分错误。如果你不想,你不必使用精灵。 pygame.draw
不漂亮但非常好用。主要问题似乎确实是您对在事件循环中做什么的理解。所有这些都应该放在里面:
while running:
# Handdle your events
# update your state
# draw to your display
pygame.display.update()
我还注意到您在绘制后填充的循环之后无法访问的代码。请记住,无论您是 fill
、blit
还是 draw
,最新的都比其他的重要。所以对于你的例子:
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 <= rad or 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()
我是 Pygame 的初学者。我已经编写了一个函数,用于在不同方向移动两个球,并且我按照编写它的说明进行操作,但它似乎不起作用。我可以在屏幕上画两个球,但它们不会移动。我修复了将近 1 小时,但不知道为什么球没有移动。
所以,有人可以帮我检查我的代码并给我一些提示吗?我会非常感谢任何帮助我的人!
我的代码如下所示
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]<rad:
speed[item]=-speed[item]
if pos[item]+rad>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'))
你的代码有很多问题。最基本的是你还没有弄清楚事件驱动编程是如何工作的。您需要将移动球的代码放入循环中。
另一个问题是我认为您不想使用 pygame.draw
模块。我已经很久没有写 pygame 脚本了,但我记得,这个模块对于绘制固定对象很有用,比如背景。快速查看文档似乎可以证实这一点。
对于移动物体,我想你需要看看pygame.sprite
模块。即使你让这段代码工作,它也不会移动球。它只会在另一个位置画一个新球。所以你会先有两个球,然后是四个,然后是八个,......精灵实际上移动。对象不仅在新位置绘制,而且在旧位置被擦除。您的代码根本没有解决擦除问题。
希望这对您有所帮助。
saulspatz 的回答部分正确,部分错误。如果你不想,你不必使用精灵。 pygame.draw
不漂亮但非常好用。主要问题似乎确实是您对在事件循环中做什么的理解。所有这些都应该放在里面:
while running:
# Handdle your events
# update your state
# draw to your display
pygame.display.update()
我还注意到您在绘制后填充的循环之后无法访问的代码。请记住,无论您是 fill
、blit
还是 draw
,最新的都比其他的重要。所以对于你的例子:
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 <= rad or 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()