我正在尝试为我的第一个 Pygame 游戏创建一个乒乓球游戏。我对 Pygame 很陌生,对 Python 也比较陌生。到目前为止,我只有两侧的两个保险杠。这是我到目前为止的代码:
import pygame, sys, os
from pygame.locals import *
from multiprocessing import Process
window = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pong")
screen = pygame.display.get_surface()
pongBackground = pygame.image.load("C:/Users/LN/Desktop/PongBackground.png")
pongHitter = pygame.image.load("C:/Users/LN/Desktop/PongHitter.png")
pongHitter = pygame.image.load("C:/Users/LN/Desktop/PongHitter.png")
pongBall = pygame.image.load("C:/Users/LN/Desktop/PongBall.png")
pygame.init()
class hitter():
"""moves the pong hitter with arrow keys"""
def _init_(self):
screen.blit(pongBackground, (0,0))
self.position = pongHitter.get_rect()
pygame.display.update()
def goTo(self, coord):
screen.blit(pongHitter,coord)
self.position = self.position.move(coord)
pygame.display.update()
def moveUp(self):
for i in range(1):
screen.blit(pongBackground, self.position, self.position)
self.position = self.position.move(0, -1)
screen.blit(pongHitter, self.position)
pygame.display.update()
pygame.time.delay(10)
def moveDown(self):
for i in range(1):
screen.blit(pongBackground, self.position, self.position)
self.position = self.position.move(0, 1)
screen.blit(pongHitter, self.position)
pygame.display.update()
pygame.time.delay(10)
hitterA=hitter()
hitterB=hitter()
hitterA._init_()
hitterB._init_()
hitterA.goTo((5,200))
hitterB.goTo((590,200))
pygame.key.set_repeat(1,1)
contin=True
def inputLeft():
while True:
for event in pygame.event.get():
if pygame.key.get_pressed()[K_UP]:
hitterA.moveUp()
elif pygame.key.get_pressed()[K_DOWN]:
hitterA.moveDown()
elif pygame.key.get_pressed()[K_ESCAPE]:
sys.exit(0)
def inputRight():
while True:
for event in pygame.event.get():
if pygame.key.get_pressed()[K_w]:
hitterB.moveUp()
elif pygame.key.get_pressed()[K_s]:
hitterB.moveDown()
elif pygame.key.get_pressed()[K_ESCAPE]:
sys.exit(0)
if __name__=='__main__':
p = Process(target=inputLeft)
p.start()
p.join()
p2 = Process(target=inputRight)
p2.start()
p2.join()
现在我正试图让两个保险杠可以同时移动。正如您在上面看到的,我已经尝试过多重处理,但它会为每个进程创建不同的窗口。如何让所有内容都显示在一个窗口上?谢谢!顺便说一下,这个python 3.3。