連續試了兩天的程式都沒有弄懂,該怎麼讓角色顯現出來。
今天決定砍掉重練,把角色部分拉掉,重新再打一次。
總覺得這三天眼睛都要瞎了)笑哭
老樣子,先上程式碼:
import pygame
import random
import os
pygame.init()
black = (0, 0, 0)
green = (91,231,196)
gameNaughtybox = pygame.display.set_mode((600,600))
pygame.Surface((600,600))
dest = (0,0)
Frame_per_seconds = 27
pygame.display.set_caption ("Naughty Box")
walkRight = [pygame.image.load('/Users/RGB/iThome/Naughty box music/cutegirlfiles/Walk (1).png'),
pygame.image.load('/Users/RGB/iThome/Naughty box music/cutegirlfiles/Walk (2).png'),
pygame.image.load('/Users/RGB/iThome/Naughty box music/cutegirlfiles/Walk (3).png'),
pygame.image.load('/Users/RGB/iThome/Naughty box music/cutegirlfiles/Walk (4).png'),
pygame.image.load('/Users/RGB/iThome/Naughty box music/cutegirlfiles/Walk (5).png')]
walkLeft = [pygame.image.load('Angel-1-7.png'), pygame.image.load('Angel-1-6.png')]
character = [pygame.image.load ('cutegirlfiles/Idle (1).png')]
bg = pygame.image.load('pipo-battlebg007b.png')
gameNaughtybox.blit(bg,(0,0))
pygame.display.flip()
x = 50
y = 50
width = 64
height = 64
vel = 8
isJump = False
jumpCount = 15
left = False
right = False
walkCount = 0
run = True
clock = pygame.time.Clock()
gameNaughtybox.blit(bg, (x,0))
x -= 1
class character(pygame.sprite.Sprite):
def __init__(character, number, arranges):
character.image = pygame.image.load('cutegirlfiles/Idle (1).png').convert()
character.rect = character.image.get_rect()
character._layer = -10
character.sprite.Sprite.__init__(character, arranges)
character.moved = 0
character.number = number
character.rect.x = character.rect.width * character.number
def update(character):
character.rect.move_ip(-1, 0)
character.moved += 1
def redrawGameWindow():
global walkCount
gameNaughtybox.blit(bg,(0,0))
if walkCount + 1 >= Frame_per_seconds:
walkCount = 0
if left:
gameNaughtybox.blit (walkLeft[walkCount//3], (x,y))
walkCount +=1
elif right:
gameNaughtybox.blit (walkRight[walkCount//3], (x,y))
walkCount +=1
else:
gameNaughtybox.blit(character, (0,0))
pygame.display.update()
while run:
clock.tick(Frame_per_seconds)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys [pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
bg.scroll(5,0)
elif keys [pygame.K_RIGHT] and x < 600 - width - vel:
x += vel
left = False
right = True
bg.scroll(-5,0)
else:
right = False
left = False
walkCount = 0
if not (isJump):
if keys [pygame.K_SPACE]:
isJump = True
right = False
left = False
walkCount = 0
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
pygame.quit()
雖然知道要用 class
來定義,但是還是有點停留在模仿階段,目前整理出下面的樣子。
我覺得回去看昨天前天的程式碼,相比之下,現在真的整齊多了。
class character(pygame.sprite.Sprite):
def __init__(character, number, arranges):
character.image = pygame.image.load('cutegirlfiles/Idle (1).png').convert()
character.rect = character.image.get_rect()
character._layer = -10
character.sprite.Sprite.__init__(character, arranges)
character.moved = 0
character.number = number
character.rect.x = character.rect.width * character.number
def update(character):
character.rect.move_ip(-1, 0)
character.moved += 1
昨天和前天測試程式碼的時候,都會出現 Error 然後背景一直都不出來,真的覺得很暴躁。
今天好不容易背景出來了。
不過今天在修改這段程式碼的過程中,突然有點理解了,我的感覺是在一大段程式碼中,程式碼就像大行李箱,而 class
有點像是行李箱中的旅行袋,每個旅行袋都裝著不同類型的衣服。
當需要什麼樣的衣服的時候,就要拿出特定的行李袋。
有點難精確形容,不過希望這樣有比較形象化的解釋。
這個關卡了三天,坦白說超級不爽的。
不過我覺得這樣也挺好的,如果總是一兩天就做出成果了,就會真的以為自己是天才了。
學無止盡,要努力。
今天就先到這裡好了。
我的最後一天,收工!
心得文後補。