文本没有在我的pygame脚本上使用python进入屏幕

时间:2013-11-21 10:09:43

标签: python pygame

我想在我的射击游戏中添加一个弹药夹子的东西,它非常简单我所做的就是blit我*弹药的数量,但我希望有三条线来显示这样的剪辑:

IIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIII

每一行都有35个弹药符号,但如果玩家的弹药数量超过95,我希望它能弹出那个弹药夹旁边的数字,但无论我有多少弹药,它都不会闪现数字,无论如何我开始时有多少弹药,它在开始时不会比这更多,直到我收集一个弹药箱:

IIIIIIIIII

我真的无法找出它为什么会这样做,我试图改变它的数量以及许多其他东西,但似乎没有任何效果,所以听到我的代码:

            #wave font 
            font = pygame.font.SysFont("", 34)
            self.text_pause = font.render("WAVE " + str(self.wave) * self.blitwave, -1, RED)
            self.text_pause_rect = self.text_pause.get_rect(center=self.screen.get_rect().center) # center text


            texthealth = int(self.health / 10)
            #health font
            self.text_health = font.render("o" * texthealth, -1, RED)


            #score font
            self.text_score = font.render("SCORE " + str(self.score), -1, BLACK)

            #cash font
            self.text_cash = font.render(str(self.cash), -1, GREEN)

            #ammo font
            self.text_ammoS = font.render("I" * 35, -1, RED)
            self.text_ammo = font.render("I" * self.ammo_amount, -1, RED)
            self.text_ammoW = font.render("I" * (self.ammo_amount - 35), -1, RED)
            self.text_ammoE = font.render("I" * (self.ammo_amount - 70), -1, RED)
            self.text_ammoN = font.render(str(self.ammo_amount), -1, RED)

            # send event to player
            self.player.event_handler(event)

        if not PAUSED:
            self.all_sprites_list.update()
            self.bullets_update()

        player_rect = pygame.Rect(self.player.rect.x, self.player.rect.y, 20, 20)
        block_rect = pygame.Rect(block.rect.x, block.rect.y, 20, 20)

我以为我可以制作一个跟随块的矩形,如果那个矩形与他的玩家发生碰撞他们死了,几乎使用与弹药相同的系统,因为它立即起作用

            if pygame.sprite.collide_rect(self.player, block):
                self.player.health =- 00.0000000003
                print('hit')


            if self.ammo and player_rect.colliderect(self.ammo.rect):
                self.ammo_amount += 70
                self.all_sprites_list.remove(self.ammo)
                self.ammo = None

            #if self.Bomb and player_rect.colliderect(self.ammo.rect):
                #print('nuke')
                #self.all_sprites_list.remove(self.ammo)
                #self.Bomb = None 

            self.crosshair.update()

            # --- draws ---

            self.background.draw(self.screen)

            self.all_sprites_list.draw(self.screen)

            #must be last
            self.screen.blit(self.text_pause, (10, 610))
            self.screen.blit(self.text_score, (700, 10))
            self.screen.blit(self.text_cash, (740, 500))
            #self.screen.blit(self.text_ammo, (450, 610))

            if self.ammo_amount > 0 and self.ammo_amount < 36:
                self.screen.blit(self.text_ammo,  (600, 540))

            if self.ammo_amount > 35 and self.ammo_amount < 71:
                self.screen.blit(self.text_ammoS, (600, 540))
                self.screen.blit(self.text_ammoW,  (600, 560))

            if self.ammo_amount > 70 and self.ammo_amount < 96:
                self.screen.blit(self.text_ammoS, (600, 540))
                self.screen.blit(self.text_ammoS, (600, 560))
                self.screen.blit(self.text_ammoE,  (600, 580))

            if self.ammo_amount > 95:
                self.screen.blit(self.text_ammoS, (600, 540))
                self.screen.blit(self.text_ammoS, (600, 560))
                self.screen.blit(self.text_ammoS, (600, 580))
                self.screen.blit(self.text_ammoN, (550, 580))

            self.screen.blit(self.text_health, (5, 5))

            self.crosshair.draw(self.screen)

            pygame.display.update() # use flip() OR update()

            # --- FPS ---

            clock.tick(70)

        # --- quit ---

        pygame.quit()

#----------------------------------------------------------------------

Game().run()

这是重要的部分,但如果你想在这里看到整个代码,那就是:

import pygame
from pygame import *

import sys
import math
import random
import cmath

#----------------------------------------------------------------------

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED   = (255, 0, 0)
BLUE  = (0, 0, 255)
YELLOW = (255, 255, 0)
GREEN = (154 ,205, 50)

#images
IMAGE_GRASS       = "grass_shit.png" 
IMAGE_PLAYER      = "shithead.png"   
IMAGE_ALI         = "shit_head2.png" 
IMAGE_DEAD_SCREEN = "dead_shit.png"
IMAGE_CROSSHAIR   = "crosshair.png"
IMAGE_PLAYBUTTON  = "playbutton.png"
IMAGE_AMMO        = "ammoshit.png"

#----------------------------------------------------------------------

class Block(pygame.sprite.Sprite):

    def __init__(self, color, x, y, player = None):

        pygame.sprite.Sprite.__init__(self)

        self.player = player

        self.image = pygame.Surface([20, 20])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        self.move_x = self.move_y = 0

    def update(self):

        if self.player:
            player_x, player_y = self.player.rect.center

            if self.rect.x < player_x:
                self.rect.x += 1
            elif self.rect.x > player_x:
                self.rect.x -= 1

            if self.rect.y < player_y:
                self.rect.y += 1
            elif self.rect.y > player_y:
                self.rect.y -= 1

#----------------------------------------------------------------------

class Player(pygame.sprite.Sprite):

    def __init__(self, screen_rect, x=0, y=0):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([20,20])
        self.image.fill(RED)

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        self.min_x = screen_rect.left
        self.min_y = screen_rect.top
        self.max_x = screen_rect.right
        self.max_y = screen_rect.bottom

        self.move_x = self.move_y = 0

        self.health = 138

    def update(self):

        pos = pygame.mouse.get_pos()

        self.rect.x += self.move_x
        self.rect.y += self.move_y

        if self.rect.top < self.min_x:
            self.rect.top = self.min_x
        elif self.rect.bottom > self.max_y:
            self.rect.bottom = self.max_y

        if self.rect.left < self.min_x:
            self.rect.left = self.min_x
        elif self.rect.right > self.max_x:
            self.rect.right = self.max_x

    def event_handler(self, event):

        if event.type == pygame.KEYDOWN:    
            if event.key == pygame.K_a:

                self.move_x = -8

            elif event.key == pygame.K_d:

                self.move_x = +8

            elif event.key == pygame.K_w:

                self.move_y = -8

            elif event.key == pygame.K_s:

                self.move_y = +8

        if event.type == pygame.KEYUP:
            if event.key in (pygame.K_a, pygame.K_d):

                self.move_x = 0
            elif event.key in (pygame.K_w, pygame.K_s):

                self.move_y = 0

#----------------------------------------------------------------------

class Bullet(pygame.sprite.Sprite):

    def __init__(self, start_pos, mouse_pos):

        pygame.sprite.Sprite.__init__(self)

        self.start_rect = start_pos.rect.copy()
        self.mouse_x, self.mouse_y = mouse_pos # mouse[0], mouse[1]

        self.image = pygame.Surface([5, 5])
        self.image.fill(BLACK)      
        self.rect = self.image.get_rect()
        self.rect.centerx = self.start_rect.centerx
        self.rect.centery = self.start_rect.centery

        self.speed = 20
        self.max_range = 100
        self.current_range = 0

        distance_x = self.mouse_x - self.start_rect.centerx
        distance_y = self.mouse_y - self.start_rect.centery

        norm = math.sqrt(distance_x ** 2 + distance_y ** 2)
        direction_x = distance_x / norm
        direction_y = distance_y / norm

        self.bullet_vector_x = direction_x * self.speed
        self.bullet_vector_y = direction_y * self.speed

    def update(self):

        self.current_range += 1

        if self.current_range < self.max_range:
            #print self.start_rect.centerx + (self.bullet_vector_x*self.current_range), 
            #print self.rect.centerx + self.bullet_vector_x, 

            #self.rect.centerx += self.bullet_vector_x
            self.rect.centerx = self.start_rect.centerx + (self.bullet_vector_x*self.current_range) 

            #print self.rect.centerx

            #self.rect.centery += self.bullet_vector_y
            self.rect.centery = self.start_rect.centery + (self.bullet_vector_y*self.current_range)

        else:
            self.kill()

#----------------------------------------------------------------------

class Crosshair(pygame.sprite.Sprite):

    def __init__(self):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load(IMAGE_CROSSHAIR).convert_alpha()
        self.rect = self.image.get_rect()

    def update(self):

        mouse_x, mouse_y = pygame.mouse.get_pos()

        self.rect.centerx = mouse_x
        self.rect.centery = mouse_y

    def draw(self, screen):
        screen.blit(self.image,self.rect.topleft)

#----------------------------------------------------------------------

class Background(pygame.sprite.Sprite):

    def __init__(self):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load(IMAGE_GRASS).convert_alpha()
        self.rect = self.image.get_rect()

    def draw(self, screen):
        screen.fill((128,128,128))
        screen.blit(self.image,(0,0))

#----------------------------------------------------------------------

class Ammo(pygame.sprite.Sprite):

    def __init__(self, color, x, y, player = None):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([20, 20])
        self.image.fill(BLACK)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self):
        pass

class Bomb(pygame.sprite.Sprite):

    def __init__(self, color, x, y, player = None):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([20, 20])
        self.image.fill(BLACK)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self):
        pass

class Game():

    def __init__(self):

        pygame.init()

        screen_width = 850
        screen_height = 640

        place_ammo = False

        self.screen = pygame.display.set_mode( (screen_width,screen_height) )

        pygame.mouse.set_visible(False)

        #-----

        self.all_sprites_list = pygame.sprite.Group()
        self.block_list = pygame.sprite.Group()
        self.bullet_list = pygame.sprite.Group()

        self.blitwave = 1

        # --- create sprites ---

        self.background = Background()

        self.player = Player(self.screen.get_rect(), 0, 370)
        self.all_sprites_list.add(self.player)
        self.ammo = Ammo(self.screen.get_rect(),random.randrange(10, 750),random.randint(10,600 - 10))
        self.all_sprites_list.add(self.ammo)

        self.ammo_amount = 100
        self.on_screen = 1
        self.score = 0

        self.crosshair = Crosshair()

    def bullet_create(self, start_pos, mouse_pos):
        bullet = Bullet(start_pos, mouse_pos)

        self.all_sprites_list.add(bullet)
        self.bullet_list.add(bullet)

    def bullets_update(self):

        for bullet in self.bullet_list:

            block_hit_list = pygame.sprite.spritecollide(bullet, self.block_list, True)
            screen_width = 850
            screen_height = 640

            for block in block_hit_list:
                self.bullet_list.remove(bullet)
                self.all_sprites_list.remove(bullet)
                self.score += 1
                self.on_screen -= 1
                self.ammo_chance = self.ammo_amount * 5
                if self.ammo_chance > 0:
                    self.drop = random.randint(1, self.ammo_chance)
                    print(self.drop)
                if self.drop > 0 and self.drop < 2:

                    print('ammo drop')
                    self.ammo = Ammo(self.screen.get_rect(),random.randrange(10,screen_width),random.randint(10,screen_height - 10))
                    self.all_sprites_list.add(self.ammo)

                if bullet.rect.y < -10:
                    self.bullet_list.remove(bullet)
                    self.all_sprites_list.remove(bullet)

    # -------- Main Program Loop -----------

    def run(self):
        screen_width = 850
        screen_height = 640

        #wave
        self.wave = 1
        self.wave_no = 2
        self.wave_running = True
        block = Block(BLUE, random.randrange(100, screen_width), random.randrange(10, screen_height-10), self.player)

        self.block_list.add(block)
        self.all_sprites_list.add(block)

        clock = pygame.time.Clock()

        self.cash = 1
        self.health = 100
        self.ammo_amount = 10

        RUNNING = True
        PAUSED  = False

        while RUNNING:

            # --- events ---
            if self.player.health <= 0:
                RUNNING = False
            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    RUNNING = PAUSED

                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        PAUSED = True

                elif event.type == pygame.KEYUP:
                    if event.key == pygame.K_SPACE:
                        PAUSED = not PAUSED

                elif event.type == pygame.MOUSEBUTTONDOWN and self.ammo_amount > 0:
                    self.bullet_create(self.player, event.pos)
                    self.ammo_amount -= 1

                self.cash = self.score * 5

                if self.on_screen == 0:

                    for i in range(self.wave_no):

                        block = Block(BLUE, random.randrange(100, screen_width), random.randrange(10, screen_height-10), self.player)

                        self.block_list.add(block)
                        self.all_sprites_list.add(block)
                        self.on_screen += 1

                    self.wave_div = int(self.wave_no / 2)
                    self.wave_no += self.wave_div
                    self.wave += 1

                #wave font 
                font = pygame.font.SysFont("", 34)
                self.text_pause = font.render("WAVE " + str(self.wave) * self.blitwave, -1, RED)
                self.text_pause_rect = self.text_pause.get_rect(center=self.screen.get_rect().center) # center text

                texthealth = int(self.health / 10)
                #health font
                self.text_health = font.render("o" * texthealth, -1, RED)

                #score font
                self.text_score = font.render("SCORE " + str(self.score), -1, BLACK)

                #cash font
                self.text_cash = font.render(str(self.cash), -1, GREEN)

                #ammo font
                self.text_ammoS = font.render("I" * 35, -1, RED)
                self.text_ammo = font.render("I" * self.ammo_amount, -1, RED)
                self.text_ammoW = font.render("I" * (self.ammo_amount - 35), -1, RED)
                self.text_ammoE = font.render("I" * (self.ammo_amount - 70), -1, RED)
                self.text_ammoN = font.render(str(self.ammo_amount), -1, RED)

                # send event to player
                self.player.event_handler(event)

            if not PAUSED:
                self.all_sprites_list.update()
                self.bullets_update()

            player_rect = pygame.Rect(self.player.rect.x, self.player.rect.y, 20, 20)
            block_rect = pygame.Rect(block.rect.x, block.rect.y, 20, 20)

    #I thought i could make a rect that follows the blocks and if that rect collides witht he player they die, pretty much using the same system as the ammo, coz that works instantly

            if pygame.sprite.collide_rect(self.player, block):
                self.player.health =- 00.0000000003
                print('hit')

            if self.ammo and player_rect.colliderect(self.ammo.rect):
                self.ammo_amount += 70
                self.all_sprites_list.remove(self.ammo)
                self.ammo = None

            #if self.Bomb and player_rect.colliderect(self.ammo.rect):
                #print('nuke')
                #self.all_sprites_list.remove(self.ammo)
                #self.Bomb = None 

            self.crosshair.update()

            # --- draws ---

            self.background.draw(self.screen)

            self.all_sprites_list.draw(self.screen)

            #must be last
            self.screen.blit(self.text_pause, (10, 610))
            self.screen.blit(self.text_score, (700, 10))
            self.screen.blit(self.text_cash, (740, 500))
            #self.screen.blit(self.text_ammo, (450, 610))

            if self.ammo_amount > 0 and self.ammo_amount < 36:
                self.screen.blit(self.text_ammo,  (600, 540))

            if self.ammo_amount > 35 and self.ammo_amount < 71:
                self.screen.blit(self.text_ammoS, (600, 540))
                self.screen.blit(self.text_ammoW,  (600, 560))

            if self.ammo_amount > 70 and self.ammo_amount < 96:
                self.screen.blit(self.text_ammoS, (600, 540))
                self.screen.blit(self.text_ammoS, (600, 560))
                self.screen.blit(self.text_ammoE,  (600, 580))

            if self.ammo_amount > 95:
                self.screen.blit(self.text_ammoS, (600, 540))
                self.screen.blit(self.text_ammoS, (600, 560))
                self.screen.blit(self.text_ammoS, (600, 580))
                self.screen.blit(self.text_ammoN, (550, 580))

            self.screen.blit(self.text_health, (5, 5))

            self.crosshair.draw(self.screen)

            pygame.display.update() # use flip() OR update()

            # --- FPS ---

            clock.tick(70)

        # --- quit ---

        pygame.quit()

#----------------------------------------------------------------------

Game().run()

非常感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:1)

你没有弹出弹药号码 - 请参阅#排队:

   #self.screen.blit(self.text_ammo, (450, 610))

必须是text_ammoN而不是text_ammo


顺便说一下:

你需要在主循环中使用很多代码 - 将一些代码放在诸如update_wave_text(),update_ammo_text()等函数中,blit_wave_text(),blit_ammo_tex()等等。

self.设置

中使用__init__ :.
self.screen_width = 850
self.screen_height = 640

而不是使用self.screen_widthself.screen_height,您将不必重复

screen_width = 850
screen_height = 640