gpt4 book ai didi

python - pygame中的矩形不会一致移动

转载 作者:行者123 更新时间:2023-12-01 06:25:20 25 4
gpt4 key购买 nike

我认为这段代码会在按下按键时导致矩形移动,并在释放按键时导致矩形停止移动,但我一定错过了一些东西。谁能看出这段代码有什么问题吗?我试图让它在按住键时移动一个矩形,并在释放它时停止,就像乒乓球一样。

import pygame, random, sys
from pygame.locals import *
fpsClock=pygame.time.Clock()
pygame.init()

WINDOWWIDTH = 600
WINDOWHEIGHT = 600
TEXTCOLOR = (255, 255, 255)
BACKGROUNDCOLOR = (0, 0, 255)
FPS = 40
BLACK = (0,0,0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
rectY1 = 300
rectY2 = 300
Y1change = 0
Y2change = 0
keys = pygame.key.get_pressed()

def moveup():
rectY1 -= 10
pygame.display.set_caption('Pong')
def drawshapes():
pygame.init()
DISPLAY=pygame.display.set_mode((600,600),0,32)
DISPLAY.fill(BLACK)
pygame.draw.rect(DISPLAY,RED,(18,rectY1,10,120))
pygame.draw.rect(DISPLAY,RED,(580,rectY2,10,120))
pygame.draw.ellipse(DISPLAY,BLUE,(300,300,30,30))
drawshapes()

while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if (event.key == pygame.K_UP):
Y1change -= 10

elif (event.key == pygame.K_DOWN):
Y1change += 10

elif (event.key == ord('w')):
Y2change -= 10

elif (event.key == ord('s')):
Y2change += 10
if event.type == KEYUP:
if (event.key == pygame.K_UP):
Y1change = 0

elif (event.key == pygame.K_DOWN):
Y1change = 0

elif (event.key == ord('w')):
Y2change = 0

elif (event.key == ord('s')):
Y2change = 0

rectY1 += Y1change
rectY2 += Y2change
drawshapes()
pygame.display.update()






fpsClock.tick(FPS)
pygame.display.update()

最佳答案

您实际上并不需要 Pygame 事件来进行这种连续移动,只需使用 pygame.key.get_pressed() 它会返回当前按下的键的字典。

在下面的示例代码中,我将 PyGame 矩形对象包装到另一个类中。这使得“Paddle”能够维护位置数据,并知道如何将自己绘制到屏幕上。

请尝试这个示例。

import pygame

# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE

DARK_BLUE = ( 3, 5, 54 )
YELLOW = ( 255, 250, 205 )

PADDLE_WIDTH = 70
PADDLE_HEIGHT= 10

### initialisation
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Moveable Rectangle")


### Simple class to hold the position of a moveable rectangle
### and keep it on-screen.
class Paddle:
def __init__( self, x, y, width, height ):
self.rect = pygame.Rect( x, y, width, height )

def draw( self, window ):
""" Draw the paddle on the given surface """
pygame.draw.rect( window, YELLOW, self.rect, 0 ) # filled rectangle

def move( self, x_delta ):
""" adjust the position of the paddle by <x_delta> pixels """
# Change position
self.rect.x += x_delta
# Keep it on the screen
if ( self.rect.x < 0 ):
self.rect.x = 0
elif ( self.rect.x + self.rect.width > WINDOW_WIDTH ):
self.rect.x = WINDOW_WIDTH - self.rect.width



### Controls
# Create a paddle, roughly centred in the window
player_paddle = Paddle( WINDOW_WIDTH//2, WINDOW_HEIGHT-50, PADDLE_WIDTH, PADDLE_HEIGHT )

### Main Loop
clock = pygame.time.Clock()
done = False
while not done:

# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True

# Movement keys
keys = pygame.key.get_pressed()
if ( keys[pygame.K_LEFT] ):
player_paddle.move( -10 )
if ( keys[pygame.K_RIGHT] ):
player_paddle.move( 10 )

# Update the window, but not more than 60fps
window.fill( DARK_BLUE )
player_paddle.draw( window )
pygame.display.flip()

# Clamp FPS
clock.tick_busy_loop(60)

pygame.quit()

关于python - pygame中的矩形不会一致移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60181192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com