pygame object moves for a second - python

The 'PlayerRect' in the code appears in the screen, and i can move it, but it only moves for a fraction of a second before returning to the position that it started. I've used different codes to give it movement, but they all have the same result. Any suggestions?
import pygame,sys,os
from pygame.locals import *
pygame.init
MOVERATE = 10
WINDOWWIDTH = 500
WINDOWHEIGHT = 500
def terminate():
pygame.quit()
sys.exit()
playerImage = pygame.image.load('Test_Block.png')
playerRect = playerImage.get_rect()
WHITE = (255,255,255,0)
WindowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.update()
WindowSurface.fill(WHITE)
mainClock = pygame.time.Clock()
while True:
moveLeft = moveRight = moveUp = moveDown = False
playerRect.topleft = (WINDOWHEIGHT / 2),(WINDOWWIDTH / 2)
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == ord('s'):
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.type == K_ESCAPE:
terminate()
if event.key == ord('a'):
moveLeft = False
if event.type == ord('d'):
moveRight = False
if event.key == ord('w'):
moveUp = False
if event.key == ord('s'):
moveDown = False
if moveLeft and playerRect.left > 0:
playerRect.move_ip(-1 * MOVERATE,0)
if moveRight and playerRect.right < WINDOWWIDTH:
playerRect.move_ip(MOVERATE,0)
if moveUp and playerRect.top >0:
playerRect.move_ip(0,-1 * MOVERATE)
if moveDown and playerRect.bottom < WINDOWHEIGHT:
playerRect.move_ip(0,MOVERATE)
WindowSurface.blit(playerImage,playerRect)
pygame.display.update()
mainClock.tick(30)

I suspect that the problem is that you reset the position of playerRect every time through the loop. Take the move_* = false line and the playerRect.topleft line and move them to before the while loop:
moveLeft = moveRight = moveUp = moveDown = False
playerRect.topleft = (WINDOWHEIGHT / 2),(WINDOWWIDTH / 2)
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
If that doesn't fix things, trace the position. On each loop iteration, print the coordinates of playerRect to a log file. Also trace the key events. This will discriminate between a failure in display from a failure in positioning.

Related

Pygame code will not actually start game?

We have been doing Pygame at school and its our first time ever using the language. This piece of code does not start the game properly and plays the game over sound. Any tips for fixing this so it actually works.
I got this piece of code from an online book called invent with python(https://inventwithpython.com/invent4thed/chapter21.html) and should be creating a game that allows for characters to move around the screen and interact with enemies but instead after pressing any key it will instead play the game over clip.
import pygame, random, sys
from pygame.locals import *
WINDOWWIDTH = 600
WINDOWHEIGHT = 600
TEXTCOLOR = (0,0,0)
BACKGROUNDCOLOR = (255, 255, 255)
FPS = 60
BADDIEMINSIZE= 10
BADDIEMAXSIZE = 40
BADDIEMINSPEED = 1
BADDIEMAXSPEED = 8
ADDNEWBADDIERATE =6
PLAYERMOVERATE = 5
def terminate():
pygame.quit()
sys.exit()
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key ==K_ESCAPE:
terminate()
return
def playerHasHitBaddie(playerRect, baddies):
for b in baddies:
if playerRect.colliderect(b["rect"]):
return True
return False
def drawText(text, font, surface, x, y):
textobj = font.render(text, 1,TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft =(x,y)
surface.blit(textobj,textrect)
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption("Dodger")
pygame.mouse.set_visible(False)
font = pygame.font.SysFont(None, 48)
gameOverSound= pygame.mixer.Sound("gameover.wav")
pygame.mixer.music.load("background.wav")
playerImage = pygame.image.load("Knight.png")
playerRect=playerImage.get_rect()
baddieImage = pygame.image.load("Skeleton.png")
windowSurface.fill(BACKGROUNDCOLOR)
drawText("Dodger",font, windowSurface,(WINDOWWIDTH / 3),(WINDOWHEIGHT / 3))
drawText("press a key to start. ", font, windowSurface, (WINDOWWIDTH / 3) - 30,(WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
topscore = 0
while True:
baddies = []
score = 0
playerRect.topleft =(WINDOWWIDTH / 2, WINDOWHEIGHT- 50)
moveLeft = moveRight = moveUp = moveDown = False
baddiesAddCounter = 0
pygame.mixer.music.play(-1,0.0)
while True:
score += 1
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type ==KEYDOWN:
if event.key == K_z:
reverseCheat = True
if event.key == K_x:
slowCheat= True
if event.key == K_LEFT or event.key == K_a:
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key ==K_d:
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == K_w:
moveDown= False
moveUp = True
if event.key == K_DOWNorevent.key == K_s:
moveUp =False
moveDown= True
if event.type == KEYUP:
if event.key == K_z:
reverseCheat = True
if event.key == K_x:
slowCheat = True
score = 0
if event.key == K_ESCAPE:
terminate()
if event.key == K_LEFT or event.key == K_a:
moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
moveRight = False
if event.key == K_UP or event.key == K_w:
moveUp = False
if event.key == K_DOWN or event.key == K_s:
moveDown = False
if event.type == MOUSEMOTION:
playerRect.centerx = event.pos[0]
playerRect.centery = event.pos[1]
if not reverseCheat and not slowCheat:
baddiesAddCounter +=1
if baddieAddCounter == ADDNEWBADDIERATE:
baddieAddCounter = 0
baddieSize = random.randint(BADDIEMINSIZE, BADDIEMAXSIZE)
newBaddie = {"rect": pygame.Rect(random.randint(0, WINDOWWIDTH - baddieSize), 0 - baddieSize, baddieSize, baddieSize), "speed": random.randint(BADDIEMINSPEED, BADDIEMAXSPEED), "surface":pygame.transform.scale(baddieImage,(baddieSize, baddieSize)),}
baddies.apped(newBaddie)
if moveLeft and playerRect.left > 0:
playerRect.move_ip(-1 * PLAYERMOVERATE, 0)
if moveRight and playerRect.right< WINDOWWIDTH:
playerRect.move_ip(PLAYERMOVERATE, 0)
if moveUp and playerRect.top > 0:
playerRect.move_ip(0, - 1 * PLAYERMOVERATE, 0)
if moveDown and playerRect.bottom <WINDOWHEIGHT:
playerRect.move_ip(0, PAYERMOVERATE)
for b in baddies:
if not reverseCheat and not slowCheat:
b["rect"].move_ip(0, b ["Speed"])
elif reverseCheat:
b["rect"].move_ip(0, -5)
elif slowCheat:
b["rect"].move_ip(0, 1)
for b in baddies[:]:
if b["rect"].top > WINDOWHEIGHT:
baddies.remove(b)
windowSurface.fill(BACKGROUNDCOLOR)
drawText("Score: %s" % (score), font, windowSurface, 10, 0)
drawText("Top Score: %s" % (topScore), font, windowSurface, 10, 40)
windowSurface.blit(playerImage, playerRect)
for b in baddies:
windowSurface.blit(b["surface"], b["rect"])
pygame.display.update()
if playerHasHitBaddie(playerRect, baddies):
if score > topScore:
topScore = score
break
mainClock.tick(FPS)
pygame.mixer.music.stop()
gameOverSound.play()
drawText("GAME OVER", font, windowSurface, (WINDOWWIDTH / 3),(WINDOWHEIGHT / 3))
drawText("Press a key to play again. ", font, windowSurface,(WINDOWWIDTH / 3) -80, (WINDOWHEIGHT / 3) + 50)
pygame .display.update()
waitForPlayerToPressKey()
gameOverSound.stop()
It seems to me like someone made some mistakes when typing out? the source code listing.
I downloaded https://inventwithpython.com/InventWithPython_resources.zip and compared your source with dodger.py. Here are some examples of the differences:
baddiesAddCounter should be baddieAddCounter
topscore should be topScore
baddies.apped should be baddies.append
PAYERMOVERATE should be PLAYERMOVERATE
b["rect"].move_ip(0, b ["Speed"]) should be b["rect"].move_ip(0, b ["speed"]) (Speed --> speed)
K_DOWNorevent should be K_DOWN or event
playerRect.move_ip(0, -1 * PLAYERMOVERATE, 0) should be playerRect.move_ip(0, -1 * PLAYERMOVERATE)
And the main reason everything doesn't work: the indentation is slightly wrong. For example, there are two while True loops, and the second one should be "in" the first one, not below it.

Python game not responding

So far, I'm almost done making this dodger/avoider game, I added sound as found on a tutorial on the internet, however, when I try to run the game, when I press a button to play, the game doesn't respond but when I press the mouse button, the sound plays and when I press the X button, which is close the program, the game runs, I don't have any idea on how to make the program run when I press any key and at the same time when I press the mouse button make the sound play, here is the following tutorial I used for doing this ('http://programarcadegames.com/index.php?chapter=bitmapped_graphics_and_sound.') Here is the code:
import pygame, random, sys
from pygame.locals import *
TEXTCOLOR = (0, 0, 0)
FPS = 60
BADDIEMINSIZE = 8
BADDIEMAXSIZE = 70
BADDIEMINSPEED = 1
BADDIEMAXSPEED = 12
ADDNEWBADDIERATE = 1
PLAYERMOVERATE = 3
WINDOWWIDTH = 1280
WINDOWHEIGHT = 780
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
background = pygame.image.load("background.png")
backgroundRect = background.get_rect
background_position = [0, 0]
def terminate():
pygame.quit()
sys.exit()
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE: # pressing escape quits
terminate()
return
def playerHasHitBaddie(playerRect, baddies):
for b in baddies:
if playerRect.colliderect(b['rect']):
return True
return False
def drawText(text, font, surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
# set up pygame, the window, and the mouse cursor
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Dodger')
pygame.mouse.set_visible(False)
# set up fonts
font = pygame.font.SysFont(None, 48)
# set up sounds
gameOverSound = pygame.mixer.Sound('gameover.wav')
click_sound = pygame.mixer.Sound("pistol.wav")
# set up images
playerImage = pygame.image.load('ship.png')
playerRect = playerImage.get_rect()
baddieImage = pygame.image.load('baddie.png')
background_image = pygame.image.load("background.png") .convert()
# Copy image to screen
screen.blit(background_image, background_position)
# Set positions of graphics
background_position = [0, 0]
# show the "Start" screen
drawText('Dodger', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
drawText('Press a key to start.', font, windowSurface, (WINDOWWIDTH / 3) - 30, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
topScore = 0
while True:
# set up the start of the game
baddies = []
score = 0
playerRect.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT - 50)
moveLeft = moveRight = moveUp = moveDown = False
reverseCheat = slowCheat = False
baddieAddCounter = 0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
click_sound.play()
while True: # the game loop runs while the game part is playing
score += 1 # increase score
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == ord('z'):
reverseCheat = True
if event.key == ord('x'):
slowCheat = True
if event.key == K_LEFT or event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == ord('s'):
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == ord('z'):
reverseCheat = False
score = 0
if event.key == ord('x'):
slowCheat = False
score = 0
if event.key == K_ESCAPE:
terminate()
if event.key == K_LEFT or event.key == ord('a'):
moveLeft = False
if event.key == K_RIGHT or event.key == ord('d'):
moveRight = False
if event.key == K_UP or event.key == ord('w'):
moveUp = False
if event.key == K_DOWN or event.key == ord('s'):
moveDown = False
if event.type == MOUSEMOTION:
# If the mouse moves, move the player where the cursor is.
playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery)
# Add new baddies at the top of the screen, if needed.
if not reverseCheat and not slowCheat:
baddieAddCounter += 1
if baddieAddCounter == ADDNEWBADDIERATE:
baddieAddCounter = 0
baddieSize = random.randint(BADDIEMINSIZE, BADDIEMAXSIZE)
newBaddie = {'rect': pygame.Rect(random.randint(0, WINDOWWIDTH-baddieSize), 0 - baddieSize, baddieSize, baddieSize),
'speed': random.randint(BADDIEMINSPEED, BADDIEMAXSPEED),
'surface':pygame.transform.scale(baddieImage, (baddieSize, baddieSize)),
}
baddies.append(newBaddie)
# Move the player around.
if moveLeft and playerRect.left > 0:
playerRect.move_ip(-1 * PLAYERMOVERATE, 0)
if moveRight and playerRect.right < WINDOWWIDTH:
playerRect.move_ip(PLAYERMOVERATE, 0)
if moveUp and playerRect.top > 0:
playerRect.move_ip(0, -1 * PLAYERMOVERATE)
if moveDown and playerRect.bottom < WINDOWHEIGHT:
playerRect.move_ip(0, PLAYERMOVERATE)
# Move the mouse cursor to match the player.
pygame.mouse.set_pos(playerRect.centerx, playerRect.centery)
# Move the baddies down.
for b in baddies:
if not reverseCheat and not slowCheat:
b['rect'].move_ip(0, b['speed'])
elif reverseCheat:
b['rect'].move_ip(0, -5)
elif slowCheat:
b['rect'].move_ip(0, 1)
# Delete baddies that have fallen past the bottom.
for b in baddies[:]:
if b['rect'].top > WINDOWHEIGHT:
baddies.remove(b)
# Draw the game world on the window.
screen.blit(background_image, background_position)
# Draw the score and top score.
drawText('Score: %s' % (score), font, windowSurface, 10, 0)
drawText('Top Score: %s' % (topScore), font, windowSurface, 10, 40)
# Draw the player's rectangle
windowSurface.blit(playerImage, playerRect)
# Draw each baddie
for b in baddies:
windowSurface.blit(b['surface'], b['rect'])
pygame.display.update()
# Check if any of the baddies have hit the player.
if playerHasHitBaddie(playerRect, baddies):
if score > topScore:
topScore = score # set new top score
break
mainClock.tick(FPS)
# Stop the game and show the "Game Over" screen.
gameOverSound.play()
drawText('You lose', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
drawText('Press a key to play again.', font, windowSurface, (WINDOWWIDTH / 3) - 80, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
gameOverSound.stop()
I have no idea what I'm doing wrong, Help me Pleease! Support on this would be appreciated.
You have two while True loops - one after another.
First one
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
click_sound.play()
is running till you click "X" button (pygame.QUIT). And it can only play sound when you click mouse (pygame.MOUSEBUTTONDOWN)
Remove this loop and use lines if/else in second while True loop.

pygame player is not updating

So I have this small piece of code, and it runs fine, but when i move the character, wherever the character was before, theres a picture of it behind it. imagine it as taking a pencil and drawing on a piece of paper, and thats basically how its showing up. I don't know where the code is wrong as it runs fine otherwise. It has this problem with any other way to display the player. Help?
import pygame,sys,os
from pygame.locals import *
pygame.init
MOVERATE = 8
WINDOWWIDTH = 1000
WINDOWHEIGHT = 1000
def terminate():
pygame.quit()
sys.exit()
playerImage = pygame.image.load('Test_Block.png')
playerRect = playerImage.get_rect()
WHITE = (255,255,255)
WindowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.update()
WindowSurface.fill(WHITE)
mainClock = pygame.time.Clock()
pygame.display.update()
while True:
playerRect.topleft = (WINDOWWIDTH /3, WINDOWHEIGHT / 3)
moveLeft = moveRight = moveUp = moveDown = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == ord('s'):
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.type == K_ESCAPE:
terminate()
if event.key == ord('a'):
moveLeft = False
if event.key == ord('d'):
moveRight = False
if event.key == ord('w'):
moveUp = False
if event.key == ord('s'):
moveDown = False
pygame.display.update()
if moveLeft and playerRect.left > 0:
playerRect.move_ip(-1 * MOVERATE,0)
if moveRight and playerRect.right < WINDOWWIDTH:
playerRect.move_ip(MOVERATE,0)
if moveUp and playerRect.top >0:
playerRect.move_ip(0,-1 * MOVERATE)
if moveDown and playerRect.bottom < WINDOWHEIGHT:
playerRect.move_ip(0,MOVERATE)
pygame.display.update()
WindowSurface.blit(playerImage, playerRect)
pygame.display.update()
mainClock.tick(30)
pygame.display.update()
You need to fill the screen with white to erase the old image. Use WindowSurface.fill(WHITE) in your code right before WindowSurface.blit(playerImage, playerRect) at the same indentation.

I keep getting syntax error when using def on python

NOTE: This is a very basic game i am working on as a practice project i get a syntax error when defining explosions ( near the end of the code list... also i am very new to programming so yeah... if anyone could help that would be great i am stuck because i am new so your help is more than appreciated
import pygame, aya, random
from pygame.locals import *
from threading import Timer
#set up pygame
pygame.init()
mainClock = pygame.time.Clock()
#set up the window
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
WindowSurface = pygame.display.set_mode ( (WINDOW_WIDTH,
WINDOW_HEIGHT),0)
pygame.display.set_caption("Get Home!!")
#set up color constants
BLACK = (0,0,0)
BLUE = (0, 0, 255)
#set winning text
textFont = pygame.font.sysFont ("impact", 60)
text = textFont.render ("Welcome Home!", True, (193, 0, 0))
#set up the player and breadcrumbs
mapCounter = 0
NEW_GHOST = 20
GHOST_SIZE = 64
playerImage = pygame.image.load("playerimage.jpg")
playerImageTwo = pygame.image.load("playerimage.jpg")
ghostImage = pygame.image.load("ghost image.jpg")
ghostImageTwo = pygame.image.load("ghost image2.jpg")
player = pygame.Rect (300, 100,40, 40)
ghost = []
for i in range(20):
ghost.append(pygame.Rect(random.randint(0, WINDOW_WIDTH - GHOST_SIZE),
random.randint(0, WINDOW_HEIGHT - GHOST_SIZE),
GHOST_SIZE, GHOST_SIZE))
#movement variables
moveLeft = False
moveRight = False
moveDown = False
MOVE_SPEED = 6
#run the game loop
startGame = True
while startgame == True:
#check for quit
for event in pygame.event.get () :
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
#keyboard variables
if event.key ++ K_LEFT:
moveRight = False
moveLeft = True
if event.key ++ K_RIGHT:
moveRight = False
moveLeft = False
if event.key ++ K_UP:
moveUp = True
moveDown = False
if event.key ++ K_DOWN:
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
ays.exit()
if event.key == K_LEFT:
moveLeft = False
if event.key == K_RIGHT:
moveRight = False
if event.key == K_UP:
moveUP = False
if event.key == K_DOWN:
moveDown = False
ghostCounter += 1
if ghostcounter >= NEW_GHOST:
#clear ghost array and add new ghost
ghostCounter = 0
ghost.append(pygame.Rect(random.randint(0, WINDOW_WIDTH - GHOST_SIZE),
random.randint(0, WINDOW_HEIGHT - GHOST_SIZE),
GHOST_SIZE, GHOST_SIZE))
#draw black background
windowSurface.fill(BLACK)
#move player
if moveDown and play.bottom < WINDOW_HEIGHT:
player.top += MOVE_SPEED
if moveUp and play.top > 0:
player.top -= MOVE_SPEED
if moveleft and play.left > 0:
player.left -= MOVE_SPEED
if moveRight and play.right < WINDOW_HEIGHT:
player.right += MOVE_SPEED
windowSurface.blit(playerImage, player)
for ghost in ghosts:
windowSurface.blit(ghostImage, ghost)
#check if player has intersected with ghost
for ghost in ghosts[:]:
if player.colliderect(ghost):
windowSurface.blit(ghostImageTwo,ghost
def explosion():
for ghost in ghosts:
if player.colliderect(ghost) and (moveLeft == False and
moveRight == False and moveUp == False and
moveDown == False):
ghosts.remove(ghost)
if player.colliderect(ghost) and (moveLeft == false and
moveRight == False and moveUp == False and moveDown == False):
t = Timer(3, explosion)
t.start()
if len(ghost == 0:
ghostCounter = 0
windowSurface.blit(text, (90, 104))
startgame = False
#draw the window
pygame.display.update()
mainClock.tick(40)
while startgame == False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
As #BurhanKhalid pointed out, you are missing ) at the end of line 111 (windowSurface.blit(ghostImageTwo,ghost), which is causing the error you noticed.
Additionally, you have numerous syntax errors. You define variables in a different case than you use them (startGame being used as startgame, you forget to close several other )s (line 124, etc). The list goes on.
Python is a forgiving language, but not that forgiving. Find an editor and use it, learn how to debug your code, and stop being sloppy. You will be unable to write code that works otherwise.

Basic Pygame Game

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?
One of your function calls is missing an argument. The line number of the error will tell you which one.
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!

Categories