如何在pygame中添加秒表? [英] how to add a stopwatch to pygame?

查看:60
本文介绍了如何在pygame中添加秒表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

导入pygame从 pygame.locals 导入 *导入操作系统随机导入导入数学导入系统导入时间白色 = (255,255,255)蓝色 = (0,0,255)重力 = 10尺寸 =10高度 = 500宽度=600varHeigth = 高度球数 = 5电子球 = []apGame = pygame.display.set_mode((width, height))pygame.display.set_caption("AP 项目")时钟 = pygame.time.Clock()类播放器(对象):def __init__(self):红色 = (255, 0, 0)移动_x = 300移动_y = 400self.rect = pygame.draw.rect(apGame,red, (move_x, move_y, 10, 10))self.dist = 10def handle_keys(self):对于 pygame.event.get() 中的 e:如果 e.type == pygame.QUIT:pygame.quit();出口()键 = pygame.key.get_pressed()如果键[pygame.K_LEFT]:self.draw_rect(-1, 0)elif 键 [pygame.K_RIGHT]:self.draw_rect(1, 0)elif 键 [pygame.K_ESCAPE]:pygame.quit();出口()别的:self.draw_rect(0, 0)def draw_rect(self, x, y):红色 = (255, 0, 0)黑色 = (0, 0, 0)'''apGame.fill(黑色)'''self.rect = self.rect.move(x * self.dist, y * self.dist);pygame.draw.rect(apGame,红色,self.rect)pygame.display.update()def draw(self,surface):红色 = (255, 0, 0)移动_x = 300移动_y = 400pygame.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("游戏结束", 1, red)apGame.blit(标签,(300,100))定义指令():pygame.font.init()myfont = pygame.font.SysFont("Comic Sans MS", 15)label = myfont.render("避开圆圈", 1, red)apGame.blit(标签,(250,450))移动_x = 300移动_y = 400红色 = (255, 0, 0)黑色 = (0, 0, 0)玩家 = 玩家()时钟 = pygame.time.Clock()'''apGame.fill(黑色)'''player.draw(apGame)pygame.display.update()对于范围内的 q(ballNum):x = random.randrange(0, 宽度)y = random.randrange(0, varHeigth)eBall.append([x, y])为真:apGame.fill(黑色)对于我在范围内(len(eBall)):ball_rect = pygame.draw.circle(apGame,蓝色,eBall[i],大小)如果 player.rect.colliderect(ball_rect):show_go_screen()休息电子球[i][1] += 5如果 eBall[i][1] >高度:y = random.randrange(-50, -10)电子球[i][1] = yx = random.randrange(0, 宽度)电子球[i][0] = x指导()player.handle_keys()pygame.display.flip()时钟滴答(30)

解决方案

首先不要连续初始化

I am making a game about falling circles and the player, which is basically a square, has to avoid them. The code is almost done but I have a problem with how to add a stopwatch to the game on the window? I have no idea where to put this type of code can anyone help me? I have all of the code done except for the stopwatch.

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)

解决方案

First of all do not continuously initialize the font module, and do not generate the pygame.font.Font objects every time when you display a text. That is wast of resources and performance. Do it once at initialization:

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)

Use the font objects when a text has to be displayed:

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))

Write a function which can display the time, for instance with the accuracy of tenths of a second:

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))

The exact time time which has passed since the previous frame is returned by pygame.time.Clock.tick in milliseconds. Count the time and use the function to display the time:

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

这篇关于如何在pygame中添加秒表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆