pygame-pong-pong-pongectangle在按下W或S时不会移动
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将玩家的道具定义为:
但是您正在设置:
哪个趋于:
因此将上述内容更改为:
矩形将移动 w 和 s 。
You're defining the players props as:
But you're setting:
Which trows an:
So change the above to:
And the rectangles will move on w and s.