- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
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/
我的一个页面上有一个 map 控件,我希望拍摄它的照片并将其显示在不同的屏幕上。有谁知道我怎样才能实现这一目标?该 map 位于 范围内. 最佳答案 您应该使用 WriteableBitmap 类的
我正在使用 Qt 4.5 和 qgraphicsscene/view 向用户显示视频。 我想提供一个“获取快照”按钮,我相信有一种非常简单的方法可以让我想到的一切都简单得多。 我怎样才能优雅地做到这一
使用以下代码拍摄 AGSMapView 的快照 此代码将设置 kEAGLDrawablePropertyRetainedBacking 属性,这是必需的,否则它将拍摄白色快照。 let layer =
我目前正在使用 direct X 用 c++ 编写游戏,目前有一个 vector 来存储绘制我所有的 Sprite 。我无法让子弹在 vector 中工作。子弹射了,但应该有 50 颗却只射出一颗 /
如何获取安卓手机的打印屏幕??我正在用 GPS 做一个应用程序。所以我无法使用模拟器拍摄打印照片。有没有应用程序可以做到这一点? 最佳答案 通过 USB 连接您的手机,转到 Eclipse 中的调试透
我有以下 JSON Firebase 数据库: { "fruits": { "apple": { "name": "Gala", "ur
我正在做一个项目,它应该获取 iPhone 中视频图标内的视频。任何人都可以通过提供链接或其他方式来帮助我吗?如有任何帮助,我们将不胜感激。 谢谢 最佳答案 这是不可能的,因为我们无法从一个应用程序内
如何链接到嵌入 Flash 视频的快照而不是实际的 Flash 视频,以减少网站的加载时间? 最佳答案 是的,事实上这是一种经常使用的方法。您从带有播放按钮覆盖层的图像开始。单击后,图像元素将替换为播
我正在用这个截取 avPlayer 的屏幕截图.. print(startTime) print(Float64(startTime)) var time: CMTime =
我试图从 nib 加载一个 View ,配置它,并拍摄它的快照,而不是将它添加到屏幕上: + (void)composeFromNibWithImage:(UIImage*)catImage comp
我正在制作一个 tvOS 应用程序,我希望它看起来与电影应用程序相似。因此我有一个 UICollectionView。现在我的单元格不仅仅是简单的 UIImageView,而是更复杂一些。 我仍然想要
Apple 建议从提交给应用商店的屏幕截图中裁剪掉状态栏。在预览版中手动执行此操作非常繁琐且容易出错。 是否有任何开发人员有任何最佳实践建议或自动化技术来加速此过程?目标是将 iPad 和/或 iPh
我所有的搜索都涉及更一般的 arc/sin/cos 用法或射击到鼠标位置。我希望用键盘瞄准并发射射弹,并且作为网络类(class)中的菜鸟做一个项目,我已经从头开始做了很多工作,但我陷入了困境。我当前
假设我有一个服务: public interface ICustomersService { IObservable Customers { get; } }
在我的 IOS 应用程序中,我正在截取 UIImageView 的屏幕截图。如附件照片所示,它非常完美。但是在这里,我采用了 UIImageView content mode aspect fit 。
我正在使用 WebRTC 在两个用户之间建立视频聊天。我想拍一张显示其中一个人的 localView View 。 这是我的类,它使用 configureLocalPreview 方法将视频流与 UI
EBS 文档指出: As an example, volumes that operate with 20 GB or less of modified data since their most r
import qualified Data.Vector as V import Control.Lens import Data.Vector.Lens v = V.fromList [V.from
我在我的应用程序中的“查看”按钮中有一个用于使用以下代码拍摄快照的按钮 open func takeScreenshot(_ shouldSave: Bool = true) -> UIImage?
我基本上是在尝试在我的 Activity 中捕获相对布局的屏幕截图并将其保存为 PNG 图像。然后我需要将它作为位图文件检索。我编写了一些代码来完成此操作,但遗憾的是屏幕截图未保存在我的设备上。 XM
我是一名优秀的程序员,十分优秀!