对于一个学校项目,我需要完成下面的pygame程序,实现向鼠标位置射击Kunais/Shurikens的方法,以便能够击中敌人的精灵
import pygame
import math
import random
from pygame.locals import *
pygame.init()
fenetre = pygame.display.set_mode((640,480), RESIZABLE)
pause = False
class perso():
def __init__(self,image,x=0,y=0,directionX=1,directionY=1):
self.image = pygame.image.load(image).convert_alpha()
self.x = x
self.y = y
self.directionX = directionX
self.directionY = directionY
def move(self):
if self.x==0:
self.directionX=1
if self.x==640-60:
self.directionX=-1
if self.y==0:
self.directionY=1
if self.y==480-100:
self.directionY=-1
self.x+=self.directionX
self.y+=self.directionY
fond = pygame.image.load("background.png").convert_alpha()
naruto = perso("naruto.png")
tobi = perso("tobi.png", 250, 100)
kunai = perso("kunai.png")
kunai.x = naruto.x + 40
kunai.y = naruto.y + 70
continuer = True
pygame.key.set_repeat(100, 25)
myfont = pygame.font.SysFont("monospace", 40)
colorRed = (255, 0, 0)
colorBlack = (0, 0, 0)
colorWhite = (255, 255, 255)
colorBlue = (0, 0, 255)
vieNaruto = 10
vieTobi = 3
result = 0
while continuer:
if int(naruto.x) >= int(tobi.x) and int(naruto.x) < int(tobi.x + 60) and int(naruto.y) >= int(tobi.y) and int(naruto.y) < int(tobi.y + 105):
vieNaruto -= 1
naruto.x = 0
naruto.y = 0
if int(naruto.x + 50) >= int(tobi.x) and int(naruto.x + 50) < int(tobi.x + 60) and int(naruto.y) >= int(tobi.y) and int(naruto.y) < int(tobi.y + 105):
vieNaruto -= 1
naruto.x = 0
naruto.y = 0
if int(naruto.x) >= int(tobi.x) and int(naruto.x) < int(tobi.x + 60) and int(naruto.y + 105) >= int(tobi.y) and int(naruto.y + 105) < int(tobi.y + 105):
vieNaruto -= 1
naruto.x = 0
naruto.y = 0
if int(naruto.x + 50) >= int(tobi.x) and int(naruto.x + 50) < int(tobi.x + 60) and int(naruto.y + 105) >= int(tobi.y) and int(naruto.y + 105) < int(tobi.y + 105):
vieNaruto -= 1
naruto.x = 0
naruto.y = 0
if vieNaruto <= 0:
result = 1
continuer = False
if vieTobi == 0:
result = 2
continuer = False
textVieNaruto = myfont.render("Vie Naruto : "+str(vieNaruto), True, colorRed)
textVieTobi = myfont.render("Vie Tobi : "+ str(vieTobi), True, colorRed)
for event in pygame.event.get():
"""GESTION DU CLAVIER"""
if event.type == QUIT:
continuer = False
elif event.type == KEYDOWN:
if event.key == K_SPACE:
pause = True
while pause == True:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key==K_SPACE:
pause = False
if event.key == K_UP and 5 <= naruto.y:
naruto.y -= 5
if event.key == K_DOWN and naruto.y <= 375:
naruto.y += 5
if event.key == K_LEFT and 5 <= naruto.x :
naruto.x -= 5
if event.key == K_RIGHT and naruto.x <= 575:
naruto.x += 5
tobi.move()
tobi.move()
fenetre.blit(fond,(0,0))
fenetre.blit(textVieNaruto, (400,0))
fenetre.blit(textVieTobi, (400,30))
fenetre.blit(naruto.image,(naruto.x,naruto.y))
fenetre.blit(tobi.image,(tobi.x,tobi.y))
pygame.time.Clock().tick(30)
pygame.display.update()
if result == 1:
while True :
fond = pygame.image.load("Defeat.png").convert_alpha()
fenetre.blit(fond,(0,0))
pygame.display.update()
if result == 2:
while True :
fond = pygame.image.load("Victory.png").convert_alpha()
fenetre.blit(fond,(0,0))
pygame.display.update()
额外的png文件在以下位置可见:https://repl.it/@LeVeveysan/ISNNaruto
有人能帮我吗?我知道如何获得鼠标位置,但我无法让类正常工作
辛辣地
让我们放弃一切,从头开始,利用pygame的功能,如精灵和向量数学
我们从pygame游戏的基本框架开始,一个简单的窗口:
我们的游戏将有不同的场景(标题场景、游戏场景、游戏场景),所以现在让我们来实现它们:
这允许我们通过按空格键循环浏览游戏场景
现在让我们实现核心游戏。首先,我们需要一些精灵,所以让我们创建一个} class
Actor
类,准备游戏场景以显示和重置精灵。我们使用pygame的一些基本功能,比如^{该采取行动了。让我们在演员中实施一些行为。我们创造了我们想要的每一种不同行为的功能。一个是托比在屏幕上移动,一个是火影忍者通过键盘控制,还有一个是库奈人
因为我们使用
Vector
类来表示精灵的位置和方向,所以要使kunai移动到鼠标位置,只需进行减法运算以下是完整的代码:
现在我们有了一个简单的游戏,它可以重复播放,并且很容易扩展。您可以随意使用此代码
相关问题 更多 >
编程相关推荐