问题描述
我一直在开发游戏,我想知道当我到现在为止我该如何使角色停留在屏幕上,否则它将不断消失在屏幕上,我是python和pygame的新手,所以我不知道该怎么办。我有一个受用户输入控制的.png图像,我已经尝试过
if event.type == pygame.K_w and playery > 0:
playery -= 10
if event.type == pygame.K_s and playery < 1920 - 64:
playery += 10
if event.type == pygame.K_a and playerx > 0:
playerx -= 10
if event.type == pygame.K_d and playerx < 1080 - 64:
playerx += 10
但是它不起作用,下面是代码:
import pygame
import sys
import os
pygame.mixer.init()
pygame.font.init()
pygame.init()
pygame.mixer.music.load('Music/Start.mp3')
click = pygame.mixer.sound('Music/Sounds/Click.wav')
pygame.mixer_music.play()
os.environ['SDL_VIDEO_CENTERED'] = "True"
clock = pygame.time.Clock()
walkRight = [pygame.image.load('Wraith/Wraith_01.png'),pygame.image.load('Wraith/Wraith_02.png'),pygame.image.load('Wraith/Wraith_03.png'),pygame.image.load('Wraith/Wraith_04.png'),pygame.image.load('Wraith/Wraith_05.png'),pygame.image.load('Wraith/Wraith_06.png'),pygame.image.load('Wraith/Wraith_07.png'),pygame.image.load('Wraith/Wraith_08.png'),pygame.image.load('Wraith/Wraith_09.png'),pygame.image.load('Wraith/Wraith_10.png'),pygame.image.load('Wraith/Wraith_11.png'),pygame.image.load('Wraith/Wraith_12.png'),pygame.image.load('Wraith/Wraith_01.png'),pygame.image.load('Wraith/Wraith_12.png')]
walkLeft = [pygame.image.load('Wraith/Wraith_13.png'),pygame.image.load('Wraith/Wraith_14.png'),pygame.image.load('Wraith/Wraith_15.png'),pygame.image.load('Wraith/Wraith_16.png'),pygame.image.load('Wraith/Wraith_17.png'),pygame.image.load('Wraith/Wraith_18.png'),pygame.image.load('Wraith/Wraith_19.png'),pygame.image.load('Wraith/Wraith_20.png'),pygame.image.load('Wraith/Wraith_21.png'),pygame.image.load('Wraith/Wraith_22.png'),pygame.image.load('Wraith/Wraith_23.png'),pygame.image.load('Wraith/Wraith_24.png'),pygame.image.load('Wraith/Wraith_13.png'),pygame.image.load('Wraith/Wraith_24.png')]
idle = [pygame.image.load('Wraith/Wraith_01_Idle.png'),pygame.image.load('Wraith/Wraith_02_Idle.png'),pygame.image.load('Wraith/Wraith_03_Idle.png'),pygame.image.load('Wraith/Wraith_04_Idle.png'),pygame.image.load('Wraith/Wraith_05_Idle.png'),pygame.image.load('Wraith/Wraith_06_Idle.png'),pygame.image.load('Wraith/Wraith_07_Idle.png'),pygame.image.load('Wraith/Wraith_08_Idle.png'),pygame.image.load('Wraith/Wraith_10_Idle.png'),pygame.image.load('Wraith/Wraith_11_Idle.png'),pygame.image.load('Wraith/Wraith_12_Idle.png'),pygame.image.load('Wraith/Wraith_01_Idle.png'),pygame.image.load('Wraith/Wraith_12_Idle.png')]
fullheart = [pygame.image.load('Wraith/Heart.png'),pygame.image.load('Wraith/Heart.png')]
halfheart = [pygame.image.load('Wraith/Halfheart.png'),pygame.image.load('Wraith/Halfheart.png')]
noheart = [pygame.image.load('Wraith/NoHeart.png'),pygame.image.load('Wraith/NoHeart.png')]
startscreen = pygame.image.load('Wraith/StartScreen.png')
sword = [pygame.image.load('Wraith/Sword.png'),pygame.image.load('Wraith/Sword.png')]
shield = [pygame.image.load('Wraith/Shield.png'),pygame.image.load('Wraith/Shield.png')]
redberries = [pygame.image.load('Wraith/RedBerries.png'),pygame.image.load('Wraith/RedBerries.png')]
blueberries = [pygame.image.load('Wraith/BlueBerries.png'),pygame.image.load('Wraith/BlueBerries.png')]
inventory = [pygame.image.load('Wraith/Inventory.png'),pygame.image.load('Wraith/Inventory.png')]
slots = [pygame.image.load('Wraith/Slots.png'),pygame.image.load('Wraith/Slots.png')]
notequipped = [pygame.image.load('Wraith/NotEquiped.png'),pygame.image.load('Wraith/NotEquiped.png')]
swordslot = [pygame.image.load("Wraith/SwordSlot.png"),pygame.image.load("Wraith/SwordSlot.png")]
shieldslot = [pygame.image.load('Wraith/ShieldSlot.png'),pygame.image.load('Wraith/ShieldSlot.png')]
white = (255,255,255)
black = (0,0)
font = pygame.font.SysFont("Arial",18)
newfont = pygame.font.SysFont('Ariel',150)
new = newfont.render("NEW GAME",True,black)
settings = newfont.render("SETTINGS",black)
quit = newfont.render("QUIT",black)
WIDTH = 1920
HEIGHT = 1080
playerx = 900
playery = 485
mouseposx = 175
mouseposy = 185
startx = 175
starty = 185
slotx = 175
sloty = 185
walkCount = 0
icon = 1
inv = 0
move_left = False
move_right = False
move_up = False
move_down = False
arangeinventory = False
accessinventory = False
mousepos = False
Heath = 6
ability1 = "Sword"
ability2 = "Shield"
ability3 = "Red Berries"
screen = pygame.display.set_mode((WIDTH,HEIGHT),pygame.FULLSCREEN)
game = True
startarea = True
def redrawGameWindow():
global walkCount
global icon
screen.blit(bg,(0,0))
screen.blit(update_fps(),(1900,0))
if walkCount + 1 >= 60:
walkCount = 0
if move_right:
screen.blit(walkRight[walkCount],(playerx,playery))
walkCount += 1
elif move_left:
screen.blit(walkLeft[walkCount],playery))
walkCount += 1
else:
screen.blit(idle[walkCount],playery))
walkCount += 1
if Heath == 6:
screen.blit(fullheart[icon],0))
screen.blit(fullheart[icon],(64,(128,0))
elif Heath == 5:
screen.blit(fullheart[icon],0))
screen.blit(halfheart[icon],0))
elif Heath == 4:
screen.blit(fullheart[icon],0))
screen.blit(noheart[icon],0))
elif Heath == 3:
screen.blit(fullheart[icon],0))
elif Heath == 2:
screen.blit(fullheart[icon],0))
elif Heath == 1:
screen.blit(halfheart[icon],0))
if ability1 == "Sword":
screen.blit(sword[icon],(1728,888))
if ability2 == "Shield":
screen.blit(shield[icon],(1792,760))
if ability3 == "Red Berries":
screen.blit(redberries[icon],(1601,952))
elif ability3 == "Blue Berries":
screen.blit(blueberries[icon],952))
if accessinventory:
screen.blit(inventory[icon],0))
screen.blit(slots[icon],(175,185))
screen.blit(slots[icon],(769,(1263,185))
screen.blit(swordslot[icon],(slotx,sloty))
screen.blit(shieldslot[icon],185))
pygame.display.update()
def update_fps():
fps = str(int(clock.get_fps()))
fps_text = font.render(fps,1,white)
return fps_text
screen.blit(startscreen,0))
screen.blit(new,(650,488))
screen.blit(quit,(790,888))
while game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
while startarea:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
startx,starty = pygame.mouse.get_pos()
if startx >= 624 and startx <= 1257 and starty >= 855 and starty <= 1028:
click.play()
sys.exit()
elif startx >= 624 and startx <= 1257 and starty >= 445 and starty <= 618:
click.play()
bg = pygame.image.load('Ground/BG.png')
startarea = False
else:
None
pygame.display.update()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
move_left = True
move_right = False
elif event.key == pygame.K_d:
move_right = True
move_left = False
elif event.key == pygame.K_w:
move_up = True
elif event.key == pygame.K_s:
move_down = True
elif event.key == pygame.K_i:
inv += 1
elif event.key == pygame.K_ESCAPE:
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
move_left = False
elif event.key == pygame.K_d:
move_right = False
elif event.key == pygame.K_w:
move_up = False
elif event.key == pygame.K_s:
move_down = False
if (inv % 2) == 0:
arangeinventory = False
accessinventory = False
else:
arangeinventory = True
accessinventory = True
if move_left:
playerx -= 10
if move_right:
playerx += 10
if move_up:
playery -= 10
if move_down:
playery += 10
redrawGameWindow()
clock.tick(60)
解决方法
尝试修改移动播放器的if语句。更改了它们,因此仅当x和y仍在屏幕内时才更改它们。像这样:
if move_left and playerx>0:
playerx -= 10
if move_right and playerx<(1920-64):
playerx += 10
if move_up and playery>0:
playery -= 10
if move_down and playery<(1080-64):
playery += 10
这仅允许玩家在不离开屏幕的情况下移动