как сделать спрайт на экране, как библиотека черепах - PullRequest
0 голосов
/ 09 октября 2019

У меня есть функция рисования и готовый спрайт, и я пытаюсь соединить их так, чтобы это был рисунок черепахи на белом экране. Извините за длинный код, я все еще новичок в Python и изучаю меня и моего друга Натана.

оба кода работают, я не уверен насчет чертежа, но я планирую объединить их вместе

import pygame, itertools 
import sys

# define colors in the RGB format
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED       = ( 255,   0,   0)
ORANGE    = ( 255, 127,   0)
YELLOW    = ( 255, 255,   0)
GREEN     = (   0, 128,   0)
BLUE      = (   0,   0, 255)
PURPLE    = ( 128,   0, 128)
SALMON    = ( 250, 160, 122)
ORANGERED = ( 255,  69,   0)
GOLD      = ( 255, 215,   0)
KHAKI     = ( 240, 230, 140)
LIME      = (   0, 255,   0)
OLIVE     = ( 128, 128,   0)
CYAN      = (   0, 255, 255)
TEAL      = (   0, 128, 128)
NAVY      = (   0,   0, 128)
FUSCHIA   = ( 255,   0, 255)
INDIGO    = (  75,   0, 130)
FOREST    = (   0, 100,   0)

###variables by nathan
penUp = True
penColor = BLACK
colorList = ("BLACK", "WHITE", "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "PURPLE", "SALMON", "ORANGERED", "GOLD", "KHAKI", "LIME", "OLIVE", "CYAN", "TEAL", "NAVY", "FUSCHIA", "INDIGO", "FOREST")

#win size
windowX = (640)
windowY = (480)
win = pygame.display.set_mode ((windowX , windowY))
screenCenter = ( int(windowX/2)-1, int(windowY/2)-1 )
screenMargin = 0
###center by nathan
centerX = int((windowX / 2) - 1)
centerY = int((windowY/ 2) - 1)
center = [centerX , centerY]
#window name
pygame.display.set_caption("TRUTLE")
#player images
walkRight = [pygame.image.load('R1.jpg'), pygame.image.load('R2.jpg'), pygame.image.load('R3.jpg')]
walkLeft = [pygame.image.load('L1.jpg'), pygame.image.load('L2.jpg'), pygame.image.load('L3.jpg')]
walkUP = [pygame.image.load('up1.jpg'), pygame.image.load('up2.jpg'), pygame.image.load('up3.jpg')]
walkDown = [pygame.image.load('down1.jpg'), pygame.image.load('down2.jpg'), pygame.image.load('down3.jpg')]
stand = pygame.image.load('standing.jpg')

clock = pygame.time.Clock()

class player(object):
    def __init__(self, x, y, width, height) :
        self.x = x
        self.y = y
        self.width = 42
        self.height = 45
        self.vel = 5
        self.left = False
        self.right = False
        self.up = False
        self.down = False
        self.walkCount = 0    

    def draw(self,win) :
        if self.walkCount + 1 >= 27:
            self.walkCount = 0
        if self.left:
            win.blit(walkLeft[self.walkCount//9], (self.x,self.y))
            self.walkCount += 1
        elif self.right:
            win.blit(walkRight[self.walkCount//9], (self.x,self.y))
            self.walkCount += 1
        elif self.up:
            win.blit(walkUP[self.walkCount//9], (self.x,self.y))
            self.walkCount += 1
        elif self.down:
            win.blit(walkDown[self.walkCount//9], (self.x,self.y))
            self.walkCount += 1
        else:
            win.blit(stand, (self.x,self.y))


def redrawGameWindow():
 # fills screen with a background color    
    win.fill(WHITE)
    turtle.draw(win)
    pygame.display.update()

# If the pen is down, change to up. If up; down
def togglePen():
    global penUp
    if penUp == True:
        penUp = False
    elif penUp != True:
        penUp = True


# Change the pen width by using the keybinds in the main function
####this isn't working how nathan intended it to and changing
def changeWidth():
    penSize = 0
    change = 0
    penWidth = penSize + change
    ##pygame.draw.line()
##draw a straight line
##line(surface, color, start_pos, end_pos, width) -> Rect
##line(surface, color, start_pos, end_pos, width=1)


# Change the pen color to color
def changeColor(colors = itertools.cycle(colorList)):
    global penColor
    penColor = next(colors)

# Open the instruction file. Return a poiner to
# the file, or a list of the instructions in the
# file? You decide
def openInstructions( filename ):
    return

# Open the specified image file and write it to
# to the screen. Easiest to overwrite current
# screen contents, but layers may be possible...
def openBackground( filename ):
    return

# Output the contents of the screen to named file.
# If file already exists, prompt the user for
# Overwrite, Rename, Cancel
def saveAs( filename ):

    return

#main loop for
turtle = player(300, 410, 42, 45)
run = True
while run:
    clock.tick(27)

    for event in pygame.event.get() :
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

 # Suggested code by Evan Brown by edited by zaymes
    if keys[pygame.K_i]:
        penWidth = change + 1
 #print(penWidth)
    if keys[pygame.K_o]:
        if penWidth != 0:
            penWidth = changeWidth(change - 1)
#print(penWidth)
    if keys[pygame.K_p]:
        togglePen()
#print(penUp)
    if keys[pygame.K_c]:
        changeColor()
#print(penColor)

#left
    if keys[pygame.K_LEFT] and turtle.x > turtle.vel:
        turtle.x -= turtle.vel
        turtle.left = True
        turtle.right = False
#right
    elif keys[pygame.K_RIGHT] and turtle.x < windowX - turtle.width - turtle.vel:
        turtle.x += turtle.vel
        turtle.right = True
        turtle.left = False

# up
    elif keys[pygame.K_UP] and turtle.y > turtle.vel:
        turtle.y -= turtle.vel
        turtle.up = True
        turtle.down = False
#down
    elif keys[pygame.K_DOWN] and turtle.y < windowY - turtle.width - turtle.vel:
        turtle.y += turtle.vel
        turtle.down = True
        turtle.up = False
    else:
        turtle.right = False
        turtle.left = False
        turtle.down = False
        turtle.up = False
        turtle.walkCount = 0

    redrawGameWindow()


pygame.quit()      

извините за длинный код, я все еще новичок в Python и изучаю меня и моего друга Натана, написавших это.

...