gpt4 book ai didi

python - 相同的绘制对象如何同时出现? (pygame)

转载 作者:行者123 更新时间:2023-11-30 22:21:55 25 4
gpt4 key购买 nike

我试图让红色矩形(注意)在屏幕上多次显示,而不是一次只显示 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.Rects进入列表而不仅仅是 y 位置。它们有一些方便的碰撞检测方法和属性。

关于python - 相同的绘制对象如何同时出现? (pygame),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490948/

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