python - 如何在pygame中生成随机矩形并使它们像飞扬的小鸟一样移动?

标签 python python-3.x pygame game-development

  1. 我是一个Python初学者。我想重新创建 chrome 恐龙游戏。随机矩形不会停止,循环会永远运行...请帮助我停止循环并使矩形移动。

代码:

import pygame
import random

pygame.init()

win = pygame.display.set_mode((500, 500))


#red rectangle(dino)
x = 20
y = 400
width = 30
height = 42
gravity = 5
vel = 18
black = (0, 0, 0)
#ground
start_pos = [0, 470]
end_pos = [500, 470]
#cactus
x1 = 20
y1 = 30

white = (2, 200, 200)

run = True
clock = pygame.time.Clock()
while run:

    clock.tick(30)

    pygame.time.delay(10)
    win.fill(black)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #random rectangle generation
    for i in range(1):
        width2 = random.randint(25, 25)
        height2 = random.randint(60, 60)
        top = random.randint(412, 412)
        left = random.randint(300, 800)
        rect = pygame.draw.rect(win, white, (left, top, width2,height2))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_UP]:
        y = y - vel

    else:
        y = min(428, y + gravity)

    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.draw.line(win, white, start_pos, end_pos, 2)
    pygame.display.update()
    pygame.display.flip()
pygame.quit()

最佳答案

pygame.draw.rect() des 不是“生成”矩形,而是在表面上绘制矩形。
pygame.Rect是一个矩形对象。在主应用程序循环之前创建 pygame.Rect 的实例:

obstracle = pygame.Rect(500, random.randint(0, 412), 25, 60)

改变矩形的位置:

obstracle.x -= 3
if obstracle.right <= 0:
    obstracle.y = random.randint(0, 412)

并将矩形绘制到窗口表面:

pygame.draw.rect(win, white, obstracle)

示例:

import pygame
import random

pygame.init()

win = pygame.display.set_mode((500, 500))

start_pos = [0, 470]
end_pos = [500, 470]
gravity = 5
vel = 18
black = (0, 0, 0)
white = (2, 200, 200)

hit = 0
dino = pygame.Rect(20, 400, 30, 40)

obstracles = []
number = 5
for i in range(number):
    ox = 500 + i * 500 // number
    oy = random.randint(0, 412)
    obstracles.append(pygame.Rect(ox, oy, 25, 60))

run = True
clock = pygame.time.Clock()
while run:

    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        dino.y -= vel
    else:
        dino.y = min(428, dino.y + gravity)

    for obstracle in obstracles:
        obstracle.x -= 3
        if obstracle.right <= 0:
            obstracle.x = 500
            obstracle.y = random.randint(0, 412)
        
        if dino.colliderect(obstracle):
            hit += 1
            
    win.fill(black)
    color = (min(hit, 255), max(255-hit, 0), 0)
    pygame.draw.rect(win, color, dino)
    for obstracle in obstracles:
        pygame.draw.rect(win, white, obstracle)
    pygame.draw.line(win, white, start_pos, end_pos, 2)
    pygame.display.update()

pygame.quit()

关于python - 如何在pygame中生成随机矩形并使它们像飞扬的小鸟一样移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59393279/

相关文章:

python - 如何创建命名元组的 __repr__?

python - 检测鼠标悬停并在鼠标悬停时发出声音?

python - while 循环中的按键识别。 Python

Python 字典作为文本存储在 MySQL 中

python - Django 通过 F() 更新日志和平方,需要一个 float

arrays - Python3从列表创建数组(我认为)

python - 为什么Pygame只播放最后一个声音?

python - 类型错误 : 'Keyword argument not understood:' , 'padding'

c++ - C++ 应用程序 (Sci)Python 之间的数据交换

python - 尝试在 Python 中对 .csv 文件进行排序(创建列表并删除重复项)