【发布时间】:2020-02-14 15:51:33
【问题描述】:
我正在制作一个小型 2d 游戏,其目标是尽可能多地吃便便,但我在随机生成便便时遇到了麻烦。我希望便便在敌人的 y 位置生成,然后像 rpg 一样向前射击。
import pygame
from pygame.locals import *
from numpy.random import rand
pygame.init()
pygame.display.set_caption('STINKY BEETLE')
screen_width = 800
screen_height = 600
game_running = True
pl_x = int(screen_width/10)
pl_y = int(screen_height/2)
pl_width = 80
pl_height = 40
pl_vel = 30
en_width = 80
en_height = 40
en_x = screen_width - screen_width/10 - en_width
en_y = int(screen_height/2)
en_yvel = -10
po_width = 50
po_height = 30
po_x = 720
po_y = en_y
po_xvel = 15
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
while game_running:
clock.tick(10)
po_delay = rand(1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
if event.type == MOUSEBUTTONDOWN:
if event.button == 4 and pl_y > pl_vel:
pl_y -= pl_vel
elif event.button == 5 and pl_y < screen_height - pl_width:
pl_y += pl_vel
if po_delay < 0.01:
poop(po_x, po_y)
en_y += en_yvel
if en_y <= 0 or en_y >= screen_height - en_height:
en_yvel =- en_yvel
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (105, 255, 125), (pl_x, pl_y, pl_width, pl_height))
pygame.display.update()
pygame.draw.rect(screen, (255, 125, 115), (en_x, en_y, en_width, en_height))
pygame.display.update()
pygame.quit()
【问题讨论】:
-
到目前为止你尝试过什么?有什么工作吗?看来您在随机帧期间调用了
poop(po_x, po_y)函数,这个poop()函数有什么作用?你如何在屏幕上移动便便?如果您更详细地解释您现在的工作以及您遇到的下一步问题,我们可以更轻松地帮助您解决这个问题! -
我已经尝试了几种方法,例如,如果随机生成的数字低于某个阈值,它会生成一个 show_poop 变量。我有 true 并且 poop 函数只是另一个失败的尝试,我试图创建一个包含关于 poop 的所有信息的函数,然后如果随机生成的数字低于阈值,我就会调用该函数。跨度>