Issues with movement in pygame - python

I have my image moving correctly when I put in the inputs, but since I need the image to quit moving when I let go of the key, I naturally used pygame.KEYUP to make all movement stop. However, this leads to choppy gameplay because if you even hit a button a fraction of a second before you let off the one you were holding, your character stops all together, and sometimes it feels like it happens even when I do let off the button in time, though I suppose that could just be on me. Is there another way I can code the movement so that this isn't a problem?

I use:
if event.type == KEYDOWN:
if event.key == ord('d'):
Right = True
if event.type == KEYUP:
if event.key == ord('d'):
Right = False
(Somewhere else in the code...)
if Right == True:
PlayerX += 10
(Then)
screen.blit(PlayerImage,(PlayerX,PlayerY))

Just stop using events for movement.
Simply use pygame.key.get_pressed() to check which keys are pressed, then alter the position of your game object accordingly.
Here's a minimal example of a moving monkey:
import base64, pygame, math
from pygame.locals import *
def magnitude(v):
return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))
def add(u, v):
return [ u[i]+v[i] for i in range(len(u)) ]
def normalize(v):
vmag = magnitude(v)
return [ v[i]/vmag for i in range(len(v)) ]
pygame.init()
size = width, height = 640, 480
screen = pygame.display.set_mode(size)
img = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAgFRg8JRdZOyQYVj4lGSkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAArKysGPCMWiTwkFTg9JBfsPCQX4DwkFT07JBe9OiQWIwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAgFQA8JRcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA8IxemPCQX1DsjGEEAAAACAAAAAD0kGBU8JReEPCMX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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKqFZQD1yJgAAAAAAAAAAAAAAAAAsItniT7QnvTU0J7T98qZT6uFY3cAAAAAAAAAAAAAAAAAAAAAAAAAALiSba3U0J7TTtCeTT7QnvU6k2UxAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlHFTAP7QngDswJIAp4RkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALONaQDU0J4AAAAAAAAAAAAAAAAAqINhYf7QnvTU0J7TTtCeT7yWcLgAAAAAAAAAAAAAAAAAAAAAAAAAALSMaJX8zp3T7cCSTLmSbqukgFsOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAsItnAP7QngDU0J4A98qZAKuFYwAAAAAAAAAAAAAAAAAAAAAAAAAAALiSbQDU0J4AAAAAAAAAAAAAAAAApoVZFTbJmfT0yJfUwp12zQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACZc1kUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAqINhAP7QngDU0J4ATtCeALyWcAAAAAAAAAAAAAAAAAAAAAAAAAAAALSMaAD8zp0AAAAAAAAAAAAAAAAAAAAAAKOAX06ogF4mAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAApoVZAPbJmQD0yJcAwp12AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACZc1kA'
player = pygame.image.fromstring(base64.b64decode(img, 'UTF-8'), (36, 56), 'RGBA')
player_rect = player.get_rect()
# define which keys move the monkey in which direction
move_map = {pygame.K_w: ( 0, -1),
pygame.K_s: ( 0, 1),
pygame.K_a: (-1, 0),
pygame.K_d: ( 1, 0)}
def main():
running = True
clock = pygame.time.Clock()
while running:
screen.fill(pygame.Color("darkgreen"))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# get all pressed keys
pressed = pygame.key.get_pressed()
# get all directions the monkey should move
move = [move_map[key] for key in move_map if pressed[key]]
# add all directions together to get the final direction
reduced = reduce(add, move, (0, 0))
if reduced != (0, 0):
# normalize the target direction and apply a speed
move_vector = [c * 5 for c in normalize(reduced)]
# move the monkey...
player_rect.move_ip(*move_vector)
# ...but keep it inside the screen
player_rect.clamp_ip(screen.get_rect())
screen.blit(player, player_rect)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()

Related

Pygame screen doesn't seem to refresh

I've been trying to make a Chrome Dino Game, however, I'm struggling with this problem:
On every frame, it should draw a new one at the new position and delete the previous one to make it look as if it's moving. HOWEVER, it remains at its previous position and a new image appears on its next position. I did write the pygame.display.update() code at the end of my maintop.
In the last time I ran into a similar problem, I managed to make it work by drawing a background image, but this time, it doesn't work.
following are my codes:
import pygame
import os
from random import randint
import schedule
pygame.init()
assets = os.path.join(os.path.dirname(__file__), "Assets")
screen_size = (screen_width, screen_height) = (1280, 720)
screen = pygame.display.set_mode(screen_size)
clock = pygame.time.Clock()
fps = 120
bg = pygame.image.load(os.path.join(assets, "IMG_15.png"))
ground = 700
running = True
spacebaridx = 0
gamestart = False
tick_on_start = 0
obs1 = pygame.image.load(os.path.join(assets, "colourmat/light_green.png"))
pygame.transform.scale(obs1, (100, 200))
obs2 = pygame.image.load(os.path.join(assets, "colourmat/light_green.png"))
pygame.transform.scale(obs2, (120, 200))
obs3 = pygame.image.load(os.path.join(assets, "colourmat/light_green.png"))
pygame.transform.scale(obs3, (150, 200))
ls_obs = []
def create_obs():
k = randint(1, 3)
if k == 1:
info = {"type":1, "img":obs1, "x":screen_width, "y":ground - 200, "tox":2}
ls_obs.append(info)
if k == 2:
info = {"type":2, "img":obs2, "x":screen_width, "y":ground - 200, "tox":2}
ls_obs.append(info)
else:
info = {"type":3, "img":obs3, "x":screen_width, "y":ground - 200, "tox":2}
ls_obs.append(info)
schedule.every(3).seconds.do(create_obs)
while running:
dt = clock.tick(fps)
if gamestart == True:
game_ticks = pygame.time.get_ticks() - tick_on_start
schedule.run_pending()
else:
game_ticks = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT: running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if spacebaridx == 0: # Press space to start / to tell whether it's the first press
spacebaridx += 1
gamestart = True
tick_on_start = pygame.time.get_ticks()
else:
pass # Jump
for o in ls_obs:
o["x"] += o["tox"] * -1
screen.blit(bg, (0, 0))
for o in ls_obs:
screen.blit(o["img"], (o["x"], o["y"]))
pygame.display.update()
pygame.quit()
This issue is occurring because you aren't clearing the display within each frame. In pygame, in order to clear the display, we need to use the fill method. So in your code, at the top of your game loop before the event loop, add screen.fill((0, 0, 0)). This will fill your screen in the color black. Don't worry, the black won't be shown if you draw the background on top of it. Now, when you add a new image, the previous images won’t be displayed.
Modified Game Loop
while running:
screen.fill((0, 0, 0))
dt = clock.tick(fps)
if gamestart == True:
game_ticks = pygame.time.get_ticks() - tick_on_start
schedule.run_pending()
else:
game_ticks = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT: running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if spacebaridx == 0: # Press space to start / to tell whether it's the first press
spacebaridx += 1
gamestart = True
tick_on_start = pygame.time.get_ticks()
else:
pass # Jump
for o in ls_obs:
o["x"] += o["tox"] * -1
screen.blit(bg, (0, 0))
for o in ls_obs:
screen.blit(o["img"], (o["x"], o["y"]))
pygame.display.update()
pygame.quit()

Trying to get my player to transfrom.flip python

I am struggling with pygame. I want my player to move and face right or left when I press the right or left key respectively. Instead it just flips. I don't know how to make it point to the left, or right and then walk.
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
swordsman = pg.transform.flip(swordsman, True, False)
x += speed
if event.key == pg.K_LEFT:
swordsman = pg.transform.flip(swordsman, True, False)
x -= speed
You can either check the state of the keys with pg.key.get_pressed() to see if ← or → are pressed (as in the example below) or alternatively, change the speed variable to another value in the event loop and then change the x position (x += speed) each frame in the while loop not in the event loop (reset speed to 0 when the key is released).
With regard to the image flipping, keep a reference to the original image and assign it to the swordsman variable. When the player wants to move in the other direction, flip the original image and assign it (just assign the original image if they move in the original direction).
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
# This is the original swordsman image/surface (just a
# rectangular surface with a dark topleft corner).
SWORDSMAN_ORIGINAL = pg.Surface((30, 50))
SWORDSMAN_ORIGINAL.fill((50, 140, 200))
pg.draw.rect(SWORDSMAN_ORIGINAL, (10, 50, 90), (0, 0, 13, 13))
# Assign the current swordsman surface to another variable.
swordsman = SWORDSMAN_ORIGINAL
x, y = 300, 200
speed = 4
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
# When the player presses right, flip the original
# and assign it to the current swordsman variable.
swordsman = pg.transform.flip(SWORDSMAN_ORIGINAL, True, False)
elif event.key == pg.K_LEFT:
# Here you can just assign the original.
swordsman = SWORDSMAN_ORIGINAL
# Check the state of the keys each frame.
keys = pg.key.get_pressed()
# Move if the left or right keys are held.
if keys[pg.K_LEFT]:
x -= speed
elif keys[pg.K_RIGHT]:
x += speed
screen.fill(BG_COLOR)
screen.blit(swordsman, (x, y))
pg.display.flip()
clock.tick(60)
pg.quit()
Is a swordsman a pygame.Sprite object? If it is, it should contain a pygame.Rect object in itself. Variable x is just a single variable, and its change has no effect on your swordsman. Try swordsman.rect.x = x

How to make paddles move in pygame pong?

I am a beginner of pygame. Recently I code a Pong game.
However, I cannot make paddles move when I press the certain keys in keyboard. Can someone helps me check the code. I think maybe I have some problems in giving paddles' new position. But I cannot fix it. And hopefully give me some hints about that.
Thanks!
Code below:
import pygame, sys, time,math
from pygame.locals import *
# User-defined functions
def main():
# Initialize pygame
pygame.init()
# Set window size and title, and frame delay
surfaceSize = (500, 400) # window size
windowTitle = 'Pong' #window title
frameDelay = 0.005 # smaller is faster game
# Create the window
pygame.key.set_repeat(20, 20)
surface = pygame.display.set_mode(surfaceSize, 0, 0)
pygame.display.set_caption(windowTitle)
# create and initialize red dot and blue dot
gameOver = False
color1=pygame.Color('white')
center1 = [250, 200]
radius1=10
score=[0, 0]
speed1=[4,1]
location1=[50, 150]
location2=[450, 150]
size=(5, 100)
position1=(0,0)
position2=(350,0)
rect1=pygame.Rect(location1,size)
rect2=pygame.Rect(location2,size)
# Draw objects
pygame.draw.circle(surface, color1, center1, radius1, 0)
# Refresh the display
pygame.display.update()
# Loop forever
while True:
# Handle events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# Handle additional events
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
location1[1] =+ 1
if event.key == pygame.K_p:
location2[1] =+ 1
if event.key == pygame.K_a:
location1[1] =- 1
if event.key == pygame.K_i:
location2[1] =- 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_q:
location1[1] =+ 0
if event.key == pygame.K_p:
location2[1] =+ 0
if event.key == pygame.K_a:
location1[1] =- 0
if event.key == pygame.K_i:
location2[1] =- 0
# Handle additional events
# Update and draw objects for the next frame
gameOver = update(surface,color1,center1,radius1,speed1,rect1,rect2,score,position1,position2)
# Refresh the display
pygame.display.update()
# Set the frame speed by pausing between frames
time.sleep(frameDelay)
def update(surface,color1,center1,radius1,speed1,rect1,rect2,score,position1,position2):
# Check if the game is over. If so, end the game and
# returnTrue. Otherwise, erase the window, move the dots and
# draw the dots return False.
# - surface is the pygame.Surface object for the window
eraseColor=pygame.Color('Black')
surface.fill(eraseColor)
moveDot(surface,center1,radius1,speed1,score,position1,position2)
pygame.draw.circle(surface,color1,center1,radius1,0)
r1=pygame.draw.rect(surface, color1, rect1)
r2=pygame.draw.rect(surface, color1, rect2)
if r1.collidepoint(center1) and speed1[0]<0:
speed1[0]=-speed1[0]
if r2.collidepoint(center1) and speed1[0]>0:
speed1[0]=-speed1[0]
def moveDot(surface,center,radius,speed,score,position1,position2):
#Moves the ball by changing the center of the ball by its speed
#If dots hits left edge, top edge, right edge or bottom edge
#of the window, the then the ball bounces
size=surface.get_size()
for coord in range(0,2):
center[coord]=center[coord]+speed[coord]
if center[coord]<radius:
speed[coord]=-speed[coord]
if center[coord]+radius>size[coord]:
speed[coord]=-speed[coord]
if center[0]<radius:
score[0]=score[0]+1
drawScore(center,surface,score,position2,0)
if center[0]+radius>size[0]:
score[1]=score[1]+1
drawScore(center,surface,score,position1,1)
def drawScore(center1,surface,score,position,whichscore):
FontSize=30
FontColor=pygame.Color('White')
String='Score : '
font=pygame.font.SysFont(None, FontSize, True)
surface1=font.render(String+str(score[whichscore]), True, FontColor,0)
surface.blit(surface1,position)
main()
You always use the rects rect1 and rect2 to draw your paddles. But to update their position, you try to change values in the lists location1 and location2.
Stop it. Just alter the rects. The easiest way is to use move_ip to change the rects in place.
Also, if you want to keep your paddles moving while the players keep the movement keys pressed, use pygame.key.get_pressed to get a list of all pressed keys (since pressing a key only generates a single KEYDOWN event, unless you mess with pygame.key.set_repeat, which you shouldn't).
So your code should look like this:
...
while True:
# Handle events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pressed = pygame.key.get_pressed()
if pressed[pygame.K_q]: rect1.move_ip(0, -1)
if pressed[pygame.K_a]: rect1.move_ip(0, 1)
if pressed[pygame.K_p]: rect2.move_ip(0, -1)
if pressed[pygame.K_i]: rect2.move_ip(0, 1)
gameOver = ...
...

python pygame doesn't recognize keyboard event

I want to make a program where I move a rectangle via the keyboard, but it doesn't move like it doesn't understand the event commands. I can't find what's wrong. I think the problem is the sequence of commands, but as a beginner, I can't find it. Can anyone help me? Thanks!
import pygame
import sys
from pygame.locals import *
fps = 30
fpsclock = pygame.time.Clock()
w = 640
h = 420
blue = (0, 0, 255)
white = (255, 255, 255)
x = w / 3
y = 350
boxa = 20
movex = 0
def drawwindow():
global screen
pygame.init()
screen = pygame.display.set_mode((w, h))
screen.fill(blue)
def drawbox(box):
if box.right > (w - boxa):
box.right = (w - boxa)
if box.left < 0:
box.left = 0
pygame.draw.rect(screen, white, box)
def main():
global x
global movex
drawwindow()
box1 = pygame.Rect(x, y, boxa, boxa)
drawbox(box1)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
movex = +4
if event.key == K_LEFT:
movex = -4
if event.type == KEYUP:
if event.key == K_RIGHT:
movex = 0
if event.key == K_LEFT:
movex = 0
x += movex
pygame.display.update()
fpsclock.tick(fps)
if __name__ == '__main__':
main()
Keyboard events are being accepted properly. That can be verified by sticking a print statement inside one of the if event.key == ... blocks.
One of the problems is you are never redrawing the box after initially drawing it. Every iteration of the game loop should redraw the background (ideally only the area that changes, but that's for later) and the box in its new position. Something like this:
while True:
# [event handling code omitted for brevity]
x += movex
drawwindow()
drawbox(box1)
pygame.display.update()
fpsclock.tick(fps)
However there's another problem. Changing x or movex has no effect on anything, because they are not used anywhere once the main loop is entered. Rather than x += movex, the box would move if its x attribute was changed, as in the following code:
while True:
# [event handling code omitted for brevity]
box1.x += movex # this line changed
drawwindow() # this line added
drawbox(box1) # this line added
pygame.display.update()
fpsclock.tick(fps)
Running your code with the changes above, the box now moves.

pygame key.set_repeat not working

I am new to pygame and I am trying to make pong in order to learn it. I am trying to make smooth controls so that holding down an arrow will work, but it is not working right now.
import sys, pygame
pygame.init()
size = (500, 350)
screen = pygame.display.set_mode(size)
x = 1
xwid = 75
yhei = 5
pygame.key.set_repeat(0, 500)
while True:
vector = 0
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
vector = 4
elif event.key == pygame.K_LEFT:
vector = -4
pygame.draw.rect(screen,(255,255,255),(x,size[1] - yhei,xwid,yhei),0)
pygame.display.update()
screen.fill((0,0,0))
x += vector
if x <= 0:
x = 0
elif x >= size[0] - xwid:
x = size[0] - xwid
why does this not work for holding down the left or right arrows?
pygame.key.set_repeat(0, 500)
If you set the delay parameter to 0, key repeat will be disabled. The documentation isn't quite clear about that:
pygame.key.set_repeat()
control how held keys are repeated
set_repeat() -> None
set_repeat(delay, interval) -> None
When the keyboard repeat is enabled, keys that are held down will generate
multiple pygame.KEYDOWN events. The delay is the number of
milliseconds before the first repeated pygame.KEYDOWN will be sent.
After that another pygame.KEYDOWN will be sent every interval
milliseconds. If no arguments are passed the key repeat is disabled.
When pygame is initialized the key repeat is disabled.
Emphasis mine.
You could set the delay to 1, and it would work as expected:
pygame.key.set_repeat(1, 10) # use 10 as interval to speed things up.
But note that you should not use set_repeat and the pygame.KEYDOWN event to implement movement. If you do, you won't be able to observe real single key strokes, since if the player presses a key, a whole bunch of pygame.KEYDOWN events would be created.
Better use pygame.key.get_pressed(). Have a look at his minimal example:
import pygame
pygame.init()
screen = pygame.display.set_mode((680, 460))
clock = pygame.time.Clock()
# use a rect since it will greatly
# simplify movement and drawing
paddle = pygame.Rect((0, 0, 20, 80))
while True:
if pygame.event.get(pygame.QUIT): break
pygame.event.pump()
# move up/down by checking for pressed keys
# and moving the paddle rect in-place
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]: paddle.move_ip(0, -7)
if keys[pygame.K_DOWN]: paddle.move_ip(0, 7)
# ensure the paddle rect does not go out of screen
paddle.clamp_ip(screen.get_rect())
screen.fill((0,0,0))
pygame.draw.rect(screen, (255,255,255), paddle)
pygame.display.flip()
clock.tick(60)
try it !
pygame.key.set_repeat(1,500)
I know what do you mean. Try this:
import sys, pygame
from pygame.locals import *
pygame.init()
size = (500, 350)
screen = pygame.display.set_mode(size)
x = 1
xwid = 75
yhei = 5
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_LEFT]:
x -= 4
if x <= 0:
x = 0
if key_pressed[K_RIGHT]:
x += 4
if x >= size[0] - xwid:
x = size[0] - xwid
pygame.draw.rect(screen,(255,255,255),(x,size[1] - yhei,xwid,yhei),0)
pygame.display.update()
screen.fill((0,0,0))

Categories

Resources