如何解决如何停止屏幕滚动并开始移动角色
在我正在制作的游戏中,我有背景滚动来模仿玩家的移动,但是我想要的是当您到达背景边缘时背景停止滚动并且玩家能够移动下角和边缘。我在这里待了大约2-3个小时,无法解决任何问题。这是我的代码。
import pygame
import time
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')
typewriter = pygame.mixer.sound('Music/Sounds/Typewriter.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)
notification = pygame.font.SysFont('Courier',150)
new = newfont.render("NEW GAME",True,black)
settings = newfont.render("SETTINGS",black)
quit = newfont.render("QUIT",black)
notification = notification.render("Windy Waters",black)
WIDTH = 1920
HEIGHT = 1080
playerx = 900
playery = 485
up = 0
down = 0
left = 0
right = 0
mouseposx = 175
mouseposy = 185
startx = 175
starty = 185
slotx = 175
sloty = 185
screenx = -9600
screeny = -5400
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"
level = 0
noti1 = False
map1 = pygame.image.load('Ground/Map 1.png')
screen = pygame.display.set_mode((WIDTH,HEIGHT),pygame.FULLSCREEN)
game = True
startarea = True
def redrawGameWindow():
global walkCount
global icon
Map()
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,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))
if noti1 == True:
screen.blit(notification,(400,300))
pygame.display.update()
typewriterplay = False
def update_fps():
fps = str(int(clock.get_fps()))
fps_text = font.render(fps,1,white)
return fps_text
def Map():
if level == 1:
screen.blit(map1,(screenx,screeny))
screen.blit(startscreen,0))
screen.blit(new,(650,488))
screen.blit(settings,(690,688))
screen.blit(quit,(790,888))
t = 0
while game:
if screenx >= -7000 and screenx <= - 6700:
pygame.mixer_music.set_volume(0.2)
typewriterplay = True
noti1 = True
else:
pygame.mixer_music.set_volume(0.4)
noti1 = False
if typewriterplay == True and t == 0:
typewriter.play(maxtime=3000)
t += 1
typewriterplay = False
for event in pygame.event.get():
print(event)
print(screenx,screeny)
if event.type == pygame.QUIT:
sys.exit()
while startarea:
print(event)
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()
startarea = False
level += 1
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 and playerx > 0 :
screenx += 10
if move_right and playerx < (1920 - 64):
screenx -= 10
if move_up and playery > 0:
screeny += 10
if move_down and playery < (1080 - 64):
screeny -= 10
redrawGameWindow()
clock.tick(60)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。