微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 pygame 中重复我的功能

如何解决如何在 pygame 中重复我的功能

所以我正在做一个项目,该项目需要复制一个功能并且在所有形状之间不留任何空间。

基本上,我的问题是我能找到路。

代码

# importing modules
import pygame
import sys
import random

# colors
white = (255,255,255)
blue = (0,255)
red = (255,0)
green = (4,0)
brown=  (165,42,42)
# starting pygame
pygame.init()
# making a screen
(width,height) = (500,500)
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
def grass_block(green,brown):
    pygame.draw.rect(screen,green,(395,90,50))
    pygame.draw.rect(screen,brown,10,20,50))
# geting the x button to work
while running:
    grass_block(green,brown)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.display.update()
    clock.tick(60)

pygame.display.quit()
pygame.quit()
exit()

附带问题 id=f 有人可以将矩形转换为 3d 对象并使每一面都带有纹理吗?

谢谢,所有回复的人

解决方法

如果您尝试重复草方块以便可以多次绘制,则需要对代码进行大量修改。

首先,你为什么要根据参数设置颜色

完全没有必要

def grass_block(green,brown):

而是把“块”的位置作为参数 def草块(x,y):

现在更改您的函数以反映这些值:

def grass_block(x,y):
    pygame.draw.rect(screen,green,(x,0 + y,90,50))
    pygame.draw.rect(screen,brown,10 + y,20 + y,50))

然后在你的while循环中有一个额外的循环重复该方法

while running:
    for i in range("The amount of block you need to make"):
#this should make another block every repetition to the right of the previous
        grass_block(395 + (90 * i),"starting Y value") 

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.display.update()
    clock.tick(60)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。