[英]Is there a way to have a Pygame object reach its destination at one speed, in the same amount of time, but visually move at another speed?
[英]How do have the same drawn object appear at the same time? (pygame)
我试图让红色矩形(注释)在屏幕上显示不止一次,而不是一次只出现 1 个矩形。 有什么办法可以在不创建多个相同的矩形和删除更多代码的情况下实现这一点吗? 我是 pygame 的初学者,所以肯定会感谢一个简单的答案。
import pygame
import time
pygame.init()
cont = True
delNote = False
delUser = False
delScoreUpdate = False
reset = False
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
userColor = (0, 255, 0)
grey = (224, 224, 224)
displayX = 800
displayY = 600
noteY = -600
currentScore = 0
speed = 5
gameDisplay = pygame.display.set_mode((displayX, displayY))
pygame.display.set_caption("game")
clock = pygame.time.Clock()
def drawUser():
user = pygame.draw.rect(gameDisplay, userColor, [375, 430, 50, 50])
def drawNote():
note = pygame.draw.rect(gameDisplay, red, [385, noteY, 30, 30])
def crashDisplay():
font = pygame.font.Font("freesansbold.ttf", 115)
text = font.render(str(currentScore), True, black)
gameDisplay.blit(text, [150, 200])
def scoreUpdate():
fontS = pygame.font.Font("freesansbold.ttf", 30)
textS = fontS.render(str(currentScore), True, black)
gameDisplay.blit(textS, [0, 0])
while cont is True:
if reset is True:
reset = False
delNote = False
gameDisplay.fill(grey)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT and noteY > 420 and noteY < 490:
delNote = True
reset = True
if event.key == pygame.K_RIGHT:
userColor = (0, 150, 0)
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
userColor = (0, 255, 0)
if event.type == pygame.QUIT:
cont = False
if delScoreUpdate is False:
scoreUpdate()
if delUser is False:
drawUser()
if delNote is False:
noteY += speed
drawNote()
if reset is True:
noteY = -600
noteY += speed
if noteY > 600:
delUser = True
delScoreUpdate = True
delNote = True
crashDisplay()
pygame.display.update()
if delScoreUpdate is False:
clock.tick(60)
currentScore += 0.1
currentScore = round(currentScore, 4)
pygame.quit()
quit()
定义一个带有音符位置 ( notes
) 的列表,并在您的 while 循环中遍历此列表以移动和绘制音符,并在事件循环中进行碰撞检测。
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
cont = True
red = (255, 0, 0)
userColor = (0, 255, 0)
grey = (224, 224, 224)
speed = 5
notes = [0, 100, 200]
while cont:
# Event handling.
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
userColor = (0, 150, 0)
# Use enumerate to get the index and the
# item at the same time.
for i, noteY in enumerate(notes):
# If the player rect collides with a note.
if noteY > 420 and noteY < 490:
notes[i] -= 660 # Reset the position.
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
userColor = (0, 255, 0)
elif event.type == pygame.QUIT:
cont = False
# Game logic.
for i in range(len(notes)): # Loop over the notes.
notes[i] += speed # Move the note.
if notes[i] >= 600: # If below the screen ...
notes[i] -= 660 # reset the position.
# Draw everything.
gameDisplay.fill(grey)
pygame.draw.rect(gameDisplay, userColor, [375, 430, 50, 50])
for noteY in notes:
pygame.draw.rect(gameDisplay, red, [385, noteY, 30, 30])
pygame.display.update()
clock.tick(60)
pygame.quit()
您还可以将pygame.Rect
放入列表中,而不仅仅是 y 位置。 他们有一些方便的碰撞检测方法和属性。
这有点晚了,但如果其他人有同样的问题,这是给他们的。
检查红色块是否在屏幕外,例如如果块的 y 坐标大于显示高度,则 y 坐标 = 0(或 -60 或使其像从上来的东西)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.