如何解决如何在pygame中实现重启功能?
import pygame
from pygame.locals import *
pygame.init()
screen_width = 600
screen_height = 500
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('Pong')
#define font
font = pygame.font.SysFont('None',30)
#define game variables
margin = 50
cpu_score = 0
player_score = 0
fps = 90
live_ball = False
winner = 0
speed_increase = 0
#define colours
bg = (12,91)
white = (241,185,0)
ball_color = (168,43,17)
bar_color = (10,106,86)
def draw_board():
screen.fill(bg)
pygame.draw.line(screen,white,(0,margin),(screen_width,2)
def draw_text(text,font,text_col,x,y):
img = font.render(text,True,text_col)
screen.blit(img,(x,y))
class paddle():
def __init__(self,y):
self.x = x
self.y = y
self.rect = Rect(x,y,20,100)
self.speed = 5
self.ai_speed = 5
def move(self):
key = pygame.key.get_pressed()
if key[pygame.K_UP] and self.rect.top > margin:
self.rect.move_ip(0,-1 * self.speed)
if key[pygame.K_DOWN] and self.rect.bottom < screen_height:
self.rect.move_ip(0,self.speed)
def draw(self):
pygame.draw.rect(screen,bar_color,self.rect)
def ai(self):
#ai to move the paddle automatically
#move down
if self.rect.centery < pong.rect.top and self.rect.bottom < screen_height:
self.rect.move_ip(0,self.ai_speed)
#move up
if self.rect.centery > pong.rect.bottom and self.rect.top > margin:
self.rect.move_ip(0,-1 * self.ai_speed)
class ball():
def __init__(self,y):
self.reset(x,y)
def move(self):
#check collision with top margin
if self.rect.top < margin:
self.speed_y *= -1
#check collision with bottom of the screen
if self.rect.bottom > screen_height:
self.speed_y *= -1
if self.rect.colliderect(player_paddle) or self.rect.colliderect(cpu_paddle):
self.speed_x *= -1
#check for out of bounds
if self.rect.left < 0:
self.winner = 1
if self.rect.left > screen_width:
self.winner = -1
#update ball position
self.rect.x += self.speed_x
self.rect.y += self.speed_y
return self.winner
def draw(self):
pygame.draw.circle(screen,ball_color,(self.rect.x + self.ball_rad,self.rect.y + self.ball_rad),self.ball_rad)
def reset(self,y):
self.x = x
self.y = y
self.ball_rad = 8
self.rect = Rect(x,self.ball_rad * 2,self.ball_rad * 2)
self.speed_x = -4
self.speed_y = 4
self.winner = 0# 1 is the player and -1 is the cpu
#create paddles
player_paddle = paddle(screen_width - 40,screen_height // 2)
cpu_paddle = paddle(20,screen_height // 2)
#create pong ball
pong = ball(screen_width - 60,screen_height // 2 + 50)
#create game loop
run = True
while run:
fpsClock.tick(fps)
draw_board()
draw_text('cpu: ' + str(cpu_score),15)
draw_text('Sihu: ' + str(player_score),screen_width - 100,15)
draw_text('BALL SPEED: ' + str(abs(pong.speed_x)),screen_width // 2 - 100,15)
#draw paddles
player_paddle.draw()
cpu_paddle.draw()
if live_ball == True:
speed_increase += 1
winner = pong.move()
if winner == 0:
#draw ball
pong.draw()
#move paddles
player_paddle.move()
cpu_paddle.ai()
else:
live_ball = False
if winner == 1:
player_score += 1
elif winner == -1:
cpu_score += 1
#print player instructions
if live_ball == False:
if winner == 0:
draw_text('CLICK ANYWHERE TO START',160,screen_height // 2 -0)
if winner == 1:
draw_text('YOU scoreD!',220,screen_height // 2 -100)
draw_text('CLICK ANYWHERE TO START',screen_height // 2 -0)
if winner == -1:
draw_text('cpu scoreD!',screen_height // 2 -0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN and live_ball == False:
live_ball = True
pong.reset(screen_width - 60,screen_height // 2 + 50)
if speed_increase > 500:
speed_increase = 0
if pong.speed_x < 0:
pong.speed_x -= 1
if pong.speed_x > 0:
pong.speed_x += 1
if pong.speed_y < 0:
pong.speed_y -= 1
if pong.speed_y > 0:
pong.speed_y += 1
pygame.display.update()
pygame.quit()
我在YOUTUBE上得到了代码 并稍微编辑一下代码 但我没有添加重启代码 如何在pygame中实现重启功能? 当玩家在游戏中获得 5 分时,我只想结束游戏。 并想让游戏重新开始。 游戏结束后,玩家可以通过按钮重新开始游戏。
解决方法
让循环拥有自己的方法,从 main()
运行中调用它,当游戏结束并结束时,让 main()
询问用户是否想再次玩,如果是,请回想将所有内容设置为默认值后的主循环
或
只需将再次询问的部分清除并重新初始化所有变量作为默认值,如果用户想要再次播放,则让循环
,要重新开始,您需要重置在游戏过程中发生变化的所有变量。编写一个将所有对象和变量设置为其初始状态的函数:
def reset():
global player_paddle,cpu_paddle,pong
global cpu_score,player_score
global live_ball,winner,speed_increase
player_paddle = paddle(screen_width - 40,screen_height // 2)
cpu_paddle = paddle(20,screen_height // 2)
pong = ball(screen_width - 60,screen_height // 2 + 50)
cpu_score = 0
player_score = 0
live_ball = False
winner = 0
speed_increase = 0
当您想重新启动游戏时调用该函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。