Related
I am making a mini-game that involves movement. I created an object that moves according to the controls, but how do i make it not move if it collides with a wall?
#Imports
import pygame, sys
#Functions
#General Set Up
pygame.init()
clock = pygame.time.Clock()
#Main Window
swid = 1280
shgt = 700
screen = pygame.display.set_mode((swid, shgt))
pygame.display.set_caption("Raid: The Game || Movement Test")
#Game Rectangles
player = pygame.Rect(30, 30, 30, 30)
wall = pygame.Rect(140, 80, 1000, 20)
window = pygame.Rect(300, 400, 220, 20)
door = pygame.Rect(500, 500, 120, 18)
bgcol = (0,0,0)
playercol = (200,0,0)
wallcol = pygame.Color("grey12")
doorcol = pygame.Color("blue")
windowcol = (100,100,100)
xwalkspeed = 0
ywalkspeed = 0
while True:
#Handling Input
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
ywalkspeed += 10
if event.key == pygame.K_UP:
ywalkspeed -= 10
if event.key == pygame.K_LEFT:
xwalkspeed -= 10
if event.key == pygame.K_RIGHT:
xwalkspeed += 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
ywalkspeed -= 10
if event.key == pygame.K_UP:
ywalkspeed += 10
if event.key == pygame.K_LEFT:
xwalkspeed += 10
if event.key == pygame.K_RIGHT:
xwalkspeed -= 10
player.x += xwalkspeed
player.y += ywalkspeed
distancebetween = 0
if player.colliderect(wall):
ywalkspeed = 0
xwalkspeed= 0
if player.top <= 0:
player.top = 0
if player.bottom >= shgt:
player.bottom = shgt
if player.left <= 0:
player.left = 0
if player.right >= swid:
player.right = swid
#Visuals
screen.fill(bgcol)
pygame.draw.ellipse(screen, playercol, player)
pygame.draw.rect(screen, wallcol, wall)
pygame.draw.rect(screen, doorcol, door)
pygame.draw.rect(screen, windowcol, window)
#Updating the Window
pygame.display.flip()
clock.tick(60)
Whenever my Object collides upon a wall, it starts moving the opposite way until it is no longer seen in the screen.
I Found the Reason why it kept moving the opposite way when it collides the wall. It is because if I move towards the wall, my ywalkspeed = 10 and when I collided with the wall, it becomes 0, then if I started to let go of the movement key, the ywalkspeed becomes ywalkspeed = -10. I currently don't know a way how to fix this as I am just a noob.
Make a copy of the original player rectangle with copy(). Move the player. If a collision is detected, restore the player rectangle:
copy_of_player_rect = player.copy()
# move player
# [...]
if player.colliderect(wall):
player = copy_of_player_rect
You can watch for the distance between the wall and the player and when the distance according to axis positions reaches zero, just do
xwalkspeed = 0
ywalkspeed = 0
To measure the distance and smooth realistic movement, you can also use equations of motion from Physics.
After hours of searching, I still can't figure out why only the most recently spawned circle is affected by the collision detection. I commented out the code in question. I experimented with sprites and that may be the answer but I still got the same results.
import pygame,random
pygame.init()
width,height,radius = 1280,720,20
class Ball():
def __init__(self):
self.x = 0
self.y = 0
self.vx = 0
self.vy = 0
def make_ball():
ball = Ball()
ball.x = random.randrange(radius, width - radius)
ball.y = random.randrange(radius, 100)
ball.vx = random.randint(1,2)
ball.vy = 0
return ball
def main():
rect_x = 60
display = pygame.display.set_mode((width,height))
pygame.display.set_caption("BOUNCE")
running = True
ball_list = []
ball = make_ball()
ball_list.append(ball)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
ball = make_ball()
ball_list.append(ball)
for ball in ball_list:
ball.x += ball.vx
ball.vy += 0.02
ball.y += ball.vy
if ball.y >= height - radius:
ball.vy *= -1
if ball.x >= width - radius or ball.x <= radius:
ball.vx *= -1
display.fill((0,0,0))
for ball in ball_list:
random_color = (random.randint(1,255),random.randint(1,255),random.randint(1,255))
circle = pygame.draw.circle(display,random_color,(int(ball.x), int(ball.y)),radius)
rectangle = pygame.draw.rect(display,(255,255,255),(int(rect_x),660,60,60))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and rect_x > 0:
rect_x -= 2
if event.key == pygame.K_RIGHT and rect_x < width - 60:
rect_x += 2
'''if pygame.Rect(circle).colliderect(rectangle) == True: ###THIS IS THE BAD CODE!
print('Your Score:',pygame.time.get_ticks())
running = False'''
text = pygame.font.Font(None,120).render(str(pygame.time.get_ticks()),True,(255,255,255))
display.blit(text,(50,50))
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()
Indentation and code organization is the key to this. the offending section is (comments removed):
for ball in ball_list:
random_color = (random.randint(1,255),random.randint(1,255),random.randint(1,255))
circle = pygame.draw.circle(display,random_color,(int(ball.x), int(ball.y)),radius)
rectangle = pygame.draw.rect(display,(255,255,255),(int(rect_x),660,60,60))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and rect_x > 0:
rect_x -= 2
if event.key == pygame.K_RIGHT and rect_x < width - 60:
rect_x += 2
if pygame.Rect(circle).colliderect(rectangle) == True:
print('Your Score:',pygame.time.get_ticks())
running = False
You had all the correct pieces, but the order in which you are doing them is off as well as the indentation:
for ball in ball_list:
random_color = (random.randint(1,255),random.randint(1,255),random.randint(1,255))
circle = pygame.draw.circle(display,random_color,(int(ball.x), int(ball.y)),radius)
rectangle = pygame.draw.rect(display,(255,255,255),(int(rect_x),660,60,60))
if pygame.Rect(circle).colliderect(rectangle):
print('Your Score:',pygame.time.get_ticks())
running = False
This will now run through every ball in the list and check each one for collision. notice the colliderect if statement is indented into the for loop. Also notice i removed the KEYDOWN check from in the middle of it all
Speaking of that I would recommend using:
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT] and rect_x > 0:
rect_x -= 2
if pressed[pygame.K_RIGHT] and rect_x < width - 60:
rect_x += 2
for ball in ball_list:
# for loop from above
instead of what you had. This works best for when you want to allow for holding a key down. pygame.key.get_pressed() gets the state of all the keys all the time, not just when an event happens
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I'm making a platform game with pygame, and I would like to add gravity to it.
Right now I only have a picture which moves when I press the arrow keys, and my next step would be gravity. Here's my code:
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption("Jadatja")
WHITE = (255, 255, 255)
catImg = pygame.image.load("images/cat.png")
catx = 10
caty = 10
movingRight = False
movingDown = False
movingLeft = False
movingUp = False
while True: #main game loop
#update
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_RIGHT:
#catx += 5
movingRight = True
movingLeft = False
elif event.key == K_DOWN:
#caty += 5
movingDown = True
movingUp = False
elif event.key == K_LEFT:
#catx -= 5
movingLeft = True
movingRight = False
elif event.key == K_UP:
#caty -= 5
movingUp = True
movingDown = False
if event.type == KEYUP:
if event.key == K_RIGHT:
movingRight = False
if event.key == K_DOWN:
movingDown = False
if event.key == K_LEFT:
movingLeft = False
if event.key == K_UP:
movingUp = False
#actually make the player move
if movingRight == True:
catx += 5
if movingDown == True:
caty += 5
if movingLeft == True:
catx -= 5
if movingUp == True:
caty -= 5
#exit
for event in pygame.event.get():
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == QUIT:
pygame.quit()
sys.exit()
#draw
DISPLAYSURF.fill(WHITE)
DISPLAYSURF.blit(catImg, (catx, caty))
pygame.display.update()
fpsClock.tick(FPS)
I'm not 100% sure if this code is as smooth as I think it is, but I hope you guys can make something of it.
Thanks
There is a tutorial for creating a bouncing ball which I think might be helpful to you.
Now, to add gravity to that simulation, you'd simply add some extra speed in the y-direction every time through the loop:
speed[1] += gravity
What you end up with is kind of goofy however, since the image quickly descends below the bottom of the window never to be seen again :)
The next step is therefore to clip the position of the ball so it must remain in the window:
import os
import sys, pygame
pygame.init()
size = width, height = 320, 240
speed = [1, 1]
black = 0, 0, 0
gravity = 0.1
screen = pygame.display.set_mode(size)
image_file = os.path.expanduser("~/pybin/pygame_examples/data/ball.png")
ball = pygame.image.load(image_file)
ballrect = ball.get_rect()
def clip(val, minval, maxval):
return min(max(val, minval), maxval)
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
speed[1] += gravity
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
# clip the position to remain in the window
ballrect.left = clip(ballrect.left, 0, width)
ballrect.right = clip(ballrect.right, 0, width)
ballrect.top = clip(ballrect.top, 0, height)
ballrect.bottom = clip(ballrect.bottom, 0, height)
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
Okay, now you can incorporate that in your current code and you'll be off and running. However, there are some things you can do to make your code more organized and less repetitive.
For example, consider the massive if...then blocks that follow
for event in pygame.event.get():
You could rewrite it as something like:
delta = {
pygame.K_LEFT: (-20, 0),
pygame.K_RIGHT: (+20, 0),
pygame.K_UP: (0, -20),
pygame.K_DOWN: (0, +20),
}
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
deltax, deltay = delta.get(event.key, (0, 0))
ball.speed[0] += deltax
ball.speed[1] += deltay
You could also benefit from putting all the logic associated with the movement of your image into a class:
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.speed = [0, 0]
area = pygame.display.get_surface().get_rect()
self.width, self.height = area.width, area.height
def update(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > self.width:
self.speed[0] = -self.speed[0]
if self.rect.top < 0 or self.rect.bottom > self.height:
self.speed[1] = -self.speed[1]
self.rect.left = clip(self.rect.left, 0, self.width)
self.rect.right = clip(self.rect.right, 0, self.width)
self.rect.top = clip(self.rect.top, 0, self.height)
self.rect.bottom = clip(self.rect.bottom, 0, self.height)
Notice the update method is very similar to the code presented by the tutorial. One of the nice things about creating a Ball class is that the rest of your program does not need to know much about how a Ball moves. All the logic is in Ball.update. Moreover, it makes it easy to instantiate many balls. And you could create other classes (airplanes, birds, paddles, etc.) that move differently too and add them to your simulation relatively painlessly.
So, putting it all together, you would end up with something like this:
"""
http://stackoverflow.com/a/15459868/190597 (unutbu)
Based on http://www.pygame.org/docs/tut/intro/intro.html
Draws a red ball bouncing around in the window.
Pressing the arrow keys moves the ball
"""
import sys
import pygame
import os
image_file = os.path.expanduser("~/pybin/pygame_examples/data/ball.png")
delta = {
pygame.K_LEFT: (-20, 0),
pygame.K_RIGHT: (+20, 0),
pygame.K_UP: (0, -20),
pygame.K_DOWN: (0, +20),
}
gravity = +1
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.speed = [0, 0]
area = pygame.display.get_surface().get_rect()
self.width, self.height = area.width, area.height
def update(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > self.width:
self.speed[0] = -self.speed[0]
if self.rect.top < 0 or self.rect.bottom > self.height:
self.speed[1] = -self.speed[1]
self.rect.left = clip(self.rect.left, 0, self.width)
self.rect.right = clip(self.rect.right, 0, self.width)
self.rect.top = clip(self.rect.top, 0, self.height)
self.rect.bottom = clip(self.rect.bottom, 0, self.height)
def clip(val, minval, maxval):
return min(max(val, minval), maxval)
class Main(object):
def __init__(self):
self.setup()
def setup(self):
pygame.init()
size = (self.width, self.height) = (640,360)
self.screen = pygame.display.set_mode(size, 0, 32)
self.ball = Ball()
self.setup_background()
def setup_background(self):
self.background = pygame.Surface(self.screen.get_size())
self.background = self.background.convert()
self.background.fill((0, 0, 0))
self.screen.blit(self.background, (0, 0))
pygame.display.flip()
def draw(self):
self.screen.blit(self.background, (0, 0))
self.screen.blit(self.ball.image, self.ball.rect)
pygame.display.flip()
def event_loop(self):
ball = self.ball
friction = 1
while True:
for event in pygame.event.get():
if ((event.type == pygame.QUIT) or
(event.type == pygame.KEYDOWN and
event.key == pygame.K_ESCAPE)):
sys.exit()
elif event.type == pygame.KEYDOWN:
deltax, deltay = delta.get(event.key, (0, 0))
ball.speed[0] += deltax
ball.speed[1] += deltay
friction = 1
elif event.type == pygame.KEYUP:
friction = 0.99
ball.speed = [friction*s for s in ball.speed]
ball.speed[1] += gravity
ball.update()
self.draw()
pygame.time.delay(10)
if __name__ == '__main__':
app = Main()
app.event_loop()
making a simple game in pygame. have imported an image for my charactater and can move him around freely. I was wondering how to make the character slowly stop when the;
event.type == KEYUP:
as in, When I release the key the character will take say, 50 pixels from when the key has been released to the character slowly stopping.
I have tried a few different things but nothing seems to work. below is my code. any help would be great =]
bg="bg.jpg"
staticCharacter="toon.png"
import pygame, sys, time
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,360),0,32)
background=pygame.image.load(bg).convert()
staticToon = pygame.image.load(characterMid).convert_alpha()
x = 0
y = 0
movex = 0
movey = 0
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:
movex = -0.20
elif event.key == K_RIGHT:
movex = +0.20
elif event.key == K_UP:
movey = -0.20
elif event.key == K_DOWN:
movey = +0.20
if event.type == KEYUP:
if event.key == K_LEFT:
movex = 0
elif event.key == K_RIGHT:
movex = 0
elif event.key == K_UP:
movey = 0
elif event.key == K_DOWN:
movey = 0
x += movex
y += movey
if x > 560:
x = 560
if y > 280:
y = 280
if x < 0:
x = 0
if y < 0:
y = 0
screen.blit(background,(0,0))
screen.blit(staticToon,(x,y))
pygame.display.update()
distance = speed*time
Instead of augmenting the position (through movex or movey), you want to augment the speed:
delta = {
pygame.K_LEFT: (-20, 0),
pygame.K_RIGHT: (+20, 0),
pygame.K_UP: (0, -20),
pygame.K_DOWN: (0, +20),
}
...
elif event.type == pygame.KEYDOWN:
deltax, deltay = delta.get(event.key, (0, 0))
ball.speed[0] += deltax
ball.speed[1] += deltay
Now, to slow down the object (a ball in my example below), add some friction:
ball.speed[0] *= 0.95
ball.speed[1] *= 0.95
This diminishes the speed by a little bit each time through the event loop.
Move the object a distance equal to speed * time. To make things simple, let's just take change in time to be 1.
self.rect = self.rect.move(self.speed)
Here is a runnable example: (just add a correct path for staticCharacter.)
import sys
import pygame
import os
'''
Based on http://www.pygame.org/docs/tut/intro/intro.html
Draws a red ball bouncing around in the window.
Pressing the arrow keys moves the ball
'''
staticCharacter = "toon.png"
delta = {
pygame.K_LEFT: (-20, 0),
pygame.K_RIGHT: (+20, 0),
pygame.K_UP: (0, -20),
pygame.K_DOWN: (0, +20),
}
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(staticCharacter)
self.rect = self.image.get_rect()
self.speed = [2, 2]
self.area = pygame.display.get_surface().get_rect()
def update(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > self.area.width:
self.speed[0] = -self.speed[0]
if self.rect.top < 0 or self.rect.bottom > self.area.height:
self.speed[1] = -self.speed[1]
class Main(object):
def __init__(self):
self.setup()
def setup(self):
pygame.init()
size = (self.width, self.height) = (640,360)
self.black = (0, 0, 0)
self.screen = pygame.display.set_mode(size, 0, 32)
self.ball = Ball()
self.setup_background()
def setup_background(self):
self.background = pygame.Surface(self.screen.get_size())
self.background = self.background.convert()
self.background.fill(self.black)
self.screen.blit(self.background, (0, 0))
pygame.display.flip()
def draw(self):
self.screen.blit(self.background, (0, 0))
self.screen.blit(self.ball.image, self.ball.rect)
pygame.display.flip()
def event_loop(self):
ball = self.ball
while True:
for event in pygame.event.get():
if ((event.type == pygame.QUIT) or
(event.type == pygame.KEYDOWN and
event.key == pygame.K_ESCAPE)):
sys.exit()
elif event.type == pygame.KEYDOWN:
deltax, deltay = delta.get(event.key, (0, 0))
ball.speed[0] += deltax
ball.speed[1] += deltay
ball.speed = [0.99*s for s in ball.speed]
ball.update()
self.draw()
pygame.time.delay(10)
if __name__ == '__main__':
app = Main()
app.event_loop()
Normally, character movement is made to be independent from the machine speed. In your example, you do not know how many times per second the While loop goes, it is dependent on the computer speed. So firstly, you should use the pygame Clock to calculate the time difference between each call to move().
myclock = pygame.Clock()
delta = myclock.tick()
when you have the time difference, you can use a distance equation such as this:
position += speed * direction * delta
delta is in ms, so you need to set your speed accordingly. The direction would be -1 or +1 depending on the current key pressed. A keyup would set direction to 0.
Now back to the question. You would have to change the above a bit differently.
A keyup would subtract a small value from direction.
Then check if direction isn't 1 or -1 and then subtract a value from your direction. You would do this every loop. This way it will get smaller and smaller, and it will appear that your character is slowing down. It is good to also multiply the value that you subtract by delta so it will be deacceleration will constant on every computer.
My advice:
Go to the following website: http://karpathy.ca/phyces/tutorial1.php
Download pygamehelper and startercode (I promise it will make your life easier).
FPS is built-in so you don't need to make the adjustments within your movement logic.
Review some of the other tutorials if you want further clarification and use the vector library if you want some extra snaz.
Main program:
player = Player("player.png",[10,650])
players = pygame.sprite.Group()
players.add(player)
while True:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_RIGHT:
player.goright()
if event.key == K_LEFT:
player.goleft()
if event.type == KEYUP:
if event.key == K_RIGHT:
player.cangoright = False
if event.key == K_LEFT:
player.cangoleft = False
players.update()
players.draw(SCREEN)
pygame.display.update()
clock.tick(FPS)
The relevant sprite's functions:
def update(self):
if self.cangoright:
self.rect.left += self.speed
if self.cangoleft:
self.rect.left -= self.speed
def goright(self):
if self.rect.right <= 1024:
self.cangoright = True
else:
self.cangoright = False
def goleft(self):
if self.rect.left >= 0:
self.cangoleft = True
else:
self.cangoleft = False
The problem is that the "cangoright" and "cangoleft" flags don't seem to be working properly. When the sprite has surpassed the edges of the screen (0 on the left and 1024 on the right), the flags should be set to false, and thus the ifs in the update function should return false, but this doesn't happen.
This answer has some sample move methods. Maybe looking at how they were written would help?
Paddle.move()
def move(self, *, up=False, down=False):
if up or (not down and self.keys.up and
self.position.y - self.size.y > 0):
self.position -= Point(0, self.move_by)
if down or (not up and self.keys.down and
self.position.y + self.size.y < self.height):
self.position += Point(0, self.move_by)
Ball.move()
def move(self):
self.position += self.velocity
self.bounce()
I believe part of the problem is that you're only updating left in self.rect. Try updating both left and right instead, and see if that changes things.
More importantly, this code is a bit hard to read, and too complex what you're trying to do. You might be better served by checking the bounds of the screen in your update function instead of in your key event handler. In reality this is part of the physics of your game, so it should probably be hung off of there.
In other words, use the event loop to set flags which express the intention of the player to the update function. Use the update function to apply the rules of the game (physics, etc) to the game state based on the flags set by the event loop.
Maybe something like this?
while True:
...
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_RIGHT:
player.goright = True
if event.key == K_LEFT:
player.goleft = True
if event.type == KEYUP:
if event.key == K_RIGHT:
player.goright = False
if event.key == K_LEFT:
player.goleft = False
players.update()
players.draw(SCREEN)
pygame.display.update()
clock.tick(FPS)
def update(self):
if self.goright and self.speed < SPEED_MAX:
self.speed += ACCEL_INCREMENT
if self.goright and self.speed > -SPEED_MAX:
self.speed -= ACCEL_INCREMENT
new_left_pos = self.rect.left + self.speed
new_right_pos = self.rect.right + self.speed
if new_left_pos > 0 and new_right_pos < 1024:
self.rect.left += self.speed
self.rect.right += self.speed
The if statments are slightly off.
if self.rect.right <= 1024: #Shouldn't this be < 1024 and not <=
self.cangoright = True
Same for:
if self.rect.left >= 0: #Same thing here
self.cangoleft = True
Looks like your going one to far, thus placing your sprite just off the edge of the screen.
Hope this helps.