如何使球拍在乒乓球中移动?

2024-09-30 08:29:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我是pygame的初学者。最近我编写了一个Pong游戏。在

但是,当我按键盘上的某些键时,我无法使拨杆移动。有人能帮我查一下密码吗。我想也许我在给斯皮尔斯新的位置上有些问题。但我不能修好它。希望能给我一些提示。在

谢谢!在

代码如下:

import pygame, sys, time,math
from pygame.locals import *


# User-defined functions

def main():

   # Initialize pygame
   pygame.init()

   # Set window size and title, and frame delay
   surfaceSize = (500, 400) # window size
   windowTitle = 'Pong' #window title
   frameDelay = 0.005 # smaller is faster game

   # Create the window
   pygame.key.set_repeat(20, 20)
   surface = pygame.display.set_mode(surfaceSize, 0, 0)
   pygame.display.set_caption(windowTitle)

   # create and initialize red dot and blue dot
   gameOver = False
   color1=pygame.Color('white')
   center1 = [250, 200]
   radius1=10
   score=[0, 0]
   speed1=[4,1]
   location1=[50, 150]
   location2=[450, 150]
   size=(5, 100)
   position1=(0,0)
   position2=(350,0)

   rect1=pygame.Rect(location1,size)
   rect2=pygame.Rect(location2,size)

   # Draw objects
   pygame.draw.circle(surface, color1, center1, radius1, 0)

   # Refresh the display
   pygame.display.update()

   # Loop forever
   while True:
      # Handle events
      for event in pygame.event.get():
         if event.type == QUIT:
            pygame.quit()
            sys.exit()

   # Handle additional events

         if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
               location1[1] =+ 1
            if event.key == pygame.K_p:
               location2[1] =+ 1
            if event.key == pygame.K_a:
               location1[1] =- 1
            if event.key == pygame.K_i:
               location2[1] =- 1           

         if event.type == pygame.KEYUP:
            if event.key == pygame.K_q:
               location1[1] =+ 0
            if event.key == pygame.K_p:
               location2[1] =+ 0
            if event.key == pygame.K_a:
               location1[1] =- 0
            if event.key == pygame.K_i:
               location2[1] =- 0       

         # Handle additional events

      # Update and draw objects for the next frame
      gameOver = update(surface,color1,center1,radius1,speed1,rect1,rect2,score,position1,position2)


      # Refresh the display
      pygame.display.update()

      # Set the frame speed by pausing between frames
      time.sleep(frameDelay)

def update(surface,color1,center1,radius1,speed1,rect1,rect2,score,position1,position2):
   # Check if the game is over. If so, end the game and
   # returnTrue. Otherwise, erase the window, move the dots and
   # draw the dots return False.
   # - surface is the pygame.Surface object for the window
   eraseColor=pygame.Color('Black')
   surface.fill(eraseColor)
   moveDot(surface,center1,radius1,speed1,score,position1,position2)
   pygame.draw.circle(surface,color1,center1,radius1,0)
   r1=pygame.draw.rect(surface, color1, rect1)
   r2=pygame.draw.rect(surface, color1, rect2)
   if r1.collidepoint(center1) and speed1[0]<0:
      speed1[0]=-speed1[0]
   if r2.collidepoint(center1) and speed1[0]>0:
      speed1[0]=-speed1[0]




def moveDot(surface,center,radius,speed,score,position1,position2):
   #Moves the ball by changing the center of the ball by its speed
   #If dots hits left edge, top edge, right edge or bottom edge
   #of the window, the then the ball bounces
   size=surface.get_size()

   for coord in range(0,2):
      center[coord]=center[coord]+speed[coord]
      if center[coord]<radius:
         speed[coord]=-speed[coord]
      if center[coord]+radius>size[coord]:
         speed[coord]=-speed[coord]
   if center[0]<radius:
      score[0]=score[0]+1
   drawScore(center,surface,score,position2,0)
   if center[0]+radius>size[0]:
      score[1]=score[1]+1
   drawScore(center,surface,score,position1,1)


def drawScore(center1,surface,score,position,whichscore):
   FontSize=30
   FontColor=pygame.Color('White')
   String='Score : '
   font=pygame.font.SysFont(None, FontSize, True)
   surface1=font.render(String+str(score[whichscore]), True, FontColor,0)
   surface.blit(surface1,position)



main()

Tags: andthekeyeventsizeifwindowsurface
1条回答
网友
1楼 · 发布于 2024-09-30 08:29:08

你总是用矩形rect1rect2来画你的划桨。但是为了更新它们的位置,您可以尝试更改列表location1和{}中的值。在

住手。只需改变矩形。最简单的方法是使用^{}来改变矩形。在

另外,如果你想在玩家按住移动键的同时保持你的球拍移动,使用^{}来获得所有按下键的列表(因为按下一个键只会生成一个KEYDOWN事件,除非你搞乱了pygame.key.set_repeat,这是不应该的)。在

所以你的代码应该是这样的:

...
while True:
    # Handle events
    for event in pygame.event.get():
       if event.type == QUIT:
          pygame.quit()
          sys.exit()

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_q]: rect1.move_ip(0, -1)
    if pressed[pygame.K_a]: rect1.move_ip(0,  1)
    if pressed[pygame.K_p]: rect2.move_ip(0, -1)
    if pressed[pygame.K_i]: rect2.move_ip(0,  1)

    gameOver = ...
    ...

相关问题 更多 >

    热门问题