Reputation: 11
I am trying to build the border for my pygame (by following a youtube tutorial) and I've encountered an error that I need help to understand. I've tried to fix it but the error remains. Here is my code:
import pygame, sys, time, random, colorsys, math
from pygame.math import Vector2
from pygame.locals import *
def main():
x = 100
y = 100
velocity = 0
acceleration = 0.1
# Initialize the pygame
pygame.init()
# Create the screen
DISPLAY = pygame.display.set_mode((500, 400), 0, 32)
# Title and Icon
pygame.display.set_caption('Dragon')
icon = pygame.image.load('dragon.png')
pygame.display.set_icon(icon)
velocity = pygame.Vector2()
velocity.xy = 3, 0
WHITE=(255, 255, 255)
BLUE=(0, 0, 255)
DISPLAY.fill(WHITE)
while True:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
DISPLAY.fill((255, 255, 255))
pygame.draw.rect(DISPLAY, BLUE, (x, y, 50, 50))
x += velocity.x
if x + 50 > DISPLAY.get_width():
velocity.x = -3
if x < 0:
velocity.x = 3
y += velocity
velocity += acceleration
if keys[pygame.K_a]:
velocity = -3
pygame.display.update()
pygame.time.delay(10)
main()
And when I click run, the error is:
line 51, in <module>
main()
line 45, in main
y += velocity
TypeError: unsupported operand type(s) for +=: 'int' and 'pygame.math.Vector2'
Upvotes: 1
Views: 1232
Reputation: 211135
You can greatly simplify the code by using pygame.math.Vector2
objects for position
, velocity
and acceleration
:
position = pygame.Vector2(100, 100)
velocity = pygame.Vector2(3, 0)
acceleration = pygame.Vector2(0, 0.1)
The position
is changed by the velocity
and the velocity
is changed by the acceleration
:
position += velocity
velocity += acceleration
Complete example:
import pygame, sys
def main():
position = pygame.Vector2(100, 100)
velocity = pygame.Vector2(3, 0)
acceleration = pygame.Vector2(0, 0.1)
# Initialize the pygame
pygame.init()
# Create the screen
DISPLAY = pygame.display.set_mode((500, 400), 0, 32)
# Title and Icon
pygame.display.set_caption('Dragon')
icon = pygame.image.load('dragon.png')
pygame.display.set_icon(icon)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
velocity.y = -3
position += velocity
velocity += acceleration
if position.y + 50 > DISPLAY.get_height():
position.y = DISPLAY.get_height() - 50
if position.x + 50 > DISPLAY.get_width():
position.x = 0
DISPLAY.fill(WHITE)
pygame.draw.rect(DISPLAY, BLUE, (round(position.x), round(position.y), 50, 50))
pygame.display.update()
clock.tick(100)
pygame.quit()
sys.exit()
main()
Upvotes: 2
Reputation: 4181
You have two mistakes in these lines:
y += velocity
velocity += acceleration
y
is an int
, velocity
is a pygame.math.Vector2
and acceleration
is a float
.
I think you would like to have e.g.
y += velocity.y
velocity.y += acceleration
Pro tip: Use pygame.math.Vector2
for position
, velocity
and acceleration
to allow code like:
>>> dt = 0.1
>>> position = pygame.math.Vector2(0, 0)
>>> velocity = pygame.math.Vector2(3, 0)
>>> position += dt * velocity
>>> position
<Vector2(0.3, 0)>
Upvotes: 0