python - 如何在pygame游戏中的角色位置投掷炸弹
问题描述
我正在制作一个 pygame 游戏,其中一个角色当前在屏幕上四处移动,并且可以从商店购买物品。我希望角色能够将他从商店购买的炸弹扔到玩家所在的位置(我需要解决这个问题)。我附上了我的整个代码以及一些屏幕截图。任何帮助表示赞赏,谢谢。
import pygame
pygame.font.init()
width = 900
height = 600
screen = pygame.display.set_mode([width, height])
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'),
pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'),
pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
char = pygame.image.load('standing.png')
bomb_pic = pygame.transform.scale(pygame.image.load('bomb.png'), (20,20))
# char_rect = char.get_rect()
x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
left = False
right = False
down = False
up = False
walkCount = 0
shop = pygame.transform.scale(pygame.image.load("shop.png"), (60, 60))
clock = pygame.time.Clock()
FPS = 60
bag = {'bomb': 0}
print(bag["bomb"])
class Button():
def __init__(self, color, x, y, width, height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
def draw(self, win, outline=None):
# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 20)
text = font.render(self.text, 1, (0, 0, 0))
win.blit(text, (
self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))
def shop_run():
shop_bomb = Button((0, 200, 0), 820, 150, 60, 20, text="Bomb_q")
bright_green = (0, 255, 0)
green = (0, 200, 0)
shop_bomb.draw(screen)
def redrawGameWindow():
global walkCount
screen.fill([166, 166, 166])
pygame.draw.rect(screen, (0, 0, 0), (800, 0, 100, 600))
if x + char.get_width() < 60 and y + char.get_height() < 60:
shop_run()
screen.blit(shop, (0, 0))
if walkCount + 1 >= 27:
walkCount = 0
if left:
screen.blit(walkLeft[walkCount // 3], (x, y))
walkCount += 1
elif right:
screen.blit(walkRight[walkCount // 3], (x, y))
walkCount += 1
elif down:
screen.blit(char, (x, y))
walkcount = 0
elif up:
screen.blit(char, (x, y))
walkcount = 0
else:
screen.blit(char, (x, y))
walkCount = 0
buy = pygame.key.get_pressed()
if buy[pygame.K_SPACE] and bag["bomb"] >= 1:
screen.blit(bomb_pic,(x,y))
pygame.display.update()
def main():
run = True
# shopper()
pygame.display.set_caption("bomb-mania")
global x
global y
global width
global height
global vel
global isJump
global jumpCount
global left
global right
global down
global up
global walkCount
global bomb_pic
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if x + char.get_width() < 60 and y + char.get_height() < 60:
buy = pygame.key.get_pressed()
if buy[pygame.K_q]:
bag["bomb"] += 1
print(bag["bomb"])
redrawGameWindow()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel - 15:
x -= vel
left = True
right = False
down = False
up = False
elif keys[pygame.K_RIGHT] and x < 800 - vel - width:
x += vel
left = False
right = True[![enter image description here][1]][1]
down = False
up = False
elif keys[pygame.K_DOWN] and y < 600 - height:
y += vel
left = False
right = False
down = True
up = False
elif keys[pygame.K_UP] and y > vel - 15:
y -= vel
left = False
right = False
down = False
up = True
else:
left = False
right = False
down = False
up = False
walkCount = 0
clock.tick(FPS)
pygame.display.flip()
main()
解决方案
当你按下空格键时,你只是在画炸弹。你必须不断地画它。您需要在代码中添加这三件事(而不是直接在彼此之后,如此处所示)。
# Create a list to keep track on the bombs positions (do this **before**
# the game loop). Could be a global variable if you want.
bomb_positions = []
# Add a bomb when you press space. Do this in the event loop and not with
# 'pygame.key.get_pressed()', because that will add a new bomb every frame if
# you hold down the spacebar.
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
bomb_positions.append((x, y))
# Draw all the bombs at their position. If the list is empty, nothing will
# be drawn.
for bomb_position in bomb_positions:
screen.blit(bomb_pic, bomb_position)
推荐阅读
- sql - 我应该为议会投票模式使用哪种数据类型?
- c# - WPF PrintDialog.PrintVisual 在窗口 7 上不起作用
- python - Rasa 核心训练命令:无法识别的参数
- c - 谁能告诉我为什么添加功能不起作用?
- python - 在多个条件下比较两个不同大小的数据帧
- c# - 两个实例同时创建一个 StreamReader/FileStream,抛出异常 File is being used by another process
- python - 我正在尝试使用用户以前的输入自动填充表单的字段。在不刷新页面的情况下使用 AJAX 和 Django
- dart - 我们如何在飞镖中混合流?
- c# - 如何在 SQL 数据库中保存 unicode 字符
- google-cloud-functions - 如何检索从对话框流发送到云功能的 webhook 自定义标头?