简体   繁体   English

为什么在我为“ Pong”编码时,球无法在Pygame的代码中移动? 有人可以帮我检查我的代码吗?

[英]Why balls cannot move in my code in Pygame when I code 'Pong'? Can Someone help me to check my code?

I am a beginner in Pygame. 我是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. 我将其修复了将近1个小时,但不知道为什么球不动。

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]<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'))

Part of saulspatz answer is correct, part is incorrect. saulspatz答案的一部分正确,一部分不正确。 You don't have to use sprites if you dont want to. 如果您不想,则不必使用精灵。 pygame.draw is not pretty but perfectly usable. pygame.draw不是很漂亮,但是完全可以使用。 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. 请记住,是否fillblitdraw最新的内容胜过其余的事情。 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 <= 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()

There are a lot of problems with your code. 您的代码有很多问题。 The most basic is that you haven't figured out how event-driven programming works. 最基本的是,您还没有弄清楚事件驱动的编程是如何工作的。 You need to put the code that moves the balls inside your loop. 您需要将可移动球的代码放入循环中。

Another problem is that I don't think you want to use the pygame.draw module. 另一个问题是我不希望您使用pygame.draw模块。 It's been a long time since I wrote any pygame scripts, but as I remember, this module is useful for drawing fixed objects, like the background. 自编写任何pygame脚本以来已经有很长时间了,但是我记得,该模块对于绘制固定对象(例如背景)很有用。 A quick look at the docs seems to confirm this. 快速浏览一下文档似乎可以证实这一点。

For moving objects, I think you need to look at the pygame.sprite module. 对于移动对象,我认为您需要查看pygame.sprite模块。 Even if you got this code to work, it wouldn't move the balls. 即使您可以使用此代码,也不会动弹。 It would just draw a new ball at another position. 它将在另一个位置绘制一个新球。 So you would have first two balls, then four, then eight, ... . 因此,您将拥有前两个球,然后是四个,然后是八个,...。 Sprites actually move. 精灵实际上会移动。 Not only is the object drawn at the new position, but it's erased at the old position. 对象不仅在新位置绘制,而且在旧位置被擦除。 Your code don't address erasure at all. 您的代码根本无法解决擦除问题。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Pygame中对Pong进行编码时,为什么不能显示球? - Why balls cannot be displayed when I code Pong in Pygame? 有人可以帮我写代码吗? (Python) - Can someone help me with my code? (python) 有人可以帮助我使用我的随机代码生成器吗? - Can someone please help me with my random code generator? 有没有办法检测消息中的链接? (或者有人可以帮我写代码) - Is there a way to detect a link in a message? (or can someone help me with my code) 有人可以帮我找到代码中错误源的解决方案吗? - Can someone help me find a solution to error source in my code? 在Pygame中,我无法移动子画面? 是我的代码吗? - In Pygame i cannot make my sprite move?? is it my code? 你好,我在 kivymd 上练习,当我运行我的代码时,它显示我'未命名的 window' 有人可以帮助我吗? - hello, iam practicing on kivymd, and when i run my code it shows me 'unnamed window ' can someone help me? 有人可以帮助我为什么我的 pygame 代码中存在关于“球移动”的错误? - Can somebady help me why errors exist in my pygame code about `ball moving`? 谁能帮我解决这个 pygame TypeError 我的代码? - Can anyone help me solve this pygame TypeError I get with my code? 有人可以告诉我为什么我的代码不起作用吗? - Can someone tell me why my code isn't working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM