我试图让红色矩形(注释)在屏幕上不止一次出现,而不是一次只出现一个矩形。有没有办法在不创建多个相同的矩形并删除更多代码的情况下实现这一目标?我是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()
答案 0 :(得分:3)
使用音符位置(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
s放入列表而不仅仅是y位置。它们有一些方便的碰撞检测方法和属性。
答案 1 :(得分:0)
有些晚了,但这是给其他有相同问题的人的。
检查红色块是否在屏幕外,类似于该块的y坐标是否大于显示高度, y坐标= 0(或-60或使其从上而下的东西)