python - 你将如何在 pygame 中添加碰撞?
问题描述
我正在开发一个自上而下的僵尸 RPG,我主要是在僵尸向玩家移动时遇到问题,但没有与玩家或其他三个僵尸发生碰撞。而且代码变得非常重复,但由于大部分游戏都发生在主循环中,我不能用于范围。任何帮助将不胜感激。
import pygame, os, random
clock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('Dungeon Minigame')
scr_width = 1020
scr_height = 510
screen_size = (scr_width, scr_height)
screen = pygame.display.set_mode(screen_size)
images = {}
path = 'Desktop/Files/Dungeon Minigame/'
filenames = [f for f in os.listdir(path) if f.endswith('.png')]
for name in filenames:
imagename = os.path.splitext(name)[0]
images[imagename] = pygame.image.load(os.path.join(path, name)).convert_alpha()
white = [240, 240, 240]
font = pygame.font.SysFont('times_new_roman', 19)
lives_text = font.render('Lives: ', True, white)
px = 225
py = 220
score = 0
lives = 3
zx1 = random.randint(0, scr_width)
zy1 = random.randint(0, scr_height)
zx2 = random.randint(0, scr_width)
zy2 = random.randint(0, scr_height)
zx3 = random.randint(0, scr_width)
zy3 = random.randint(0, scr_height)
zx4 = random.randint(0, scr_width)
zy4 = random.randint(0, scr_height)
while True:
screen.blit(images['background'], (0, 0))
score_text = font.render('Score: ' + str(score), True, white)
screen.blit(score_text, (25, 20))
screen.blit(lives_text, (375, 20))
screen.blit(images['f_zombie'], (zx1, zy1))
screen.blit(images['f_zombie'], (zx2, zy2))
screen.blit(images['f_zombie'], (zx3, zy3))
screen.blit(images['f_zombie'], (zx4, zy4))
if lives == 3:
screen.blit(images['heart'], (430, 25))
screen.blit(images['heart'], (450, 25))
screen.blit(images['heart'], (470, 25))
if lives == 2:
screen.blit(images['heart'], (430, 25))
screen.blit(images['heart'], (450, 25))
screen.blit(images['empty_heart'], (470, 25))
if lives == 1:
screen.blit(images['heart'], (430, 25))
screen.blit(images['empty_heart'], (450, 25))
screen.blit(images['empty_heart'], (470, 25))
if lives == 0:
screen.blit(images['empty_heart'], (430, 25))
screen.blit(images['empty_heart'], (450, 25))
screen.blit(images['empty_heart'], (470, 25))
screen.blit(images['f_knight'], (px, py))
onpress = pygame.key.get_pressed()
if onpress[pygame.K_a]:
px -= 3
screen.blit(images['l_knight'], (px, py))
if onpress[pygame.K_w]:
py -= 3
screen.blit(images['b_knight'], (px, py))
if onpress[pygame.K_d]:
px += 3
screen.blit(images['r_knight'], (px, py))
if onpress[pygame.K_s]:
py += 3
screen.blit(images['f_knight'], (px, py))
if onpress[pygame.K_w] and onpress[pygame.K_a]:
screen.blit(images['b_knight'], (px, py))
if onpress[pygame.K_w] and onpress[pygame.K_d]:
screen.blit(images['b_knight'], (px, py))
if onpress[pygame.K_s] and onpress[pygame.K_d]:
screen.blit(images['r_knight'], (px, py))
if onpress[pygame.K_s] and onpress[pygame.K_a]:
screen.blit(images['l_knight'], (px, py))
if px <= -8:
px = -8
elif px >= 965:
px = 965
if py <= 5:
py = 5
elif py >= 440:
py = 440
zx1 += (px - zx1) * 0.009
zy1 += (py - zy1) * 0.009
zx2 += (px - zx2) * 0.009
zy2 += (py - zy2) * 0.009
zx3 += (px - zx3) * 0.009
zy3 += (py - zy3) * 0.009
zx4 += (px - zx4) * 0.009
zy4 += (py - zy4) * 0.009
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
clock.tick(60)
pygame.display.update()
解决方案
推荐阅读
- python - 如何在 Python 上纠正井字游戏的 While 循环?
- php - WooCommerce 中特定产品变体的自定义后缀
- amazon-web-services - 如何连接到 AWS 中应用程序负载均衡器后面托管的应用程序?
- javascript - 如何获取 Instagram 用户的个人资料图片?
- swift - Objective c 中的测试字符串扩展
- anaconda - conda 不安装软件包
- ios - 我只能在 Xcode 中使用后台获取来从 Internet 下载新内容吗?
- reactjs - 如何根据 localStorage 的状态更新组件?
- c# - 如何判断用户是否离开页面?
- azure - 标识符的搜索条件消失