I'm working on making a game for a project at my university using pygame. All I'm trying to get done right now is create a ball that can be controlled to go back and forth on the screen when the user presses the left and right arrow keys. Honestly I don't really know what I'm doing, so I'm using the code in the pygame documentation that was used for pong as the base for my game. My code is below, and if someone knows why I'm getting the error that's in the title, please let me know.
from socket import *
from pygame.locals import *
except ImportError, err:
print "couldn't load module. %s" % (err)
""" Load image and return image object"""
fullname = name
image = pygame.image.load(fullname)
if image.get_alpha() is None:
image = image.convert()
image = image.convert_alpha()
except pygame.error, message:
print 'Cannot load image:', fullname
raise SystemExit, message
return image, image.get_rect()
def __init__(self, (xy)):
self.image, self.rect = load_png('ball.png')
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.hit = 0
self.speed = 10
self.state = "still"
self.state = "still"
self.movepos = [0,0]
if self.side == "left":
self.rect.midleft = self.area.midleft
newpos = self.rect.move(self.movepos)
self.rect = newpos
self.movepos = self.movepos - (self.speed)
self.state = "moveleft"
self.movepos = self.movepos + (self.speed)
self.state = "moveright"
running = True
(width, height) = (800, 600)
screen = pygame.display.set_mode((width, height))
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
screen.blit(background, (0, 0))
player = Ball("left")
playersprite = pygame.sprite.RenderPlain(player)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_q:
running = False
if event.key == K_LEFT:
if event.key == K_RIGHT:
elif event.type == KEYUP:
if event.key == K_UP or event.key == K_DOWN:
player.movepos = [0,0]
player.state = "still"
#screen.blit(background, ball.rect, ball.rect)
screen.blit(background, player.rect, player.rect)
#screen.blit(background, player2.rect, player2.rect)
if __name__ == '__main__': main()
All I'm trying to get done right now is create a ball that can be controlled to go back and forth on the screen when the user presses the left and right arrow keys.
You are massively over complicating this, you don't need 102 lines to move a ball around. I wrote and commented a simple example that I think could help you a lot. All you really need to do is detect key presses and then update an x and y variable then draw the image using the x and y variables.
import pygame screen_width = 1280 screen_height = 720 pygame.init() screen = pygame.display.set_mode((screen_width,screen_height)) BLACK = (0,0,0) ballImg = pygame.image.load("ball.jpg") ballPosition = [0,0] speed = 1 def game_loop(): while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() #get all the keys being pressed keys = pygame.key.get_pressed() #depending on what key the user presses, update ball x and y position accordingly if keys[pygame.K_UP]: ballPosition -= speed if keys[pygame.K_DOWN]: ballPosition += speed if keys[pygame.K_LEFT]: ballPosition -= speed if keys[pygame.K_RIGHT]: ballPosition += speed screen.fill(BLACK) #fill the screen with black screen.blit(ballImg, ballPosition) #draw the ball pygame.display.update() #update the screen game_loop()