Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单人“乒乓”游戏_Python_Pygame_Livewires - Fatal编程技术网

Python 单人“乒乓”游戏

Python 单人“乒乓”游戏,python,pygame,livewires,Python,Pygame,Livewires,我刚开始学习pygame和livewires,我正在尝试制作一个单人乒乓球游戏,你只需击球,它就会反弹,直到它经过屏幕左侧的挡板,并由鼠标控制,这会让你输。我有基本的代码,但球不会停留在屏幕上,它只是闪烁,不会保持不变。此外,拨杆不随鼠标移动。我肯定我错过了一些简单的东西,但我就是想不出来。救命啊!以下是我所拥有的: from livewires import games import random games.init(screen_width=640, screen_height=480,

我刚开始学习pygame和livewires,我正在尝试制作一个单人乒乓球游戏,你只需击球,它就会反弹,直到它经过屏幕左侧的挡板,并由鼠标控制,这会让你输。我有基本的代码,但球不会停留在屏幕上,它只是闪烁,不会保持不变。此外,拨杆不随鼠标移动。我肯定我错过了一些简单的东西,但我就是想不出来。救命啊!以下是我所拥有的:

from livewires import games
import random

games.init(screen_width=640, screen_height=480, fps=50)

class Paddle(games.Sprite):

    image=games.load_image("paddle.bmp")

    def __init__(self, x=10):
        super(Paddle, self).__init__(image=Paddle.image,
                                    y=games.mouse.y,
                                    left=10)
        self.score=games.Text(value=0, size=25, top=5, right=games.screen.width - 10)
        games.screen.add(self.score)

    def update(self):
        self.y=games.mouse.y
        if self.top<0:
            self.top=0
        if self.bottom>games.screen.height:
            self.bottom=games.screen.height
        self.check_collide()

    def check_collide(self):
        for ball in self.overlapping_sprites:
            self.score.value+=1
            ball.handle_collide()

class Ball(games.Sprite):

    image=games.load_image("ball.bmp")
    speed=5

    def __init__(self, x=90, y=90):
        super(Ball, self).__init__(image=Ball.image,
                                   x=x, y=y,
                                   dx=Ball.speed, dy=Ball.speed)

    def update(self):
        if self.right>games.screen.width:
            self.dx=-self.dx
        if self.bottom>games.screen.height or self.top<0:
            self.dy=-self.dy
        if self.left<0:
            self.end_game()
            self.destroy()

    def handle_collide(self):
        self.dx=-self.dx

    def end_game(self):
        end_message=games.Message(value="Game Over",
                                  size=90,
                                  x=games.screen.width/2,
                                  y=games.screen.height/2,
                                  lifetime=250,
                                  after_death=games.screen.quit)
        games.screen.add(end_message)



def main():

    background_image=games.load_image("background.bmp", transparent=False)
    games.screen.background=background_image

    paddle_image=games.load_image("paddle.bmp")
    the_paddle=games.Sprite(image=paddle_image,
                            x=10,
                            y=games.mouse.y)
    games.screen.add(the_paddle)

    ball_image=games.load_image("ball.bmp")
    the_ball=games.Sprite(image=ball_image,
                          x=630,
                          y=200,
                          dx=2,
                          dy=2)
    games.screen.add(the_ball)

    games.mouse.is_visible=False

    games.screen.event_grab=True

    games.screen.mainloop()

main()

我帮不了你,因为你没有在这里发布完整的代码。至少,我看不到您在哪里更新精灵self.x+=self.dx的位置?以及更新绘图到屏幕。你也没有在主函数中使用你的类

也就是说,我明白了

 def __init__(self, x=10):
在构造函数中,您从未使用过x变量。这也让我担心

考虑将“桨和球”类用作精灵,如下所示:

if __name__ == '__main__':

    background_image = games.load_image("background.bmp", transparent=False)
    games.screen.background = background_image

    the_paddle = Puddle()
    games.screen.add(the_paddle)

    the_ball = Ball()
    games.screen.add(the_ball)

    games.mouse.is_visible = False
    games.screen.event_grab = True

    games.screen.mainloop()

注意,我已经冒昧地让你的代码读得更像Python。但是,我从未使用过livewires,因此我的代码可能无法正常工作。但它应该为你指明正确的方向。祝你好运

为什么要使用livewires?您只能将pygame用于乒乓球游戏

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480)) # window size
pygame.display.set_caption("Simple pong") # window title

# this is a rect that contains the ball
# at the beginning it is set in the center of the screen
ball_rect = pygame.Rect((312, 232), (16, 16))
# speed of the ball (x, y)
ball_speed = [4, 4]

# this contains your paddle
# vertically centered on the left side
paddle_rect = pygame.Rect((8, 200), (8, 80))

# 1 point if you hit the ball
# -5 point if you miss the ball
score = 0

# load the font for displaying the score
font = pygame.font.Font(None, 30)

# mainloop
while True:
    # event handler
    for event in pygame.event.get():
        # quit event => close the game
        if event.type == pygame.QUIT:
            exit(0)
        # control the paddle with the mouse
        elif event.type == pygame.MOUSEMOTION:
            paddle_rect.centery = event.pos[1]
            # correct paddle position if it's going out of window
            if paddle_rect.top < 0:
                paddle_rect.top = 0
            elif paddle_rect.bottom >= 480:
                paddle_rect.bottom = 480

    # this test if up or down keys are pressed
    # if yes move the paddle
    if pygame.key.get_pressed()[pygame.K_UP] and paddle_rect.top > 0:
        paddle_rect.top -= 5
    elif pygame.key.get_pressed()[pygame.K_DOWN] and paddle_rect.bottom < 480:
        paddle_rect.top += 5

    # update ball position
    # this move the ball
    ball_rect.left += ball_speed[0]
    ball_rect.top += ball_speed[1]

    # these two if block control if the ball is going out on the screen
    # if it's going it reverse speed to simulate a bounce
    if ball_rect.top <= 0 or ball_rect.bottom >= 480:
        ball_speed[1] = -ball_speed[1]

    if ball_rect.right >= 640:
        ball_speed[0] = -ball_speed[0]
    # this control if the ball touched the left side
    elif ball_rect.left <= 0:
        score -= 5
        # reset the ball to the center
        ball_rect = pygame.Rect((312, 232), (16, 16))

    # test if the ball is hit by the paddle
    # if yes reverse speed and add a point
    if paddle_rect.colliderect(ball_rect):
        ball_speed[0] = -ball_speed[0]
        score += 1

    # clear screen
    screen.fill((255, 255, 255))

    # draw the ball, the paddle and the score
    pygame.draw.rect(screen, (0, 0, 0), paddle_rect) # paddle
    pygame.draw.circle(screen, (0, 0, 0), ball_rect.center, ball_rect.width/2) # ball
    score_text = font.render(str(score), True, (0, 0, 0))
    screen.blit(score_text, (320-font.size(str(score))[0]/2, 5)) # score

    # update screen and wait 20 milliseconds
    pygame.display.flip()
    pygame.time.delay(20)

我在任何地方都看不到screen.flip,但可能在screen.mainloop中?如果使用双缓冲,一些库会为您翻转缓冲区。不太多,我不知道livewires是否是其中之一,但这里的问题不是不调用flip。杰姆提到他的球在闪烁。