python - 如何在pygame游戏窗口的角落添加健康分数
问题描述
我一直在关注 pygame 中的教程。最后它有一个“如何”页面。我按照如何在屏幕上添加文本的方法进行操作。我的意图是当玩家击中另一个精灵时,它会失去一些生命值——当生命值达到 0 时,游戏就会结束。我见过其他问题,但似乎没有任何效果。文字需要显示在游戏窗口的右上角。我当前的代码:
import PySimpleGUI as sg
hp = 0
import pygame, random
#Let's import the Car Class
from player import Car
pygame.init()
def play():
font = pygame.font.Font(None, 100)
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
BLUE = (100, 100, 255)
speed = 1
colorList = (RED, GREEN, PURPLE, YELLOW, CYAN, BLUE)
SCREENWIDTH=800
SCREENHEIGHT=600
lack_bar = pygame.Surface((SCREENWIDTH, 35))
size = (SCREENWIDTH, SCREENHEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Car Racing")
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()
playerCar = Car(RED, 60, 80, 70)
playerCar.rect.x = 160
playerCar.rect.y = SCREENHEIGHT - 100
car1 = Car(PURPLE, 60, 80, random.randint(50,100))
car1.rect.x = 60
car1.rect.y = -100
car2 = Car(YELLOW, 60, 80, random.randint(50,100))
car2.rect.x = 160
car2.rect.y = -600
car3 = Car(CYAN, 60, 80, random.randint(50,100))
car3.rect.x = 260
car3.rect.y = -300
car4 = Car(BLUE, 60, 80, random.randint(50,100))
car4.rect.x = 360
car4.rect.y = -900
# Add the car to the list of objects
all_sprites_list.add(playerCar)
all_sprites_list.add(car1)
all_sprites_list.add(car2)
all_sprites_list.add(car3)
all_sprites_list.add(car4)
all_coming_cars = pygame.sprite.Group()
all_coming_cars.add(car1)
all_coming_cars.add(car2)
all_coming_cars.add(car3)
all_coming_cars.add(car4)
#Allowing the user to close the window...
carryOn = True
clock=pygame.time.Clock()
while carryOn:
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
end()
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_x:
playerCar.moveRight(10)
#Check if there is a car collision
car_collision_list = pygame.sprite.spritecollide(playerCar,all_coming_cars,False)
for car in car_collision_list:
carryOn=False
end()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
playerCar.moveLeft(5)
if keys[pygame.K_RIGHT]:
playerCar.moveRight(5)
if keys[pygame.K_UP]:
speed += 0.05
if keys[pygame.K_DOWN]:
speed -= 0.05
#Game Logic
for car in all_coming_cars:
car.moveForward(speed)
if car.rect.y > SCREENHEIGHT:
car.changeSpeed(random.randint(50,100))
car.repaint(random.choice(colorList))
car.rect.y = -200
all_sprites_list.update()
#Drawing on Screen
screen.fill(GREEN)
#Draw The Road
pygame.draw.rect(screen, GREY, [40,0, 400,SCREENHEIGHT])
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [140,0],[140,SCREENHEIGHT],5)
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [240,0],[240,SCREENHEIGHT],5)
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [340,0],[340,SCREENHEIGHT],5)
#Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
all_sprites_list.draw(screen)
#Refresh Screen
pygame.display.flip()
#Number of frames per secong e.g. 60
clock.tick(60)
def end():
layout = [[sg.Text("Car Crash"), sg.Button("Try Again"), sg.Button("End")]]
window = sg.Window("Car Crash!!!", layout)
while True:
event, values = window.read()
if event == "End":
window.close()
pygame.quit()
if event == "Try Again":
window.close()
play()
play()
如果需要这里也是我的精灵类:
import pygame
WHITE = (255, 255, 255)
class Car(pygame.sprite.Sprite):
#This class represents a car. It derives from the "Sprite" class in Pygame.
def __init__(self, color, width, height, speed):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the car, and its x and y position, width and height.
# Set the background color and set it to be transparent
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
#Initialise attributes of the car.
self.width=width
self.height=height
self.color = color
self.speed = speed
# Draw the car (a rectangle!)
pygame.draw.rect(self.image, self.color, [0, 0, self.width, self.height])
# Instead we could load a proper picture of a car...
# self.image = pygame.image.load("car.png").convert_alpha()
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveForward(self, speed):
self.rect.y += self.speed * speed / 20
def moveBackward(self, speed):
self.rect.y -= self.speed * speed / 20
def changeSpeed(self, speed):
self.speed = speed
def repaint(self, color):
self.color = color
pygame.draw.rect(self.image, self.color, [0, 0, self.width, self.height])
我知道这很有可能是重复的,但是有人可以给我一个特定于我的代码的答案吗?
解决方案
而不是你的font
线插入这个
game_font_1 = pg.font.SysFont("Arial", 18, 1, 1)
然后你可以使用.render()
方法:
text_1 = game_font_1,render("your_text_here", False, pg.Color("color_name"))
最后,您可以在屏幕(窗口)上对其进行 blit:
window.blit(text_1, (x, y))
不要忘记放入循环!
推荐阅读
- post - Post Widget elementor Pro 上的巨大空间。请看图片
- android - kotlin android rooom 如何从带有编辑文本的列中计算数据?
- batch-file - 尝试禁用在 AD 中处于非活动状态超过 X 时间的用户
- python - Python pip - 从本地目录安装特定的包版本
- socket.io - 向房间内命名空间中的所有套接字发送事件,发送者除外
- python - 如何查找 id 具有前缀“Translations_”的元素?
- vue.js - 使用 axios 将外部 api 代理到 nuxtjs / vuejs devserver
- typescript - 打字稿记录,无需手动键入键
- javascript - 如何在 svelte 文件中使用电子方法 - Svelte 3 - 还是有其他方法可以做到这一点?
- algorithm - 水合节点