gpt4 book ai didi

python - 缺乏 Pygame 经验

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:22 25 4
gpt4 key购买 nike

我正在 Pygame 中开发一款射击类游戏供我自己娱乐,在创建玩家的基本 Action 的过程中我遇到了一些疑问,“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
...

我预计这两个对象都会有这两个结果。

冲刺(x 轴和 y 轴速度增加 1 秒,然后冷却)- 由于缺乏经验,无法创建它。

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

最佳答案

使用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()

关于python - 缺乏 Pygame 经验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55924003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com