我正在尝试在游戏开始时创建一个屏幕,当我按任意键时,它将激活我的游戏循环。我得到了大约80%的屏幕工作我只是无法得到它的最后一个。我只想尝试一旦通过起始屏幕,第一级将激活。非常感谢任何帮助/输入。谢谢。主要关注领域是Player
类中的3个星号。方法wait
和level
import pygame, sys
from pygame.locals import *
pygame.init()
SCR_WID, SCR_HEI = 640, 480
LEVEL = 1
hit = pygame.mixer.Sound('pickup.wav')
pygame.mixer.music.load('play.wav')
pygame.mixer.music.play(-1, 0.0)
musicPlaying = True
class Player():
def __init__(self):
self.p3, self.p4 = SCR_WID/2, 16
self.p5, self.p6 = SCR_WID/2, 464
self.speed = 3
self.padWid, self.padHei = 8, 128
self.padWid3, self.padHei4 = 128, 8
self.score = 0
self.scoreFont = pygame.font.Font("imagine_font.ttf", 64)
def music(self):
global hit
hit = pygame.mixer.Sound('pickup.wav')
pygame.mixer.music.load('play.wav')
pygame.mixer.music.play(-1, 0.0)
global musicPlaying
musicPlaying = True
def scoring(self):
scoreBlit = self.scoreFont.render(str(self.score), 1, (0,0,0))
screen.blit(scoreBlit, (32, 16))
if self.score == 15:
print ("player 1 wins!")
exit()
def scoring1(self):
scoreBlit = self.scoreFont.render(str(self.score), 1, (0,0,0))
screen.blit(scoreBlit, (SCR_HEI+92, 16))
if self.score == 15:
print ("Player 2 wins!")
exit()
***def wait(self):
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
player.level(player, enemy, ball)
return
def level(self, player, enemy, ball):
while LEVEL < 4:
font = pygame.font.SysFont("Drawing with markers", 48)
text = font.render("Pong", 1, (255, 100, 100))
text1 = font.render("Press a key to start", 1, (255, 100, 100))
screen.blit(text, (250,190))
screen.blit(text1, (133,270))
pygame.display.flip()
player.wait()
if LEVEL == 1:
self.x, self.y = 16, SCR_HEI/2
self.x1, self.y1 = SCR_WID-16, SCR_HEI/2***
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.y -= self.speed
elif keys[pygame.K_s]:
self.y += self.speed
if self.y <= 0:
self.y = 0
elif self.y >= SCR_HEI-128:
self.y = SCR_HEI-128
keys1 = pygame.key.get_pressed()
if keys1[pygame.K_UP]:
self.y1 -= self.speed
elif keys1[pygame.K_DOWN]:
self.y1 += self.speed
if self.y1 <= 0:
self.y1 = 0
elif self.y >= SCR_HEI-128:
self.y1 = SCR_HEI-128
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.p3 -= self.speed
elif keys[pygame.K_d]:
self.p3 += self.speed
if self.p3 <= 0:
self.p3 = 0
elif self.p3 >= SCR_WID-128:
self.p3 = SCR_WID-128
keys1 = pygame.key.get_pressed()
if keys1[pygame.K_LEFT]:
self.p5 -= self.speed
elif keys1[pygame.K_RIGHT]:
self.p5 += self.speed
if self.p5 <= 0:
self.p5 = 0
elif self.p5 >= SCR_WID-128:
self.p5 = SCR_WID-128
def draw(self):
pygame.draw.rect(screen, (0,0,0), (self.x, self.y, self.padWid, self.padHei))
pygame.draw.rect(screen, (0,0,0), (self.x1, self.y1, self.padWid, self.padHei))
pygame.draw.rect(screen, (0,0,0), (self.p3, self.p4, self.padWid3, self.padHei4))
pygame.draw.rect(screen, (0,0,0), (self.p5, self.p6, self.padWid3, self.padHei4))
class Ball():
def __init__(self):
self.x, self.y = SCR_WID/2, SCR_HEI/2
self.x1, self.y1 = SCR_WID/2, SCR_HEI/2
self.p3, self.p4 = SCR_WID/2, SCR_HEI/2
self.p5, self.p6 = SCR_WID/2, SCR_HEI/2
self.speed_x = -2
self.speed_y = 2
self.size = 12
def movement(self, player, enemy):
self.x += self.speed_x
self.y += self.speed_y
if self.y <= 0:
self.speed_y *= -1
elif self.y >= SCR_HEI-self.size:
self.speed_y *= -1
if self.x <= 0:
self.__init__()
enemy.score += 1
if musicPlaying:
hit.play()
elif self.x >= SCR_WID-self.size:
self.__init__()
self.speed_x = 3
player.score += 1
if musicPlaying:
hit.play()
for n in range(-self.size, player.padHei):
if self.y == player.y + n:
if self.x <= player.x + player.padWid:
self.speed_x *= -1
break
n += 1
self.x1 += self.speed_x
self.y1 += self.speed_y
if self.y1 <= 0:
self.speed_y *= -1
elif self.y1 >= SCR_HEI-self.size:
self.speed_y *= -1
if self.x1 <= 0:
self.__init__()
enemy.score += 1
if musicPlaying:
hit.play()
elif self.x1 >= SCR_WID-self.size:
self.__init__()
self.speed_x = 3
player.score += 1
if musicPlaying:
hit.play()
for n in range(-self.size, enemy.padHei):
if self.y1 == enemy.y1 + n:
if self.x1 >= enemy.x1 - enemy.padWid:
self.speed_x *= -1
break
n += 1
self.p3 += self.speed_x
self.p4 += self.speed_y
if self.p4 <= 0:
self.speed_x *= -1
elif self.p4 >= SCR_HEI-self.size:
self.speed_x *= -1
if self.p3 <= 0:
self.__init__()
enemy.score += 1
elif self.p3 >= SCR_WID-self.size:
self.__init__()
self.speed_y = 3
player.score += 1
for n in range(-self.size, enemy.padHei4):
if self.p4 == enemy.p4 + n:
if self.p3 >= enemy.p3 - enemy.padWid3:
self.speed_y *= -1
break
n += 1
self.p5 += self.speed_x
self.p6 += self.speed_y
if self.p6 <= 0:
self.speed_x *= -1
elif self.p6 >= SCR_HEI-self.size:
self.speed_x *= -1
if self.p5 <= 0:
self.__init__()
enemy.score += 1
elif self.p5 >= SCR_WID-self.size:
self.__init__()
self.speed_y = 3
player.score += 1
for n in range(-self.size, enemy.padHei4):
if self.p6 == enemy.p6 + n:
if self.p5 >= enemy.p5 - enemy.padWid3:
self.speed_y *= -1
break
n += 1
def draw(self):
pygame.draw.rect(screen, (112,138,144), (self.x, self.y, 12, 12))
screen = pygame.display.set_mode((SCR_WID, SCR_HEI))
player = Player()
ball = Ball()
enemy = Player()
##level = level(score)
def main():
## player.music()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print ("Game exited by user")
sys.exit()
else:
False
pygame.font.init()
backgroundimage = pygame.image.load("background.png")
pygame.display.set_caption("Justin's pong")
player.level(player, enemy, ball)
ball.movement(player, enemy)
player.movement()
enemy.movement()
position = (0,0)
screen.fill((0, 0, 0))
screen.blit(backgroundimage, position)
ball.draw()
player.draw()
player.scoring()
enemy.draw()
enemy.scoring1()
pygame.display.flip()
clock = pygame.time.Clock()
FPS = 60
clock.tick(FPS)
player.music()
main()
答案 0 :(得分:1)
你必须写一个函数:
def intro():
smallfont = pygame.font.SysFont(None, 30)
while intro == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
intro = False
gameDisplay.fill(white)
text = smallfont.render("press any key to continue",True , (0, 0, 0))
gameDisplay.blit(text, [320,240])
pygame.display.update()
clock.tick(15)
intro()
main()