【问题标题】:how to add a stopwatch to pygame?如何在pygame中添加秒表?
【发布时间】:2020-05-25 02:16:56
【问题描述】:

我正在制作一个关于下落圆圈的游戏,玩家,基本上是一个正方形,必须避开它们。代码差不多完成了,但是我对如何在窗口上的游戏中添加秒表有疑问?我不知道在哪里放置这种类型的代码有人可以帮助我吗?除了秒表之外,我已经完成了所有代码。

import pygame
from pygame.locals import *
import os
import random
import math
import sys
import time

white = (255,255,255)
blue = (0,0,255)
gravity = 10
size =10
height = 500
width =600
varHeigth = height
ballNum = 5
eBall = []

apGame = pygame.display.set_mode((width, height))
pygame.display.set_caption("AP Project")

clock = pygame.time.Clock()

class Player(object):

  def __init__(self):
    red = (255, 0, 0)
    move_x = 300
    move_y = 400
    self.rect = pygame.draw.rect(apGame,red, (move_x, move_y, 10, 10))
    self.dist = 10

  def handle_keys(self):
    for e in pygame.event.get():
      if e.type == pygame.QUIT:
        pygame.quit();
        exit()
    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
      self.draw_rect(-1, 0)
    elif key[pygame.K_RIGHT]:
      self.draw_rect(1, 0)
    elif key[pygame.K_ESCAPE]:
      pygame.quit();
      exit()
    else:
      self.draw_rect(0, 0)

  def draw_rect(self, x, y):
    red = (255, 0, 0)
    black = (0, 0, 0)
    '''apGame.fill(black)'''
    self.rect = self.rect.move(x * self.dist, y * self.dist);
    pygame.draw.rect(apGame, red , self.rect)
    pygame.display.update()


  def draw(self,surface):
    red = (255, 0, 0)
    move_x = 300
    move_y = 400
    pygame.draw.rect(apGame, red, (move_x, move_y, 10, 10))

def show_go_screen():
  pygame.font.init()
  myfont = pygame.font.SysFont("Comic Sans MS", 30)
  label = myfont.render("Game Over", 1, red)
  apGame.blit(label, (300,100))


def instuct():
  pygame.font.init()
  myfont = pygame.font.SysFont("Comic Sans MS", 15)
  label = myfont.render("Avoid The Circles", 1, red)
  apGame.blit(label, (250,450))


move_x = 300
move_y = 400
red = (255, 0, 0)
black = (0, 0, 0)
player = Player()
clock = pygame.time.Clock()
'''apGame.fill(black)'''
player.draw(apGame)
pygame.display.update()

for q in range(ballNum):
  x = random.randrange(0, width)
  y = random.randrange(0, varHeigth)
  eBall.append([x, y])

while True:

  apGame.fill(black)


  for i in range(len(eBall)):

    ball_rect = pygame.draw.circle(apGame, blue, eBall[i], size)

    if player.rect.colliderect(ball_rect):
      show_go_screen()
      break


    eBall[i][1] += 5

    if eBall[i][1] > height:

        y = random.randrange(-50, -10)
        eBall[i][1] = y

        x = random.randrange(0, width)
        eBall[i][0] = x



  instuct()
  player.handle_keys()
  pygame.display.flip()
  clock.tick(30)

【问题讨论】:

  • “我不知道把这种类型的代码放在哪里”好吧,你是如何决定把代码的所有其他部分放在哪里的?
  • stopwatch 就像你的 Player 但它改变了价值而不是移动。所以你可以用__init__drawupdate创建类似的类,并在类似的地方使用这些方法,比如Player。在每个循环中,您都必须使用 update 检查是否需要更改值,并使用 draw 显示当前值。

标签: python python-3.x pygame


【解决方案1】:

首先不要不断的初始化font模块,也不要每次显示文本时都生成pygame.font.Font对象。那是资源和性能的浪费。在初始化时执行一次:

import pygame
from pygame.locals import *
# [...]

pygame.init()
pygame.font.init()

myfont15 = pygame.font.SysFont("Comic Sans MS", 15)
myfont30 = pygame.font.SysFont("Comic Sans MS", 30)

当需要显示文本时使用字体对象:

def show_go_screen():
  label = myfont30.render("Game Over", 1, red)
  apGame.blit(label, (300,100))


def instuct():
  label = myfont15.render("Avoid The Circles", 1, red)
  apGame.blit(label, (250,450))

编写一个可以显示时间的函数,例如精确到十分之一秒:

def display_time(time_s):
  # time string with tents of seconds
  time_str =  str(int(time_s*10) / 10)  
  label = myfont15.render(f"Time : {time_str}", 1, red)
  apGame.blit(label, (20, 20))

pygame.time.Clock.tick 返回自上一帧以来经过的确切时间,以毫秒为单位。计算时间并使用函数显示时间:

time_seconds = 0
while True:

  apGame.fill(black)

  game_over = False
  for i in range(len(eBall)):

    ball_rect = pygame.draw.circle(apGame, blue, eBall[i], size)
    if player.rect.colliderect(ball_rect):
      show_go_screen()
      game_over = True
      break

    eBall[i][1] += 5
    if eBall[i][1] > height:
        y = random.randrange(-50, -10)
        eBall[i][1] = y
        x = random.randrange(0, width)
        eBall[i][0] = x

  instuct()
  display_time(time_seconds)
  player.handle_keys()
  pygame.display.flip()

  time_millis = clock.tick(30)
  if not game_over:
      time_seconds += time_millis / 1000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多