pygame中的乒乓球

时间:2019-03-17 17:40:08

标签: python python-3.x pygame

我正在pygame中制作一个pong的克隆游戏,我在移动球时遇到了麻烦。我的问题是:1)当我按下按钮时,如何使我的球不同时移动; 2)当球到达上边界时,他直线移动。我该如何更改代码,使球按对角线移动; 3)我应该如何更改我的代码,即从棍子击退的那个球?

这是我的代码

import pygame, sys

from pygame.locals import  *

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_velocity, y_velocity, 7.5, 7.5))

'''MAIN LOOP'''
clock = pygame.time.Clock()
running = True
while running:
    clock.tick(15)

    event = pygame.event.get()

    borders()
    objects()

    x_velocity += 5
    y_velocity -= 5

    if y_velocity < gy:
        x_velocity += 5
        y_velocity += 5
    if x_velocity == 600:
        x_velocity = 300
        y_velocity = 300
    if x_velocity == 0:
        x_velocity = 300
        y_velocity = 300
    if x_velocity + 7.5 == sp2 and y_velocity < 300:
        x_velocity -= 5
        y_velocity += 5
    if x_velocity + 7.5 ==  sp2 and y_velocity > 300:
        x_velocity -= 5
        y_velocity -= 5
    if x_velocity == sp1 and y_velocity < 300:
        x_velocity += 5
        y_velocity += 5
    if x_velocity == sp1 and y_velocity > 300:
        x_velocity += 5
        y_velocity -= 5


    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

    screen.fill(black)

    borders()
    objects()

    for event in event:
        if event.type == pygame.QUIT:
            running = False
            sys.exit()
        elif event.type == KEYDOWN and event.key == K_ESCAPE:
            sys.exit()
        else:
            pygame.display.flip( )

pygame.quit()

2 个答案:

答案 0 :(得分:1)

这是我正确的代码版本:

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()

编辑:

您使用x和y速度作为位置,这就是问题所在。您应该将它们用作向量的x和y值。我更正了这一点,现在机芯工作正常。但是由于这种变化,小贩们并没有阻止球。我更改了碰撞检查并完成了!

PS: 我建议使用random来生成随机的初始速度。

edit2:

这部分

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

应替换为此

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

答案 1 :(得分:0)

这是一个示例(py3)

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()