【问题标题】:Why are some events don't execute when called?为什么有些事件在调用时不执行?
【发布时间】:2019-11-23 00:11:54
【问题描述】:

Python 3.

你好。我做了一个游戏,它从一个主菜单开始,当按下“d”时,它会切换到游戏屏幕。

在我制作这个主菜单之前,当我按住空格键时,形状会发出隆隆声。现在,当我按“d”开始游戏时,会显示对象,但按住空格键不会做任何事情,按退出键或关闭游戏也不会。按下“d”后,似乎不再调用键盘事件/游戏事件。 代码:

import pygame
import random
import time



BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)


# Edit the intensity of the shake (Must be one number apart)
# Ex: a = -100, b = 101. A is negative, B is positive
a = -4
b = 5
up = 10
intensity = (a, b)
startGame = True

# Image Loading

pygame.init()

size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

done = False

clock = pygame.time.Clock()


class Rectangle():
    def __init__(self):
        self.x = random.randrange(0, 700)
        self.y = random.randrange(0, 500)
        self.height = random.randrange(20, 70)
        self.width = random.randrange(20, 70)
        self.x_change = random.randrange(-3, 3)
        self.y_change = random.randrange(-3, 3)
        self.color = random.sample(range(250), 4)

    def draw(self):
        pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height])

    def move(self):
        self.x += self.x_change
        self.y += self.y_change


class Ellipse(Rectangle):
    pass

    def draw(self):
        pygame.draw.ellipse(screen, self.color, [self.x, self.y, self.width, self.height])

    def move(self):
        self.x += self.x_change
        self.y += self.y_change

def text_objects(text, font):
    textSurface = font.render(text, True, BLACK)
    return textSurface, textSurface.get_rect()

def game_intro():
    global event
    intro = True
    keys = pygame.key.get_pressed()
    while intro:
        for event in pygame.event.get():
            print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        screen.fill(WHITE)
        largeText = pygame.font.Font('freesansbold.ttf', 45)
        smallText = pygame.font.Font('freesansbold.ttf', 30)
        TextSurf, TextRect = text_objects("Welcome to Crazy Rumble.", largeText)
        TextRect.center = ((700 / 2), (100 / 2))
        TextSurff, TextRectt = text_objects("Press enter to start", smallText)
        TextRectt.center = ((700 / 2), (900 / 2))
        TextStart, TextRecttt = text_objects("Hold space to make the shapes shake!", smallText)
        TextRecttt.center = ((700 / 2), (225 / 2))
        screen.blit(TextSurf, TextRect)
        screen.blit(TextSurff, TextRectt)
        screen.blit(TextStart, TextRecttt)
        pygame.display.update()
        if event.type == pygame.KEYUP:
            intro = False
            startGame = True

global intro
my_list = []
for number in range(600):
    my_object = Rectangle()
    my_list.append(my_object)
for number in range(600):
    my_object = Ellipse()
    my_list.append(my_object)
# -------- Main Program Loop -----------
while not done:
    game_intro()
    game_intro = True
    if event.type == pygame.KEYUP:
        game_intro = False
    keys = pygame.key.get_pressed()
    # --- Main event loop
    while game_intro == False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        screen.fill(BLACK)
        for rect in my_list:
            rect.draw()
            rect.move()
        for rectElli in my_list:
            rectElli.draw()
            if keys[pygame.K_SPACE]:
                rectElli.y_change = random.randrange(a, b)
                rectElli.x_change = random.randrange(a, b)
                rectElli.move()

        if keys[pygame.K_UP]:
            print(up)
            print(intensity)
            up += 1
            if up % 10 == 0:
                a -= 1
                b -= -1
        else:
            a, b = -4, 5

        pygame.display.flip()
        clock.tick(60)

【问题讨论】:

  • game_intro = True 这是用变量替换函数。

标签: python pygame


【解决方案1】:

你只是设置keys一次

keys = pygame.key.get_pressed()

您需要将该调用放入循环中,以便在每次事件后更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    相关资源
    最近更新 更多