Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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 PyGame碰撞正在工作,但精灵正在重置-运动未继续。_Python_Pygame - Fatal编程技术网

Python PyGame碰撞正在工作,但精灵正在重置-运动未继续。

Python PyGame碰撞正在工作,但精灵正在重置-运动未继续。,python,pygame,Python,Pygame,我的PyGame实验游戏有问题-我正在学习如何使用精灵 我一直在尝试编写精灵(球和桨)之间的“碰撞”检测代码,并设法使碰撞检测工作,但我的球精灵似乎重置了其位置,而不是继续。谁能看看我的错误在哪里 这是我的密码: import pygame BLACK = ( 0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) #variables, constants, functions x = 1 y = 1 x_vel = 10

我的PyGame实验游戏有问题-我正在学习如何使用精灵

我一直在尝试编写精灵(球和桨)之间的“碰撞”检测代码,并设法使碰撞检测工作,但我的球精灵似乎重置了其位置,而不是继续。谁能看看我的错误在哪里

这是我的密码:

import pygame

BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)

#variables, constants, functions
x = 1
y = 1
x_vel = 10
y_vel = 10

bat_x = 1
bat_y = 1
bat_x_vel = 0
bat_y_vel = 0

score = 0

class Ball(pygame.sprite.Sprite):
    """
    This class represents the ball.
    It derives from the "Sprite" class in Pygame.
    """
    def __init__(self, width, height):
        """ Constructor. Pass in the color of the block,
        and its x and y position. """
        # Call the parent class (Sprite) constructor
        super().__init__()
        # Set the background color and set it to be transparent
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)

        # Draw the ellipse
        pygame.draw.ellipse(self.image, (255,0,0), [0,0,width,height], 10)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        self.rect = self.image.get_rect()

        # Instance variables that control the edges of where we bounce
        self.left_boundary = 0
        self.right_boundary = 0
        self.top_boundary = 0
        self.bottom_boundary = 0

        # Instance variables for our current speed and direction
        self.vel_x = 5
        self.vel_y = 5

    def update(self):
        """ Called each frame. """
        self.rect.x += self.vel_x
        self.rect.y += self.vel_y

        if self.rect.right >= self.right_boundary or self.rect.left <= self.left_boundary:
            self.vel_x *= -1

        if self.rect.bottom >= self.bottom_boundary or self.rect.top <= self.top_boundary:
            self.vel_y *= -1


class Paddle(pygame.sprite.Sprite):
    """
    This class represents the ball.
    It derives from the "Sprite" class in Pygame.
    """
    def __init__(self, width, height):
        """ Constructor. Pass in the color of the block,
        and its x and y position. """
        # Call the parent class (Sprite) constructor
        super().__init__()
        # Set the background color and set it to be transparent
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)

        # Draw the rectangle
        pygame.draw.rect(self.image, (0, 255, 0), [0, 0, width, height], 0)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        self.rect = self.image.get_rect()

        # Instance variables for our current speed and direction
        self.x_vel = 0
        self.y_vel = 0

    def update(self):
        # Get the current mouse position. This returns the position
        # as a list of two numbers.
        self.rect.x = self.rect.x + self.x_vel
        self.rect.y = self.rect.y + self.y_vel


#initialise ball and paddle
paddle = Paddle(20, 100)
ball = Ball(100,100)

# This is a list of every sprite.
# All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(ball)
all_sprites_list.add(paddle)
ball_sprites_list = pygame.sprite.Group()
ball_sprites_list.add(ball)

# Initialize Pygame
pygame.init()

# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# Loop until the user clicks the close button.
done = False

# -------- Main Program Loop -----------
while not done:
    # --- Events code goes here (mouse clicks, key hits etc)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                paddle.y_vel = -3
            if event.key == pygame.K_DOWN:
                paddle.y_vel = 3

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                paddle.y_vel = 0
            if event.key == pygame.K_DOWN:
                paddle.y_vel = 0

    # --- Game logic should go here
    # Calls update() method on every sprite in the list
    all_sprites_list.update()

    # collision check
    ball_hit_list = pygame.sprite.spritecollide(paddle, ball_sprites_list, False)

    # Check the list of collisions.
    for ball in ball_hit_list:
        score +=1
        print(score)

    # --- Clear the screen
    screen.fill((255,255,255))

    # --- Draw all the objects
    all_sprites_list.draw(screen)

    # render text
    myfont = pygame.font.SysFont("monospace", 15)
    label = myfont.render(str(score), 1, (0,0,0))
    screen.blit(label, (100, 100))

    # --- Update the screen with what we've drawn.
    pygame.display.flip()

    # --- Limit to 60 frames per second
    clock.tick(60)

pygame.quit()
导入pygame
黑色=(0,0,0)
白色=(255,255,255)
红色=(255,0,0)
#变量、常数、函数
x=1
y=1
x_等级=10
y_水平=10
bat_x=1
bat_y=1
bat_x_水平=0
bat_y_水平=0
分数=0
班级舞会(pygame.sprite.sprite):
"""
这个类代表球。
它源于Pygame中的“精灵”类。
"""
定义初始值(自身、宽度、高度):
“”“构造函数。传入块的颜色,
以及它的x和y位置
#调用父类(Sprite)构造函数
super()。\uuuu init\uuuuu()
#设置背景色并将其设置为透明
self.image=pygame.Surface([宽度,高度])
self.image.fill(白色)
self.image.set_颜色键(白色)
#画椭圆
pygame.draw.ellipse(self.image,(255,0,0),[0,0,宽度,高度],10)
#获取具有图像尺寸的矩形对象
#形象。
#通过设置值更新此对象的位置
#关于矩形x和矩形y
self.rect=self.image.get_rect()
#控制反弹点边缘的实例变量
self.left_边界=0
self.right_边界=0
self.top_边界=0
self.bottom_边界=0
#当前速度和方向的实例变量
self.vel_x=5
self.vel_y=5
def更新(自我):
“”“调用每个帧。”“”
self.rect.x+=self.vel_x
self.rect.y+=self.vel_y
如果self.rect.right>=self.right\u boundary或self.rect.left=self.bottom\u boundary或self.rect.top对不起

我们发现了错误

没有正确设置窗口的边界

# Instance variables that control the edges of where we bounce
self.left_boundary = 0
self.right_boundary = 700
self.top_boundary = 0
self.bottom_boundary = 400

请考虑简化代码以获得更快的答案。谢谢,发现错误。感谢您的帮助。为什么不简单地使用
Rect
?您使用的值似乎描述了一个rect,因此我建议在这里使用
rect
类。