0

I have the draw function and the sprite ready and I'm trying to comebine them so it would be a turtle drawing on a white screen. Sorry for the long code I'm still new too python and learning me and my friend Nathan wrote this.

both codes work I'm not sure about the drawing but I plan on combining them together

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()      

sorry for the long code I'm still new too python and learning me and my friend Nathan wrote this.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Sinfro
  • 23
  • 4
  • 1
    you need only one even loop - so code from loop `while run` you have to use in loop `while done` using only one `for event in pygame.event.get():`. You should organize code putting all functions before `pygame.init()` – furas Oct 09 '19 at 00:20
  • I would record the path the turtle walks on the screen as a list of turtle/logo commands. Make a note of the position & current direction, and when the direction next changes, output the command. So starting from `None` direction at position `(100,100)`, the user moves the turtle *left*. Note that the state changed to left (with no previous). The user then continues to move left, before going *up*. At this change, say the turtle position is now `(30,100)` - a distance of `70` from the start-point. Save this `LEFT 70`. Note the new start-position, direction *Up*. – Kingsley Oct 09 '19 at 00:27
  • @Kingsley thank you I had that same Idea but I'm too new at this to know what to do. I've been to research on just that. – Sinfro Oct 09 '19 at 02:03
  • @furas yeah that's what I had at the moment. I just finished an edited version if that helps – Sinfro Oct 09 '19 at 02:04

0 Answers0