这是我的游戏代码。
import pygame
import os
import random
from pygame.locals import * # Constants
import math
import sys
import random
pygame.init()
screen=pygame.display.set_mode((1280,700)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background = pygame.image.load('stage.png').convert()
Black=(0,0,0)
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50,25))
self.image.fill((0,0,128))
self.image = pygame.image.load('sprite4.png')
self.rect = self.image.get_rect(topleft =(20,615))
self.dist = 10
def update(self): # code to make the character move when the arrow keys are pressed
if self.rect.right > 100: # These are to make the player so move constantly
self.rect.y += 1
self.rect.x += 2
if self.rect.bottom == 700:
pygame.quit()
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.rect.move_ip(-1,0)
elif keys[K_RIGHT]:
self.rect.move_ip(0.5,0)
elif keys[K_UP]:
self.rect.move_ip(0,-0.5)
elif keys[K_DOWN]:
self.rect.move_ip(0,1)
self.rect.clamp_ip(screen_rect)
#while self.rect == (20,615):
if keys [K_SPACE]:
self.rect = self.image.get_rect(topleft =(100,100))
player = Player()
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50,25)) # This creates the surafce with the width and length given
self.image.fill((128,0,0)) # This fills the surface with colour
self.rect = self.image.get_rect(topleft=(300, 50)) # This is to make the image into a rectangle
self.direction = 0
def update(self):
self.rect.y += 1 if self.direction == 0 else -1
if self.rect.bottom >= 600:
self.direction = 1
if self.rect.top <= 50:
self.direction = 0
class Coin(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load('coin.png')
self.rect = self.image.get_rect(topleft=(250, 200))
number_of_enemies = 10
min_enemy_x = 200
max_enemy_x = 1200
enemy_x_range = max_enemy_x - min_enemy_x
enemy_zone_width = enemy_x_range / number_of_enemies
pixel_buffer = 40
for i in range (number_of_enemies):
enemy = Enemy() # calls the enemy class
min_x = min_enemy_x + enemy_zone_width * i + pixel_buffer / 2
max_x = min_enemy_x + enemy_zone_width * (i + 1) - pixel_buffer / 2
enemy.rect.x = random.randrange(min_x, max_x) # makes the enemy spawn random
enemy.rect.y = random.randrange(50, 600)
enemy_list.add(enemy) # adds the enemy to the group
sprites.add(enemy)
for i in range (10): # creates 5 enemy spirtes
coin = Coin() # calls the coin class
coin.rect.x = random.randrange(200, 1200, 70) # makes the coin spawn random
coin.rect.y = random.randrange(300, 600)
coin_list.add(coin) # adds the enemy to the group
sprites.add(coin)
def main(): #my main loop
running = True
clock = pygame.time.Clock()
score = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sprites.update()
screen.blit(background, (0, 0))
sprites.draw(screen)
clock.tick(100) # Limit the frame rate to 60 FPS.
pygame.display.flip() #updates the whole screen
#Collison check
player_hit_list = pygame.sprite.spritecollide(player, enemy_list, True)
for coin in player_hit_list:
score = (score)+ 1
print(score)
if __name__ == '__main__':
main()
我正在尝试做到这一点,以便在玩家死亡时有一个``重试''按钮或其他东西,以便玩家可以在分数继续进行下一轮的同时重复游戏。但我找不到将文字显示在屏幕上的方法。