我正在尝试制作乒乓球游戏,但是当球离开屏幕时,比分不会改变。另一件不太对的事情是,有时,球在球拍上滑动,然后离开屏幕,而不是从球拍上反弹。我能就这些问题获得一些帮助吗?(主要是第一个)这是我的代码:
import pygame as pg
import random
from os import path
img_dir = path.join(path.dirname(__file__), 'img')
snd_dir = path.join(path.dirname(__file__), 'snd')
WIDTH = 1280
HEIGHT = 690
FPS = 60
# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (0, 255, 255)
OPPONENT_SPEED = 2.4
# initialize PyGame and create window
pg.init()
pg.mixer.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption('PONG!')
clock = pg.time.Clock()
pong_ball_x = [-3, 3]
pong_ball_y = [-3, 3]
font_name = pg.font.match_font('arial')
def draw_text(surf, text, size, x, y):
font = pg.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((5, 100))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = 10
self.rect.y = HEIGHT / 2
self.speedy = 0
def update(self):
self.speedy = 0
keys = pg.key.get_pressed()
if keys[pg.K_s]:
self.speedy = 5
if keys[pg.K_w]:
self.speedy = -5
self.rect.y += self.speedy
if self.rect.bottom >= HEIGHT:
self.rect.bottom = HEIGHT
if self.rect.top <= 0:
self.rect.top = 0
class Player2(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((5, 100))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = WIDTH - 15
self.rect.y = HEIGHT / 2
self.speedy = 0
森林海
相关分类