敌人在整个屏幕上不断出现和消失

2024-10-02 02:31:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,敌人不断出现和消失在整个屏幕上。有什么想法吗?你知道吗

    enemies = []
    for _ in range(10):
        enemyx = random.randint(0, 500)
        enemyy = random.randint(0, 500)
        enemies.append(pygame.draw.rect(win, (0, 0, 0), (enemyx, enemyy, 50, 80)))

完整代码如下:

import pygame
import time
import os
import random

pygame.init()

# Screen Resolution
screen_width = 800
screen_height = 600

# Character Variables
character_x = 380
character_y = 250

# Background Variables
backgroundx = -375
backgroundy = -255

# Images
bgimage = pygame.image.load('bg.png')
characterimage = pygame.image.load('character.png')
characterbackimage = pygame.image.load('characterback.png')
characterleftimage = pygame.image.load('characterright.png')
characterrightimage = pygame.image.load('characterleft.png')

healthbarimage = pygame.image.load('healthbar.png')
hungerbarimage = pygame.image.load('hungerbar.png')
thirstbarimage = pygame.image.load('thirstbar.png')
energybarimage = pygame.image.load('energybar.png')

healthimage = pygame.image.load('health.png')
hungerimage = pygame.image.load('hunger.png')
thirstimage = pygame.image.load('thirst.png')

vignetteimage = pygame.image.load('vignette.png')

# Movement Fix
characterbackx = 6.5
characterbacky = 5
characterleftx = 5
characterlefty = 4
characterrightx = 7.5
characterrighty = 4.5

# Bars
healthbarx = 580
healthbary = 555
hungerbarx = 580
hungerbary = 520
thirstbarx = 580
thirstbary = 485
energybarx = 765
energybary = 430

energybardraw_x = 770
energybardraw_y = 432.5
energybardraw_width = 15
energybardraw_height = 1
energybar_gain = False

thirstbardraw_x = 581
thirstbardraw_y = 490.7
thirstbardraw_width = 1
thirstbardraw_height = 15

hungerbardraw_x = 581
hungerbardraw_y = 524
hungerbardraw_width = 1
hungerbardraw_height = 15

thirsttimer = 0
hungertimer = 0
notsprintthirst = True
notsprinthunger = True
sprintthirst = False
sprinthunger = False

# Settings Variables
vignettex = 0
vignettey = 0

# Settings
vignette_on = True
vignette_off = False

# Movement
timerdown = 0
timerup = 0
timerleft = 0
timerright = 0

goingdown = True
movingleft = False
movingright = False
movingup = False

able_to_move = True

moveup = False
movedown = False
moveleft = False
moveright = False
sprint = False
speed = 2.5

loadingscreen = pygame.image.load('loadingscreen.png')
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
screen = pygame.display.set_mode((500, 80), pygame.NOFRAME)
background = pygame.Surface(screen.get_size())
background.fill((0, 244, 0))
screen.blit(loadingscreen, (0, 0))
pygame.display.update()
time.sleep(1)

# Tiers / Level Ups
speedtier_1 = True

# Hitboxes

character_hitbox = pygame.Rect(character_x + 6, character_y + 5, 50, 80)
hitboxvisible = False
enemyhitboxvisible = True

win = pygame.display.set_mode((screen_width, screen_height))
character = win.blit(characterimage, (character_x, character_y))

while True:
    keys = pygame.key.get_pressed()
    pygame.display.update()

    class Background:
        win.blit(bgimage, (backgroundx, backgroundy))

    class Vignette:
        if vignette_on:
            win.blit(vignetteimage, (vignettex, vignettey))

    if vignette_off:
        vignettex = 1000
        vignettey = 1000

    class Bars:

        win.blit(healthbarimage, (healthbarx, healthbary))
        win.blit(hungerbarimage, (hungerbarx, hungerbary))
        win.blit(thirstbarimage, (thirstbarx, thirstbary))
        win.blit(energybarimage, (energybarx, energybary))

        win.blit(healthimage, (735, 556.2))
        win.blit(hungerimage, (735, 520.5))
        win.blit(thirstimage, (733.5, 486.6))

    class DrawBars:
        pygame.draw.rect(win, (0, 0, 0), (energybardraw_x, energybardraw_y, energybardraw_width, energybardraw_height))
        pygame.draw.rect(win, (0, 0, 0), (thirstbardraw_x, thirstbardraw_y, thirstbardraw_width, thirstbardraw_height))
        pygame.draw.rect(win, (0, 0, 0), (hungerbardraw_x, hungerbardraw_y, hungerbardraw_width, hungerbardraw_height))

    if hitboxvisible:
        pygame.draw.rect(win, (200, 10, 50), (character_x + 6, character_y + 5, 50, 80))

    if goingdown:
        character = win.blit(characterimage, (character_x, character_y))
    if movingleft:
        characterleft = win.blit(characterleftimage, (character_x + characterleftx, character_y + characterlefty))
    if movingright:
        characterright = win.blit(characterrightimage, (character_x + characterrightx, character_y + characterrighty))
    if movingup:
        characterback = win.blit(characterbackimage, (character_x + characterbackx, character_y + characterbacky))

    enemies = []
    for _ in range(10):
        enemyx = random.randint(0, 500)
        enemyy = random.randint(0, 500)
        enemies.append(pygame.draw.rect(win, (0, 0, 0), (enemyx, enemyy, 50, 80)))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                moveup = True
            if event.key == pygame.K_s:
                movedown = True
            if event.key == pygame.K_a:
                moveleft = True
            if event.key == pygame.K_d:
                moveright = True
            if event.key == pygame.K_LSHIFT:
                sprint = True
        if event.type == pygame.KEYUP:
            if able_to_move:
                if event.key == pygame.K_w:
                    moveup = False
                if event.key == pygame.K_s:
                    movedown = False
                if event.key == pygame.K_a:
                    moveleft = False
                if event.key == pygame.K_d:
                    moveright = False
                if event.key == pygame.K_LSHIFT:
                    sprint = False
    if moveup:
        goingdown = False
        movingleft = False
        movingright = False
        movingup = True
        backgroundy += speed
    if movedown:
        movingleft = False
        movingright = False
        movingup = False
        goingdown = True
        backgroundy -= speed
    if moveleft:
        goingdown = False
        movingleft = True
        movingright = False
        movingup = False
        backgroundx += speed
    if moveright:
        goingdown = False
        movingleft = False
        movingright = True
        movingup = False
        backgroundx -= speed

    if energybar_gain:
        energybardraw_height -= 0.8

    if speedtier_1:
        if sprint:
            speed = 3.5
            energybardraw_height += 2
            sprintthirst = True
            notsprintthirst = False
            sprinthunger = True
            notsprinthunger = False
        if not sprint:
            speed = 2.5
            energybar_gain = True
            sprintthirst = False
            notsprintthirst = True
            sprinthunger = False
            notsprinthunger = True

        if energybardraw_height < 0:
            energybardraw_height = 1
        if energybardraw_height > 146:
            energybardraw_height = 146
            sprint = False

    if notsprintthirst:
        thirsttimer += 0.5
    if sprintthirst:
        thirsttimer += 1

    if thirsttimer == 150:
        thirstbardraw_width += 2
    if thirsttimer > 150:
        thirsttimer = 0

    if notsprinthunger:
        hungertimer += 0.5
    if sprinthunger:
        hungertimer += 1
    if hungertimer == 175:
        hungerbardraw_width += 2
    if hungertimer > 175:
        hungertimer = 0

    if moveup or movedown or moveleft or moveright:
        timerdown += 1
        if timerdown > 12:
            timerdown = 0

        if timerdown == 1:
            character_y -= 1
        if timerdown == 2:
            character_y -= 1
        if timerdown == 3:
            character_y -= 1
        if timerdown == 4:
            character_y -= 1

        if timerdown == 5:
            character_y += 1
        if timerdown == 6:
            character_y += 1
        if timerdown == 7:
            character_y += 1
        if timerdown == 8:
            character_y += 1

    pygame.display.update()

Tags: keyimageeventfalsetrueifpngload
1条回答
网友
1楼 · 发布于 2024-10-02 02:31:03

你的循环正在以最快的速度运行,并且你正在生成新的敌人每个循环,而不管当前是否存在任何敌人,以及在哪里。你知道吗

# This loop runs constantly as fast as it can
while True:
    # This immediately returns any keys that have been pressed
    keys = pygame.key.get_pressed()
    # This redraws the screen
    pygame.display.update()

    # <more stuff here>

    # This makes 10 new enemies in random locations
    enemies = []
    for _ in range(10):
        enemyx = random.randint(0, 500)
        enemyy = random.randint(0, 500)
        # This adds them to the buffer to get drawn on next update
        enemies.append(pygame.draw.rect(win, (0, 0, 0), (enemyx, enemyy, 50, 80)))

    for event in pygame.event.get():
        # <event handlers here>

    # This redraws the screen again
    pygame.display.update()

    # and now you go back to the start of your loop

考虑在循环外生成敌人,这样就不会每次生成更多敌人。你知道吗

enemy_locations = []
for _ in range(10):
    enemyx = random.randint(0, 500)
    enemyy = random.randint(0, 500)
    enemy_locations.append((enemyx, enemyy))

while True:
    # This immediately returns any keys that have been pressed
    keys = pygame.key.get_pressed()
    # This redraws the screen
    pygame.display.update()

    # <more stuff here>

    # This adds them to the buffer to get drawn on next update
    for (enemyx, enemyy) in enemy_locations:
        pygame.draw.rect(win, (0, 0, 0), (enemyx, enemyy, 50, 80))

    for event in pygame.event.get():
        # <event handlers here>

    # This redraws the screen again
    pygame.display.update()

    # and now you go back to the start of your loop

相关问题 更多 >

    热门问题