2
votes

I've created a simple game (it's actually not really a game, just a rectangle that moves around on the screen (I hope)). I'm pretty new to pygame and not sure where I went wrong with this code.

import os, sys
import pygame
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()

WINDOWWIDTH = 400
WINDOWHEIGHT = 400
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption("Avoid!")

BLACK = (0, 0, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)

player = pygame.Surface((50, 50))

moveLeft = False
moveRight = False
moveUp = False
moveDown = False

MOVESPEED = 6

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                moveRight = False
                moveLeft = True
            if event.key == K_RIGHT:
                moveLeft = False
                moveRight = True
            if event.key == K_UP:
                moveDown = False
                moveUp = True
            if event.key == K_DOWN:
                moveUp = False
                moveDown = True
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == K_LEFT:
                moveRight = False
                moveLeft = True
            if event.key == K_RIGHT:
                moveLeft = False
                moveRight = True
            if event.key == K_UP:
                moveDown = False
                moveUp = True
            if event.key == K_DOWN:
                moveUp = False
                moveDown = True

    windowSurface.fill(WHITE)

    if moveDown and player.bottom < WINDOWHEIGHT:
        player.top += MOVESPEED
    if moveUp and player.top > 0:
        player.top -= MOVESPEED
    if moveLeft and player.left > 0:
        player.left -= MOVESPEED
    if moveRight and player.right < WINDOWWIDTH:
        player.right +=MOVESPEED

    windowSurface.blit(player)

I get this error message when I try to run it:

TypeError: Required argument 'dest' (pos 2) not found

Can anyone tell me where I went wrong?

2

2 Answers

2
votes

One of your function calls is missing an argument. The line number of the error will tell you which one.

0
votes

Currently, player is a surface object. In order to move it around as you do in the second to last group of lines, you will need to make it a rect(angle). On the line that currently says

player = pygame.Surface((50, 50))

you will want to put

player = pygame.Rect(0, 0, 50, 50)

(the arguments are left, top, width, height).

You will have to make something else into a surface, perhaps playerSO:

playerSO = pygame.Surface((50, 50))

on the last line, you will need to put

windowSurface.blit(playerSO, player)

The first argument is the Surface Object, and the second is the rect. These modifications will remove your error, but the program will probably still have other bugs that you will have to fix yourself. Good luck!