Coordinates for Moving Ball Bounce Off Canvas - python

I'm looking for this circle to rebound off the edges of the screen and bounce back into the canvas. I know you need to input coordinates just need a formula so I can understand what I'm looking at.
import tkinter # Used to draw on canvas (2D plot)
class MyCircle:
def __init__(self, x, y, dx, dy, cnum): # "self" always 1st parameter in python methods
self.x = x
self.y = y
self.dx = dx # "delta-x" or "change in x" or "x velocity"
self.dy = dy
self.cnum = cnum # Add circle number as parameter for log
# delete the log files to retry
# https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
def logData(self):
# Filename uses circle number.
with open('logfile_' + str(self.cnum) + '.txt', 'a') as f:
f.write("x=" + str(self.x) +
" y=" + str(self.y) +
" dx=" + str(self.dx) +
" dy=" + str(self.dy) + '\n')
def MoveIt(self, coordinates=tkinter.Canvas):
self.x = self.x + self.dx
self.y = self.y + self.dy
def DrawIt(self, w):
w.create_arc(self.x, self.y, self.x + 20, self.y + 20, start=0, extent=359,
fill='red')

TL;DR
Add a method called bounce and call it every frame (You can call it in the MoveIt method):
def bounce(self):
if not 0 <= self.x <= WIDTH:
self.dx *= -1
if not 0 <= self.y <= HEIGHT:
self.dy *= -1
We need to assume somethings here:
The edges (walls) has infinite mass. Means at the collisions the energy would not be passed from balls to the walls.
Collisions are elastic. Means the energy would not transferred to something else.
With these assumptions the collisions happens to be change in direction of speed vector of the ball.
If collision happens on left or right wall, you must change the sign (*-1) of the dx and if it happens on top or bottom walls you must change the sign of dy.

Related

Why is my enemy movement script not working in pygame [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I've been working on movement for the enemy in my game for a day or so and can't seem to get it working
the problem is it will chase the character for a little bit until it hits x or y zero and will stick there forever, and sometimes it will just stick in place when it hits the player
any help will be greatly appreciated
import pygame
import math
ALPHA = (0, 255, 0)
path = "data/chr/squid.png"
def findDist(coords1, coords2):
try:
return math.sqrt(pow(coords1[0] - coords2[0], 2) + pow(coords1[1] - coords2[1], 2))
except:
print("invalid dist input, ignoring")
class Squid(pygame.sprite.Sprite):
#player object
def __init__(self, X, Y):
pygame.sprite.Sprite.__init__(self)
self.speed = .01
self.images = []
for i in range(1, 5):
img = pygame.image.load(path).convert()
img.convert_alpha() # optimise alpha
img.set_colorkey(ALPHA) # set alpha
self.images.append(img)
self.image = self.images[0]
self.rect = self.image.get_rect()
self.rect.x = X
self.rect.y = Y
def chasePlayer(self, player):
dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
dist = math.hypot(dx, dy)
dx, dy = dx / dist, dy / dist
self.rect.x += dx * self.speed
self.rect.y += dy * self.speed
Since pygame.Rect is supposed to represent an area on the screen, a pygame.Rect object can only store integral data.
The coordinates for Rect objects are all integers. [...]
The fraction part of the coordinates gets lost when the new position of the object is assigned to the Rect object. If this is done every frame, the position error will accumulate over time.
If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables respectively attributes and to synchronize the pygame.Rect object. round the coordinates and assign it to the location (e.g. .topleft) of the rectangle.
x, y = # floating point coordinates
rect.topleft = round(x), round(y)
Read about Classes. If you want to spawn multiple enemies, you must create multiple Instance Objects of the Squid class. e.g.:
enemies = []
for _ in range(5):
x = random.randrange(screen_width)
y = random.randrange(screen_height)
enemies.append(Squid(x, y))
Squid class:
class Squid(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.speed = .01
self.image = pygame.image.load(path).convert()
self.image.convert_alpha() # optimise alpha
self.image.set_colorkey(ALPHA) # set alpha
self.x = x
self.y = y
self.rect = self.image.get_rect(topleft = (round(x), round(y)))
def chasePlayer(self, player):
dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
dist = math.hypot(dx, dy)
dx, dy = dx / dist, dy / dist
self.x += dx * self.speed
self.y += dy * self.speed
self.rect.x = round(self.x)
self.rect.y = round(self.y)

Python Turtle/Tkinter Timers Accelerating

I thought the canonical way to do animation with Python Turtle Graphics was to do something like
def animate():
# move stuff
ontimer(animate, delay)
Looking into the source code for turtle this implements tkinter after() in the background.
Can someone explain why in the program below the animation accelerates and decelerates dramatically when it is left running?
My theory is that since a new .after() id is created each time ontimer() is called, there are somehow multiple timers in existence which interfere with each other? Or maybe it's just a result of the randomness in the program? Or maybe the short interval between callbacks causes problems?
from random import *
from turtle import *
import math
class Vector(object):
def __init__(self, x = 0.0, y = 0.0):
self.x = x
self.y = y
def move(self, other):
""" Move vector by other (in-place)."""
self.__iadd__(other)
def __iadd__(self, other):
if isinstance(other, Vector):
self.x += other.x
self.y += other.y
else:
self.x += other
self.y += other
def rotate(self, angle):
"""Rotate vector counter-clockwise by angle (in-place)."""
radians = angle * math.pi / 180.0
cosine = math.cos(radians)
sine = math.sin(radians)
x = self.x
y = self.y
self.x = x * cosine - y * sine
self.y = y * cosine + x * sine
ant = Vector(0, 0)
aim = Vector(2, 0)
def wrap(value):
"Wrap value around -200 and 200."
if value > 200:
value = -200
elif value < -200:
value = 200
return value
def draw():
"Move ant and draw screen."
ant.move(aim)
ant.x = wrap(ant.x)
ant.y = wrap(ant.y)
aim.move(random() - 0.5)
aim.rotate(random() * 10 - 5)
clear()
goto(ant.x, ant.y)
dot(10)
if running:
ontimer(draw, 50)
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
up()
running = True
draw()
done()
My belief is that your animation accelerates and decelerates because you use tracer() but fail to do an explicit update(). The tracer() function turns off animation but some turtle operations do an implicit update() as a side effect. Since you didn't do an explicit update() you're only getting random updates caused by those side effects.
Below I've added an explicit update() and simplified the code to make the turtle itself the moving object, rather than stamping and clearing. (BTW, if you save the result of stamp() you can ask it to clear itself.)
I've also switched from a circle to a turtle cursor image and added in logic to set the heading to the direction of motion:
from random import random
from turtle import Screen, Turtle, Vec2D
from math import pi, cos, sin
class Vector(object):
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def move(self, other):
""" Move vector by other (in-place)."""
self.__iadd__(other)
self.wrap()
def __iadd__(self, other):
if isinstance(other, Vector):
self.x += other.x
self.y += other.y
else:
self.x += other
self.y += other
def rotate(self, degrees):
""" Rotate vector counter-clockwise by angle (in-place). """
radians = degrees * pi / 180.0
cosine = cos(radians)
sine = sin(radians)
x = self.x
y = self.y
self.x = x * cosine - y * sine
self.y = y * cosine + x * sine
def position(self):
return Vec2D(self.x, self.y)
def wrap(self):
""" Wrap value around -200 and 200. """
x = self.x
y = self.y
if x > 200:
self.x = -200
elif x < -200:
self.x = 200
if y > 200:
self.y = -200
elif y < -200:
self.y = 200
def draw():
""" Move ant and draw screen. """
ant.move(aim)
position = ant.position()
turtle.setheading(turtle.towards(position))
turtle.setposition(position)
screen.update()
aim.move(random() - 0.5)
aim.rotate(random() * 10 - 5)
screen.ontimer(draw, 75)
screen = Screen()
screen.setup(420, 420)
screen.tracer(False)
turtle = Turtle()
turtle.hideturtle()
turtle.shape('turtle')
turtle.shapesize(0.5)
turtle.penup()
turtle.showturtle()
ant = Vector(0, 0)
aim = Vector(2, 0)
draw()
screen.mainloop()

Pong with Python(Tkinter) paddle not working

So I'm new to GUI and Tkinter. I'm working on a one paddle pong game using Tkinter and I need the paddle to act like a paddle and bounce the disk off when the disk hits the paddle from the right or the left. The current code I have does bounce the disk of the paddles location on the x axis but it doesn't let the disk get past the line_x. Am I thinking in the right direction? or am I way off? It would be awesome if someone can fix my code so it works. This is probably very easy for someone that's been working with GUI's a while but I'm stomped. Please help.
from tkinter import *
import random
class ControlAnimation:
def __init__(self):
my_window = Tk() # create a window
my_window.title("Control Animation Demo")
self.width = 400
self.height = 200
self.line_x = 350
self.line_top = 75
self.line_bot = 125
self.paddle_width = 10
self.dy = 5
self.sleep_time = 50
self.is_stopped = False
self.my_canvas = Canvas(my_window, bg = 'white', \
width = self.width, height = self.height)
self.my_canvas.pack()
frm_control = Frame(my_window) # for comand buttons below canvas
frm_control.pack()
btn_stop = Button(frm_control, text = 'Stop', \
command = self.stop)
btn_stop.pack(side = LEFT)
btn_resume = Button(frm_control, text = 'Resume', \
command = self.resume)
btn_resume.pack(side = LEFT)
btn_faster = Button(frm_control, text = 'Faster', \
command = self.faster)
btn_faster.pack(side = LEFT)
btn_slower = Button(frm_control, text = 'Slower', \
command = self.slower)
btn_slower.pack(side = LEFT)
self.radius = 20
self.x = self.radius # just to start; y is at canvas center
self.y = self.height/2
# (x, y) is center of disk for this program, but ...
# recall: x1,y1 and x2,y2 form a bounding box for disk
self.my_canvas.create_oval(\
self.x - self.radius, self.height/2 + self.radius,\
self.x + self.radius, self.height/2 - self.radius,\
fill = "red", tags = "disk")
self.my_canvas.create_line(self.line_x, self.line_top, \
self.line_x, self.line_bot, \
width = self.paddle_width, fill = "blue", tags = "paddle")
self.my_canvas.bind("<KeyPress-Up>", self.move_paddle)
self.my_canvas.bind("<KeyPress-Down>", self.move_paddle)
self.my_canvas.bind("<B1-Motion>", self.left_click_paddle)
self.my_canvas.bind("<B3-Motion>", self.right_click_paddle)
self.animate()
self.my_canvas.focus_set()
my_window.mainloop()
def stop(self):
self.is_stopped = True
def resume(self):
self.is_stopped = False
self.animate()
def faster(self):
if self.sleep_time > 5:
self.sleep_time -= 15
def slower(self):
self.sleep_time += 15
def animate(self):
dx = 3
dy = 2
while not self.is_stopped :
self.my_canvas.move("disk", dx, dy) # move right
self.my_canvas.after(self.sleep_time) # sleep for a few ms
# redraw/update the canvas w/ new oval position
self.my_canvas.update()
# increment x to set up for next re-draw
r = random.randint(-1, 1)
self.x += dx # moves the disk
if self.x + self.radius > self.width: # hit right boundary
dx = -dx + r # add randomness
elif self.x - self.radius <= 0: # hit left boundary
dx = -dx + r # add randomness
elif self.x + self.radius > self.line_x and self.x + self.radius <= self.line_top:
dx = -dx + r
#elif self.x - self.radius <= self.line_x:
#dx = -dx + r
# increment y to set up for next re-draw
self.y += dy
if self.y + self.radius > self.height: # hit bottom boundary
dy = -dy
elif self.y - self.radius <= 0: # hit top boundary
dy = -dy
def left_click_paddle(self, event):
print(" clicked at =", event.x, event.y)
print("-"*30)
self.move_paddle( -self.dy)
def right_click_paddle(self, event):
print(" clicked at =", event.x, event.y)
print("-"*30)
self.move_paddle( self.dy)
def move_paddle(self, increment):
self.line_top += increment
self.line_bot += increment
self.my_canvas.delete("paddle")
self.my_canvas.create_line(self.line_x, self.line_top, \
self.line_x, self.line_bot, \
width = 10, fill = "blue", tags = "paddle")
ControlAnimation() # create instance of the GUI
In your movement loop, the logic to check whether to trigger a direction change in x is not complete. It should be something like this:
# new disk x, y positions
self.x += dx
self.y += dy
# Change "dx" sign if the ball hit something horizontally
if self.x + self.radius > self.width-1:
# ball hit right frame boundary
dx = -dx + r
elif self.x - self.radius <= 1:
# ball hit left frame boundary
dx = -dx + r
elif ( self.line_x <= self.x+self.radius <= self.line_x + 2*dx
and self.line_top <= self.y <= self.line_bot ):
# ball hit paddle from the left
dx = -dx + r
elif ( self.line_x + 2*dx <= self.x-self.radius <= self.line_x
and self.line_top <= self.y <= self.line_bot ):
# ball hit paddle from the right
dx = -dx + r

Pygame - Rotate and move a spaceship (Polygon)

Ok, I've been working all day on this and I haven't found the logic.
I wanna make a classic style asteroids game, and I'm starting with the spaceship.
What I did was draw some lines with the shape of a spaceship:
import pygame
import colors
from helpers import *
class Ship :
def __init__(self, display, x, y) :
self.x = x
self.y = y
self.width = 24
self.height = 32
self.color = colors.green
self.rotation = 0
self.points = [
#A TOP POINT
(self.x, self.y - (self.height / 2)),
#B BOTTOM LEFT POINT
(self.x - (self.width / 2), self.y + (self.height /2)),
#C CENTER POINT
(self.x, self.y + (self.height / 4)),
#D BOTTOM RIGHT POINT
(self.x + (self.width / 2), self.y + (self.height / 2)),
#A TOP AGAIN
(self.x, self.y - (self.height / 2)),
#C A NICE LINE IN THE MIDDLE
(self.x, self.y + (self.height / 4)),
]
def move(self, strdir) :
dir = 0
if strdir == 'left' :
dir = -3
elif strdir == 'right' :
dir = 3
self.points = rotate_polygon((self.x, self.y), self.points, dir)
def draw(self, display) :
stroke = 2
pygame.draw.lines(display, self.color, False, self.points, stroke)
The ship looks like this:
Now important things to know:
The tuple (self.x, self.y) is the middle of the spaceship.
Using this function I managed to rotate (spin) it on command using the keys A and D
def rotate_polygon(origin, points, angle) :
angle = math.radians(angle)
rotated_polygon = []
for point in points :
temp_point = point[0] - origin[0] , point[1] - origin[1]
temp_point = (temp_point[0] * math.cos(angle) - temp_point[1] * math.sin(angle),
temp_point[0] * math.sin(angle) + temp_point[1] * math.cos(angle))
temp_point = temp_point[0] + origin[0], temp_point[1] + origin[1]
rotated_polygon.append(temp_point)
return rotated_polygon
The problem is: How can I make it move forward or backwards in the direction the spaceship is pointing?
OR
How can I update the self.x and self.y values and update them inside the self.points list and preserve rotation?
The easiest, most general way to deal with movement and rotation would to use some vector math (can apply to 3D graphics as well). You could keep a 2D vector representing the forward direction of your ship. For example, if your ship starts facing upwards and your (0,0) coordinate is the top left. You could do.
self.forward = Vector2D(0, -1) # Vector2D(x, y)
When you rotate you must rotate this vector. You can rotate using the following.
self.forward.x = self.forward.x * cos(angle) - self.forward.y * sin(angle)
self.forward.y = self.forward.x * sin(angle) + self.forward.y * cos(angle)
Then when you want to move the ship you can transform the ship points relative to this vector. For example.
self.x += forward.x * velocity.x
self.y += forward.y * velocity.y
I would highly recommend you write a little Vector2D class which can do some of the basic operations, e.g. dot, cross, mult, add, sub, normalize, etc.
If you are familiar with matrices then these operations can become easier if you implement them using matrices instead of a system of linear equations.
It seems to me that you should be able to simply do the following.
def updatePosition(self, dx, dy):
self.x += dx
self.y += dy
newPoints = []
for (x,y) in self.points:
newPoints.append((x+dx, y+dy))
self.points = newPoints

making a bullet move to your cursor using pygame

I am writing a relatively complex platformer game in which a player moves using wasd and shoots with the mouse. The goal is to get the bullet to travel to the location of the mouse when it was clicked. I have code that sort of works but as the angle gets farther from 0 or 90 (straight left/right or straight up/down) the the bullets final location gets farther from the cursor. I am fairly certain the issue is simply that since the change in x and y are floating points and x,y location of the bullet cannot be floating point there is a rounding issue occurring. I have tried numerous different methods based on forum searches and all of them have the same problem. I have attached the most relevant file (pay particular attention to the bullets init class). Any advice or help would be appreciated. For the record this is just the player class NOT the main.
import pygame
import level
import platform
##import enemies
import math
pygame.init()
## sets up colors that need to be used in every part of the program
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
class Player(pygame.sprite.Sprite):
## This class represents the player. It is inhariting from the Sprite class in Pygame
window=None
screen_width=0
screen_height=0
width=None
height=None
x_velocity=0
y_velocity=0
chrono_level=500
ball_damage=0
bomb_damage=0
blast_radius=0
gravity=0
isjumping=False
isducking=False
level=None
direction=None
def __init__(self,argwindow,argsheight,argswidth,argcolor=white,argwidth=40,argheight=60,argx=0,argy=0,argball_damage=5,argbomb_damage=15,argbomb_radius=10,argchrono_level=500):
## Constructor. Pass in the color, width, and height
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([argwidth,argheight])
self.image.fill(argcolor)
self.rect=self.image.get_rect()
## sets up the variables inital variables
self.window=argwindow
self.width=argwidth
self.height=argheight
self.screen_height=argsheight
self.screen_width=argswidth
self.rect.x=argx
self.rect.y=argy
self.x_velocity=0
self.y_velocity=0
self.ball_damage=argball_damage
self.bomb_damage=argbomb_damage
self.bomb_radius=argbomb_radius
self.chrono_level=argchrono_level
self.isjumping=False
self.isducking=False
def update(self):
## check gravity
self.calc_grav()
## move left or right
self.rect.x+=self.x_velocity
## check for any collisions
platform_hit_list=pygame.sprite.spritecollide(self,self.level.platform_list,False)## this is the pygame generatred collision detection builtin to the sprite class
for platform in platform_hit_list:
if self.x_velocity > 0:##i.e sprite was moving right
self.rect.right = platform.rect.left ##puts the right side of the sprite flush with the left side of the platform
elif self.x_velocity < 0:
self.rect.left = platform.rect.right
self.x_velocity=0
## move sprite up or down
self.rect.y+=self.y_velocity
## check for any collisions
platform_hit_list=pygame.sprite.spritecollide(self,self.level.platform_list,False)## this is the pygame generatred collision detection builtin to the sprite class
for platform in platform_hit_list:
if self.y_velocity > 0:## i.e. sprite is falling
self.rect.bottom = platform.rect.top ## puts bottom of player flush with top of platform
self.isjumping=False
elif self.y_velocity < 0:
self.rect.top = platform.rect.bottom
self.y_velocity=0
## check direction
pos = pygame.mouse.get_pos()
if pos[0] > (self.rect.x+(self.width/2)): ##i.e. cursor is farther to the right then the middle of the sprite
self.direction="Right"
else: ##pos[0] < (self.rect.x+(self.width/2))
self.direction="Left"
def jump(self):
if not self.isjumping:
self.y_velocity=-15
self.isjumping=True
def calc_grav(self):
if self.y_velocity ==0:
self.y_velocity=1
else:
self.y_velocity+=self.gravity
if self.rect.y >= self.screen_height - self.rect.height and self.y_velocity >= 0:
self.y_velocity = 0
self.rect.y = self.screen_height - self.rect.height
self.isjumping=False
def move_left(self):## called if the player hits the left arrow key or the a key
self.x_velocity=-5
def move_right(self):## called is the player hits the right arrow key or the d key
self.x_velocity=5
def stop(self):## called if the player lets up on either arrow key or the a or d key
self.x_velocity=0
def shoot(self,argmouse_position):
if self.direction=="Left":
bullet_start_x=self.rect.x
bullet_start_y=(self.rect.y+(self.height/2))
elif self.direction=="Right":
bullet_start_x=(self.rect.x+self.width)
bullet_start_y=(self.rect.y+(self.height/2))
bullet=player_bullet(bullet_start_x,bullet_start_y,argmouse_position)
return (bullet)
class player_bullet(pygame.sprite.Sprite):
bullet_x=None
bullet_y=None
bullet_x_velocity=None
bullet_y_velocity=None
target_x=None
target_y=None
speed=10
def __init__(self,argx,argy,argmouse_positon):
pygame.sprite.Sprite.__init__(self)
print "it inited"
self.image = pygame.Surface([4, 10])
self.image.fill(black)
self.rect=self.image.get_rect()
self.rect.x=argx
self.rect.y=argy
self.bullet_x=argx
self.bullet_y=argy
self.target_x=argmouse_positon[0]
self.target_y=argmouse_positon[1]
dx=self.target_x-self.bullet_x
dy=self.target_y-self.bullet_y
angle=math.atan2(dy,dx)
print angle
self.bullet_x_velocity=self.speed*math.cos(angle)
self.bullet_y_velocity=self.speed*math.sin(angle)
def update(self):
print self.rect.x
print self.bullet_x_velocity
self.rect.x+=self.bullet_x_velocity
print self.rect.x
self.rect.y+=self.bullet_y_velocity
def collide(self,argdisplay_width,argdisplay_height,argplatform_list):
Platform_hit_list=pygame.sprite.spritecollide(self, argplatform_list, False)
if len(Platform_hit_list) > 0:
return True
elif self.rect.x > argdisplay_width or self.rect.x < 0:
return True
elif self.rect.y > argdisplay_height or self.rect.y < 0:
return True
else:
return False
I have written a similar game mechanic, where instead of a bullet I could shoot any projectile, of any size, of any sprite. What I did was add my current position to number of pixels I have to move to (defined as vx, vy below). I found the number of pixels i have to move by dividing the differences in the axis, by distance, and the multiplying a speed ( usually 10). Here is the projectile class below(the mask stuff is so bullets don't go into the buildings):
class projectile:
"""an image goes towards the target from starting location"""
def __init__(self, xorg, yorg, x, y, sprite, speed):
self.x = xorg
self.y = yorg
self.gotox = x
self.gotoy = y
self.sprite = sprite
self.xsize = self.sprite.get_width()
self.ysize = self.sprite.get_height()
self.rect = Rect(self.x, self.y, self.xsize, self.ysize)
# divided by 2, because we want the middle of the projectile to goto the destination, not the edge
self.gotox -= self.xsize / 2
self.gotoy -= self.ysize / 2
self.speed = speed
#differance in the x and y axis of destination to current position
self.dx = self.gotox - self.x
self.dy = self.gotoy - self.y
self.slope = (self.dy / max(1, self.dx))
self.gotox += self.gotox * self.slope
self.gotoy += self.gotoy * self.slope
self.dist = max(1, hypot(self.dx, self.dy))
self.state = "alive"
self.rect = Rect(self.x, self.y, self.xsize, self.ysize)
def move(self):
"""moves based on where the target was during time of shooting
untill it hits targer, or hits a wall"""
global currentMask
dist = max(1, hypot(self.dx, self.dy))
# found the num of pixels I have to move (speed is usually 10)
self.vx = self.speed * (self.dx / self.dist)
self.vy = self.speed * (self.dy / self.dist)
if currentMask.get_at((int(self.x + self.vx), int(self.y + self.vy))) != (0, 0, 0) and currentMask.get_at(
(int(self.x + self.vx + self.xsize), int(self.y + self.vy + self.ysize))) != (0, 0, 0) and int(
self.y + self.vy + self.ysize) <= 800 - self.ysize and int(
self.y + self.vy) >= 0 + self.ysize and int(self.x + self.vx) >= self.xsize and int(
self.x + self.vx + self.xsize) <= 1200 - self.xsize:
# added the num of pixels i have to move, to my current postition
self.x += self.vx
self.y += self.vy
else:
self.state = "dead"
self.rect = Rect(self.x, self.y, self.xsize, self.ysize)
screen.blit(self.sprite, (self.x, self.y))

Categories

Resources