缺乏Pygame的经验

时间:2019-04-30 15:15:23

标签: python pygame

我正在为自己在Pygame中的娱乐开发一种Shoot'em up类型的游戏,并且在创建玩家基本动作的过程中遇到了一些疑问, “ Dash”和“ Switch”都无法正常工作。

...

def switch(self):
    if self.offensive_stance() == True:
        self.defensive_stance() == False
    if self.defensive_stance() == True:
        self.offensive_stance() == False
    switch_stance = cycle([self.offensive_stance(), 
    self.defensive_stance()])
    next(switch_stance)

按LSHIFT键后的#switch()在Player类下正常工作

def dash(self, cooldown=200):
    self.last_dash = pygame.time.get_ticks()
    self.cooldown = cooldown

def dash_properties(self):
    now = pygame.ticks.get_ticks()
    if self.last_dash - now >= cooldown:
        self.last_dash= now
        self.rect.x -= self.speedx * 2
        self.rect.y -= self.speedy * 2
...

我希望这两个对象都有这两种结果。

Dash(x和y轴上的速度均增加1s,然后冷却)-由于经验不足,无法创建它。

切换(在2个功能,进攻性和防御性之间循环)-无法使用LSHIFT正确创建切换,仅在按下LSHIFT时激活defensive_stance

1 个答案:

答案 0 :(得分:0)

使用LSHIFT切换变量active中的值并用于切换矩形中颜色(红色/绿色)的最小示例。

import pygame

# --- constants --- (UPPER CASE NAMES)

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

# --- main ---

pygame.init()

screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()

rect = pygame.Rect(0, 0, 200, 200)
rect.center = screen_rect.center

active = False

# --- mainloop ---

clock = pygame.time.Clock()

running = True

while running:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

            elif event.key == pygame.K_LSHIFT:
                # toggle
                active = not active

    # --- updates ---

    if active:
        color = GREEN
    else:
        color = RED

    # --- draws ---

    screen.fill(BLACK)

    pygame.draw.rect(screen, color, rect)        
    pygame.display.flip()

    clock.tick(5)

# --- end ---

pygame.quit()