pygame-pong-pong-pongectangle在按下W或S时不会移动

发布于 2025-01-24 02:47:23 字数 1702 浏览 0 评论 0原文

pygame -pong-矩形在按下 w s

我只是不明白为什么它不移动plz帮助我是新手Python:3

import pygame
import sys
import numpy as np


class Game( ):
    def __init__(self):
        pygame.init( )

        self.player_x = 75
        self.player_y = 300
        self.player1_x = 1100
        self.player1_y = 300
        self.screen = pygame.display.set_mode((1200, 800))
        pygame.display.set_caption("Pong")
        self.FPS = pygame.time.Clock( )
        self.player_speed = 0

        self.width = 25
        self.hight = 100
        self.speed = 5


        self.game_loop()

    def game_loop(self):
        while True:
            self.FPS = pygame.time.Clock()

            for event in pygame.event.get( ):
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_w:
                        self.player_speed -= 7
                    if event.key == pygame.K_s:
                        self.player_speed += 7

            self.screen.fill((0, 0, 0))
            self.Player_1()

            self.player.y += self.player_speed
            self.player1.y += self.player_speed



            pygame.display.flip()
            self.FPS.tick(60)




    def Player_1(self):

        self.player = pygame.Rect(self.player_x, self.player_y, self.width, self.hight)
        self.player1 = pygame.Rect(self.player1_x, self.player1_y, self.width, self.hight)
        pygame.draw.rect(self.screen, "Red", self.player)
        pygame.draw.rect(self.screen, "Red", self.player1)


Game( )

Pygame -Pong- rectangle doesn't move while pressing w or s

I just don't get why its not moving plz help i am new at python :3

import pygame
import sys
import numpy as np


class Game( ):
    def __init__(self):
        pygame.init( )

        self.player_x = 75
        self.player_y = 300
        self.player1_x = 1100
        self.player1_y = 300
        self.screen = pygame.display.set_mode((1200, 800))
        pygame.display.set_caption("Pong")
        self.FPS = pygame.time.Clock( )
        self.player_speed = 0

        self.width = 25
        self.hight = 100
        self.speed = 5


        self.game_loop()

    def game_loop(self):
        while True:
            self.FPS = pygame.time.Clock()

            for event in pygame.event.get( ):
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_w:
                        self.player_speed -= 7
                    if event.key == pygame.K_s:
                        self.player_speed += 7

            self.screen.fill((0, 0, 0))
            self.Player_1()

            self.player.y += self.player_speed
            self.player1.y += self.player_speed



            pygame.display.flip()
            self.FPS.tick(60)




    def Player_1(self):

        self.player = pygame.Rect(self.player_x, self.player_y, self.width, self.hight)
        self.player1 = pygame.Rect(self.player1_x, self.player1_y, self.width, self.hight)
        pygame.draw.rect(self.screen, "Red", self.player)
        pygame.draw.rect(self.screen, "Red", self.player1)


Game( )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞琼窗梦回ˉ 2025-01-31 02:47:23

您将玩家的道具定义为:

self.player_x = 75
self.player_y = 300
self.player1_x = 1100
self.player1_y = 300

但是您正在设置:

self.player.y += self.player_speed
self.player1.y += self.player_speed

哪个趋于:

attributeError:'game'对象没有属性'player'


因此将上述内容更改为:

self.player_y += self.player_speed
self.player1_y += self.player_speed

矩形将移动 w s

You're defining the players props as:

self.player_x = 75
self.player_y = 300
self.player1_x = 1100
self.player1_y = 300

But you're setting:

self.player.y += self.player_speed
self.player1.y += self.player_speed

Which trows an:

AttributeError: 'Game' object has no attribute 'player'


So change the above to:

self.player_y += self.player_speed
self.player1_y += self.player_speed

And the rectangles will move on w and s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文