gpt4 book ai didi

python - 为什么我在 pygame 中的拍摄代码不起作用?

转载 作者:行者123 更新时间:2023-12-04 10:32:00 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to get if a key is pressed pygame [duplicate]

(1 个回答)


11 个月前关闭。




我正在 pygame 中制作潜艇游戏,我试图让我的潜艇射击子弹。我已经尝试了我在另一个 pygame 项目中使用的代码,但是它在这个项目中不起作用。编译器没有给出任何错误,但是当我按空格时,它不会射击。我试图找出错误,但我不能。我也尝试浏览 Stack Overflow,但没有找到我想要的答案。
这是代码:

import pygame

pygame.init()
run = True
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption('Podmornca')
desno = pygame.image.load('podmornicaD.png')
levo = pygame.image.load('podmornica.png')
ozadje = pygame.image.load('ozadje.png')
torpedoD = pygame.image.load('torpedo.png')
torpedoL = pygame.image.load('torpedoL.png')
class podmornica():
def __init__(self, x, y, v):
self.x = x
self.y = y
self.v = v
self.ziv = 100
self.levo = False
self.desno = True
def naris(self):
if self.levo:
screen.blit(levo, (self.x, self.y))
elif self.desno:
screen.blit(desno, (self.x, self.y))
class torpedo():
def __init__(self, x, y, smer):
self.x = x
self.y = y
self.smer = smer
self.v = 5 * smer
def naris(self, screen):
if self.smer < 0:
screen.blit(torpedoD, (self.x, self.y))
else:
screen.blit(torpedoL, (self.x, self.y))
igralec = podmornica(150, 300, 10)
#the list of bullets:
metki = []
def grafika():
screen.blit(ozadje, (0,0))
igralec.naris()
#Here is code for displaying the bullets
for metek in metki:
metek.naris(screen)
pygame.display.flip()
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#This piece of code is for moving of bullets:
for metek in metki:
if metek.x < 600 and metek.x > 0:
metek.x += metek.v
else:
metki.pop(metki.index(metek))
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP and igralec.y > 10:
igralec.y -= 3
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN and igralec.y < 350:
igralec.y += 3
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
igralec.levo = True
igralec.desno = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
igralec.levo = False
igralec.desno = True
#the trigger for bullet:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
if igralec.levo:
smer = -1
else:
smer = 1
if len(metki) < 5:
metki.append(torpedo(igralec.x, igralec.y, smer))
grafika()
pygame.quit()

最佳答案

事件必须在事件循环中处理。如果你想实现流畅的运动,那么你可以通过 pygame.key.get_pressed() 获得关键状态。 .
此外,子弹移动得非常快。通过 pygame.time.Clock() 控制帧分别 .tick() .例如:

FPS = 60
clock = pygame.time.Clock()
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
#the trigger for bullet:
if event.key == pygame.K_SPACE:
if igralec.levo:
smer = -1
else:
smer = 1
if len(metki) < 5:
metki.append(torpedo(igralec.x, igralec.y, smer))

# This piece of code is for moving of bullets:
for metek in metki[:]:
if metek.x < 600 and metek.x > 0:
metek.x += metek.v
else:
metki.remove(metek)

keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and igralec.y > 10:
igralec.y -= 3
if keys[pygame.K_DOWN] and igralec.y < 350:
igralec.y += 3
if keys[pygame.K_LEFT]:
igralec.levo = True
igralec.desno = False
if keys[pygame.K_RIGHT]:
igralec.levo = False
igralec.desno = True
grafika()

关于python - 为什么我在 pygame 中的拍摄代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60376654/

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