Python 乒乓球游戏

Python 乒乓球游戏,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在做pygame中的乒乓球克隆,我的球移动有困难。我的问题是:1)当我按下按钮时,如何使球不同时移动;2) 当球到达他的上边界时,他直线移动。我应该如何改变我的代码,球是按对角线运动的;3) 我应该如何改变我的密码,球被棍子击退了 这是我的代码吗 import pygame,sys 从pygame.locals导入* pygame.init() “游戏屏幕” 尺寸=(600600) screen=pygame.display.set_模式(大小) pygame.display.set_标题(

我正在做pygame中的乒乓球克隆,我的球移动有困难。我的问题是:1)当我按下按钮时,如何使球不同时移动;2) 当球到达他的上边界时,他直线移动。我应该如何改变我的代码,球是按对角线运动的;3) 我应该如何改变我的密码,球被棍子击退了

这是我的代码吗

import pygame,sys
从pygame.locals导入*
pygame.init()
“游戏屏幕”
尺寸=(600600)
screen=pygame.display.set_模式(大小)
pygame.display.set_标题('ping_-pong')
“颜色”
海军蓝=[0,0,128]
白色=[255,255,255]
橙色=[255,165,0]
黑色=[0,0,0]
“变量”
y_移动1=267.5
y_移动2=267.5
x_速度=300
y_速度=300
gy=25
sp2=585
sp1=15
“功能”
def borders():
pygame.draw.rect(屏幕,海军蓝,(0,0600,gy))
pygame.draw.rect(屏幕,海军蓝,(060600,-gy))
def objects():
pygame.draw.rect(屏幕,橙色,(sp1,y_移动1,10,25))
pygame.draw.rect(屏幕,橙色,(sp2,y_移动2,-10,25))
pygame.draw.rect(屏幕,白色,(x_速度,y_速度,7.5,7.5))
“主回路”
clock=pygame.time.clock()
运行=真
运行时:
时钟滴答(15)
event=pygame.event.get()
边界()
对象()
x_速度+=5
y_速度-=5
如果y_速度300:
x_速度-=5
y_速度-=5
如果x_速度==sp1,y_速度<300:
x_速度+=5
y_速度+=5
如果x_速度==sp1,y_速度>300:
x_速度+=5
y_速度-=5
keys=pygame.key.get_pressed()
如果键[pygame.K_w]和y_movement1>25:
y_移动1-=6
如果键[pygame.K_s]和y_movement1<575-25-6:
y_移动1+=6
如果键[pygame.K_]和y_movement2>25:
y_移动2-=6
如果键[pygame.K_j]和y_movement2<550-25-6:
y_移动2+=6
屏幕填充(黑色)
边界()
对象()
对于事件中的事件:
如果event.type==pygame.QUIT:
运行=错误
sys.exit()
elif event.type==KEYDOWN和event.key==K_转义:
sys.exit()
其他:
pygame.display.flip()
pygame.quit()

以下是我对代码的更正版本:

import pygame

pygame.init()

''' game screen'''
size=(600,600)
screen=pygame.display.set_mode(size)
pygame.display.set_caption('ping_pong')

'''colors'''
navy_blue = [0, 0, 128]
white = [255, 255, 255]
orange = [255, 165, 0]
black = [0, 0, 0]

'''variables'''

y_movement1 = 267.5
y_movement2 = 267.5
x_velocity = 300
y_velocity = 300
gy = 25
sp2 = 585
sp1 = 15

'''functions'''
def borders():
    pygame.draw.rect(screen, navy_blue, (0, 0, 600, gy) )
    pygame.draw.rect(screen, navy_blue, (0, 600, 600, -gy))

def objects():
    pygame.draw.rect(screen, orange, (sp1, y_movement1, 10, 25))
    pygame.draw.rect(screen, orange, (sp2, y_movement2, -10, 25))
    pygame.draw.rect(screen, white, (x_pos, y_pos, 7.5, 7.5))

'''MAIN LOOP'''
clock = pygame.time.Clock()
running = True
x_pos = 300
y_pos = 300
x_velocity = 5
y_velocity = 2
while running:
    clock.tick(15)
    screen.fill(black)
    borders()
    objects()
    pygame.display.flip()
    events = pygame.event.get()
    if x_pos == sp1+10:
        if y_pos > y_movement1:
            if y_pos < y_movement1+25:
                x_velocity *= -1
    if x_pos == sp2-10:
        if y_pos > y_movement2:
            if y_pos < y_movement2+25:
                x_velocity *= -1
    if y_pos < gy or y_pos > 600-gy:
        y_velocity *= -1
    x_pos += x_velocity
    y_pos += y_velocity

    keys = pygame.key.get_pressed()

    if keys[pygame.K_w] and  y_movement1 > 25:
        y_movement1 -= 6
    if keys[pygame.K_s] and  y_movement1 < 575 - 25 -6 :
        y_movement1 += 6
    if keys[pygame.K_u] and y_movement2 > 25:
        y_movement2 -= 6
    if keys[pygame.K_j] and y_movement2 < 550 - 25 - 6:
        y_movement2 += 6

    for event in events:
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False

pygame.quit()
导入pygame
pygame.init()
“游戏屏幕”
尺寸=(600600)
screen=pygame.display.set_模式(大小)
pygame.display.set_标题('ping_-pong')
“颜色”
海军蓝=[0,0,128]
白色=[255,255,255]
橙色=[255,165,0]
黑色=[0,0,0]
“变量”
y_移动1=267.5
y_移动2=267.5
x_速度=300
y_速度=300
gy=25
sp2=585
sp1=15
“功能”
def borders():
pygame.draw.rect(屏幕,海军蓝,(0,0600,gy))
pygame.draw.rect(屏幕,海军蓝,(060600,-gy))
def objects():
pygame.draw.rect(屏幕,橙色,(sp1,y_移动1,10,25))
pygame.draw.rect(屏幕,橙色,(sp2,y_移动2,-10,25))
pygame.draw.rect(屏幕,白色,(x_位置,y_位置,7.5,7.5))
“主回路”
clock=pygame.time.clock()
运行=真
x_位置=300
y_位置=300
x_速度=5
y_速度=2
运行时:
时钟滴答(15)
屏幕填充(黑色)
边界()
对象()
pygame.display.flip()
events=pygame.event.get()
如果x_pos==sp1+10:
如果y_位置>y_移动1:
如果y_位置y_移动2:
如果y_位置600 gy:
y_速度*=-1
x_位置+=x_速度
y_位置+=y_速度
keys=pygame.key.get_pressed()
如果键[pygame.K_w]和y_movement1>25:
y_移动1-=6
如果键[pygame.K_s]和y_movement1<575-25-6:
y_移动1+=6
如果键[pygame.K_]和y_movement2>25:
y_移动2-=6
如果键[pygame.K_j]和y_movement2<550-25-6:
y_移动2+=6
对于事件中的事件:
如果event.type==pygame.QUIT:
运行=错误
elif event.type==pygame.KEYDOWN和event.key==pygame.K_转义:
运行=错误
pygame.quit()
编辑:

你用x和y的速度作为一个位置,这就是问题所在。应该将它们用作向量的x和y值。我纠正了这一点,现在这项运动运行良好。但是由于这个变化,佩德尔一家没有阻止球。我更改了碰撞检查,然后。。。完成了

附言: 我建议使用“随机”生成随机初始速度

编辑2:

这部分

if keys[pygame.K_w] and  y_movement1 > 25:
    y_movement1 -= 6
if keys[pygame.K_s] and  y_movement1 < 575 - 25 -6 :
    y_movement1 += 6
if keys[pygame.K_u] and y_movement2 > 25:
    y_movement2 -= 6
if keys[pygame.K_j] and y_movement2 < 550 - 25 - 6:
    y_movement2 += 6
如果键[pygame.K_w]和y_movement1>25:
y_移动1-=6
如果键[pygame.K_s]和y_movement1<575-25-6:
y_移动1+=6
如果键[pygame.K_]和y_movement2>25:
y_移动2-=6
如果键[pygame.K_j]和y_movement2<550-25-6:
y_移动2+=6
应该换成这个

if keys[pygame.K_w] and  y_movement1 > gy:
    y_movement1 -= 6
if keys[pygame.K_s] and  y_movement1 < 600-gy-25:
    y_movement1 += 6
if keys[pygame.K_u] and y_movement2 > gy:
    y_movement2 -= 6
if keys[pygame.K_j] and y_movement2 < 600-gy-25:
    y_movement2 += 6
if键[pygame.K_w]和y_movement1>gy:
y_移动1-=6
如果键[pygame.K_s]和y_移动1<600-gy-25:
y_移动1+=6
如果键[pygame.K_]和y_movement2>gy:
y_移动2-=6
如果键[pygame.K_j]和y_移动2<600-gy-25:
y_移动2+=6
以下是一个示例(py3)

导入pygame
pygame.init()
screen=pygame.display.set_模式((500500))
#白色
颜色=(255、255、255)
#按钮的灯罩
色光=(170170170)
#按钮的暗影
颜色_深=(100100100)
宽度=屏幕。获取宽度()
高度=屏幕。获取高度()
smallfont=pygame.font.SysFont('Corbel',35)
text=smallfont.render('qui
import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))

# white color
color = (255, 255, 255)
# light shade of the button
color_light = (170, 170, 170)
# dark shade of the button
color_dark = (100, 100, 100)

width = screen.get_width()
height = screen.get_height()

smallfont = pygame.font.SysFont('Corbel', 35)

text = smallfont.render('quit', True, color)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            # if the mouse is clicked on the
            # button the game is terminated
            if width / 2 <= mouse[0] <= width / 2 + 140 and height / 2 <= mouse[1] <= height / 2 + 40:
                pygame.quit()

                # fills the screen with a color
    screen.fill((255, 128, 128))
    # stores the (x,y) coordinates into
    # the variable as a tuple
    mouse = pygame.mouse.get_pos()
    # if mouse is hovered on a button it
    # changes to lighter shade
    if width / 2 <= mouse[0] <= width / 2 + 140 and height / 2 <= mouse[1] <= height / 2 + 40:
        pygame.draw.rect(screen, color_light, (int(width / 2), int(height / 2), 140, 40))

    else:
        pygame.draw.rect(screen, color_dark, (int(width / 2), int(height / 2), 140, 40))

    screen.blit(text, (int(width / 2) + 50, int(height / 2)))
    pygame.display.update()