- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个游戏,想知道如何让我的“玩家”或绿色棋子双向移动。每当我单击任何箭头时,他都会向右移动,因为这就是我所能弄清楚的。动画代码位于第 27 行和第 28 行。每次单击任一箭头时,我需要绿色圆圈能够左右移动 5 个像素。编辑:我还需要碰撞发生在它击中红色子弹上的任何位置时,因为在我当前的代码中,子弹必须击中玩家的确切坐标。
import pygame
import random
BLACK = (0,0,0)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
pygame.init()
size = (700,700)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Dodger")
done = False
clock = pygame.time.Clock()
def resetpositions():
global bulletrect1, bulletrect2, bulletrect3, bulletrect4, bulletrect5, circlerect
bulletrect1 = pygame.rect.Rect((350, 0, 20, 20))
bulletrect2 = pygame.rect.Rect((175, 0, 20, 20))
bulletrect3 = pygame.rect.Rect((525, 0, 20, 20))
bulletrect4 = pygame.rect.Rect((525, 0, 20, 20))
bulletrect5 = pygame.rect.Rect((525, 0, 20, 20))
circlerect = pygame.rect.Rect((350, 600, 20, 20))
resetpositions()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
circlerect.x += 5
bulletrect1.y += 1
bulletrect2.y += 2
bulletrect3.y += 3
bulletrect4.y += 4
bulletrect5.y += 5
screen.fill(BLACK)
pygame.draw.circle(screen, GREEN, (circlerect.center), 15)
pygame.draw.circle(screen, RED, (bulletrect1.center), 20)
pygame.draw.circle(screen, RED, (bulletrect2.center), 20)
pygame.draw.circle(screen, RED, (bulletrect3.center), 20)
pygame.draw.circle(screen, RED, (bulletrect4.center), 20)
pygame.draw.circle(screen, RED, (bulletrect5.center), 20)
if bulletrect1.y == 800:
bulletrect1.y = 0
bulletrect1.x = random.randint(20,680)
if bulletrect2.y == 800:
bulletrect2.y = 0
bulletrect2.x = random.randint(20,680)
if bulletrect3.y == 800:
bulletrect3.y = 0
bulletrect3.x = random.randint(20,680)
if bulletrect4.y == 800:
bulletrect4.y = 0
bulletrect4.x = random.randint(20,680)
if bulletrect5.y == 800:
bulletrect5.y = 0
bulletrect5.x = random.randint(20,680)
if circlerect.x == 685:
circlerect.x = 15
if circlerect.collidelist((bulletrect1, bulletrect2, bulletrect3, bulletrect4, bulletrect5)) == 0:
screen.fill(BLACK)
font = pygame.font.SysFont('Calibri',40,True,False)
text = font.render("GAME OVER",True,RED)
screen.blit(text,[250,350])
pygame.display.update()
pygame.time.delay(3000)
resetpositions()
pygame.display.flip()
clock.tick(300)
pygame.quit()
最佳答案
第 30 行和第 31 行是:
if event.type == pygame.KEYDOWN:
circlerect.x += 5
此代码会检测何时按下任意键,然后将圆矩形对象向右移动 5 个单位(始终向右,因为它始终为 +=)。
如果你想双向移动,你将不得不检测的不仅仅是 KEYDOWN。您必须检测按键何时为左键(并减 5)以及何时为右键(并加 5)。
关于python - 我无法让 'player' 在 pygame 中双向移动,我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46387070/
stackoverflow如何得知JavaScript无法正常工作,并能够在页面顶部通知用户? 是否可以判断脚本是否未加载或产生错误,然后仍然能够使用JavaScript生成错误消息? 最佳答案
Xcode 项目方案共享...如何做到这一点? 当我将 Xcode 项目提交到 SVN 时,我发现我创建的方案保存在我的用户名文件夹下,例如 abc.xcodeproj/xcuserdata/_my_
我有这个 SQL: DROP TABLE MISSINGTABLE; CREATE TABLE MISSINGTABLE ( TABLE_NAME VARCHAR2 (70), DESCRIP
我是PHP OOP的初学者,并对使用PHP处理错误的正确方法有些疑问。 例如看这个功能: public function deleteFileFromDisk($fileNameToBeDeleted
YouTube上有很多视频,我们希望为网站访问者提供自动的YouTube成绩单对齐方式。我们想要的几乎就像this example。 但是,我们希望逐个句子对齐,而不是逐个单词自动对齐,例如this
假设,我有 IAsynchronous 接口(interface),它支持两种执行某些操作的方法(“开始/结束”模式): IAsyncResult BeginOperation(AsyncCallba
Hardware Product 1 Product 2 Product 3 Product 4 Product 5 我有这样的结构,我想做一个重新排序界面,用户可以通过单击向上箭头在层次结构中向
假设,我有 IAsynchronous 接口(interface),它支持两种执行某些操作的方法(“开始/结束”模式): IAsyncResult BeginOperation(AsyncCallba
我正在使用 Silverlight 2.0 Unleashed + Silverlight 4.0 Unleashed 学习 Silverlight,好吧,我只是在玩弄它:-) 作为其中的一部分,我正
有人可以解释一下我还是链接-我有512x512图标,但我不知道我需要创建什么图标大小以及如何将它们添加到我的iOS应用中。我需要什么尺寸以及如何添加尺寸? 最佳答案 简而言之:非视网膜iPhone或i
我想在 Java 中模拟以下情况,但我陷入困境: 特别是与客户、预订、航类预订和巴士预订相关的部分。我想要一组客户对象,每个对象都有自己的预订列表(可以是航类预订或巴士预订)。 我计划像下面的测试代码
我在 opencv、Pillow、ImageMagick、subprocess 和 ffmpeg 之间摇摆,作为操作图形数据的一种方式。 ImageMagick 看起来不错而且功能相当强大,但我在 W
我想做类似的事情 SELECT t.subtitle FROM temp t LEFT JOIN ep e ON e.subtitle=t.subtitle AND e.epi
Frame[] test = new Frame[3] {{2,5},{3,6},{4,7}}; 数组初始化器只能用在变量或字段初始化器中。尝试改用新表达式。 这怎么可能? 最佳答案 这里的问题是文字
我不知道如何正确创建第一个返回。它会像这样工作,但问题是 searchtestarrayone 总是有不同的长度,而且它可能非常大。几周前开始了我的 Swift 之旅,所以下面的代码中可能有一些愚蠢的
我有这样的表: NameSteve Martin PhoneXXX Bank account654861/46147 我对表格的相同部
我有一个关于单选按钮的快速问题,以及当用户返回页面时如何设置它们。我现在想要的是能够在他们返回页面时显示所选项目。同一组中有几个,所以我不能使用 getElementByID(遗憾!)。 这是我的 H
我做了一些事情: class Tuple1 { private T1 a; private T2 b; public Tuple1(T1 a, T2 b) {
我目前正在研究我在大学的期末项目,它看起来像 instagram。在 instagram android 应用程序中,您可以点击并按住图像和 boom,显示弹出窗口。但我不知道该怎么做! 最佳答案 您
我正在使用来自 mourner/suncalc 的函数这让我可以得到我们太阳的当前位置。使用 getPosition(),我想在图像上或使用纯 CSS(当然可以缩放到不同的屏幕分辨率和方向)创建动画,
我是一名优秀的程序员,十分优秀!