Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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 如何在乒乓球比赛中使球拍移动?_Python_Pygame - Fatal编程技术网

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

Python 如何在乒乓球比赛中使球拍移动?,python,pygame,Python,Pygame,我是pygame的初学者。最近我编写了一个Pong游戏 然而,当我按下键盘上的某些键时,我无法使拨片移动。有人能帮我查一下密码吗。我想也许我在给球拍换新位置时遇到了一些问题。但我无法修复它。希望能给我一些提示 谢谢 代码如下: import pygame, sys, time,math from pygame.locals import * # User-defined functions def main(): # Initialize pygame pygame.init

我是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()
导入pygame、sys、time、math
从pygame.locals导入*
#用户定义函数
def main():
#初始化pygame
pygame.init()
#设置窗口大小、标题和帧延迟
表面尺寸=(500400)#窗口尺寸
windowTitle='Pong'#窗口标题
frameDelay=0.005#游戏越小速度越快
#创建窗口
pygame.key.set_重复(20,20)
surface=pygame.display.set_模式(surfaceSize,0,0)
pygame.display.set_标题(windowTitle)
#创建并初始化红点和蓝点
gameOver=False
color1=pygame.Color('白色')
center1=[250200]
半径1=10
分数=[0,0]
速度1=[4,1]
位置1=[50150]
位置2=[450150]
大小=(5100)
位置1=(0,0)
位置2=(350,0)
rect1=pygame.Rect(位置1,大小)
rect2=pygame.Rect(位置2,大小)
#绘制对象
pygame.draw.circle(曲面、颜色1、中心1、半径1、0)
#刷新显示
pygame.display.update()
#永远循环
尽管如此:
#处理事件
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
#处理其他事件
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_q:
位置1[1]=+1
如果event.key==pygame.K\p:
位置2[1]=+1
如果event.key==pygame.K_a:
位置1[1]=-1
如果event.key==pygame.K_i:
位置2[1]=-1
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_q:
位置1[1]=+0
如果event.key==pygame.K\p:
位置2[1]=+0
如果event.key==pygame.K_a:
位置1[1]=-0
如果event.key==pygame.K_i:
位置2[1]=-0
#处理其他事件
#更新并绘制下一帧的对象
gameOver=更新(曲面、颜色1、中心1、半径1、速度1、rect1、rect2、分数、位置1、位置2)
#刷新显示
pygame.display.update()
#通过在帧之间暂停来设置帧速度
睡眠时间(帧延迟)
def更新(表面、颜色1、中心1、半径1、速度1、rect1、rect2、分数、位置1、位置2):
#检查比赛是否结束。如果是这样,结束游戏并
#这是真的。否则,擦除窗口,移动点和
#画出返回False的点。
#-surface是窗口的pygame.surface对象
橡皮擦颜色=pygame.Color('Black')
表面填充(擦除颜色)
移动点(表面、中心1、半径1、速度1、分数、位置1、位置2)
pygame.draw.circle(曲面、颜色1、中心1、半径1,0)
r1=pygame.draw.rect(曲面、颜色1、rect1)
r2=pygame.draw.rect(曲面、颜色1、rect2)
如果r1.碰撞点(中心1)和速度1[0]0:
speed1[0]=-speed1[0]
def移动点(表面、中心、半径、速度、分数、位置1、位置2):
#通过按速度改变球的中心来移动球
#如果点碰到左边缘、上边缘、右边缘或下边缘
#在窗户外,球弹起
大小=曲面。获取大小()
对于范围(0,2)内的坐标:
中心[coord]=中心[coord]+速度[coord]
如果中心[coord]大小[coord]:
速度[coord]=-速度[coord]
如果中心[0]大小[0]:
分数[1]=分数[1]+1
划线(中心、表面、划线、位置1,1)
def drawScore(中心1、表面、刻痕、位置、刻痕):
字体大小=30
FontColor=pygame.Color('白色')
String='Score:'
font=pygame.font.SysFont(无、FontSize、True)
surface1=font.render(String+str(score[whichscore]),True,FontColor,0)
表面。blit(表面1,位置)
main()

您总是使用rects
rect1
rect2
来划桨。但要更新它们的位置,请尝试更改列表
location1
location2
中的值

住手。只要改变矩形。最简单的方法是在适当的位置更改矩形

此外,如果您想在玩家按住移动键的同时保持划桨移动,请使用获取所有按下键的列表(因为按下键只会生成一个
KEYDOWN
事件,除非您弄乱了
pygame.key.set\u 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 = ...
    ...

location1[1]=-0
这应该做什么?你的意思是分配负零还是减去零?不管怎样,这都是一件愚蠢的事情。但是,它会在每个关键帧上为位置指定负0,因此您的位置将始终为0。同样地,
location1[1]=+1
总是分配正1。我想你的意思是增加位置[1],语法应该是
location1[1]+=1
。我从那里开始。