I'm making a game for a school assignment and every time the character moves, it moves back to its original position. I'm not particularly well versed in pygame but I've looked over time and time again and I can't really figure out what the problem is.
Any tips?
import math
import sys
import pygame
def game():
pygame.init()
clock = pygame.time.Clock()
white = (255,255,255)
black = (0,0,0)
purple = (127, 84, 253)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
running = False
player_x = 12
player_y = 12
velocity = 10
surface = pygame.display.set_mode((400,400))
player = pygame.Rect(player_x,player_y,20,20)
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:player.y-=50
if keys[pygame.K_a]:player.x-=50
if keys[pygame.K_s]:player.y+=50
if keys[pygame.K_d]:player.x+=50
if keys[pygame.K_ESCAPE]:pygame.quit()
pygame.display.flip()
pygame.display.update()
surface.fill((255, 255, 255))
clock.tick(40)
pygame.quit()
game()```
player_x = 12
player_y = 12
velocity = 10
Should be out of the while loop you are resetting them each frame which cause the problem
Related
This question already has an answer here:
Why is my pygame application loop not working properly?
(1 answer)
Closed 2 years ago.
I wrote this code but it didn't work , I mean the window went unresponding :(.Please help!! Python_Army;)
Is the problem my laptop or my laptop or is it the code and if you could improve my code please answer me as fast as possible!!
here is the code:
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
background = pygame.image.load('C:/Users/H.S/Desktop/background.png')
player = pygame.image.load('C:/Users/H.S/Desktop/player.png')
file = pygame.image.load('C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg')
pygame.display.set_icon((file))
pygame.display.set_caption('a bit racey')
clock = pygame.time.Clock()
gameDisplay.blit(background, (0,0))
x = 300
y = 500
c = input()
if c == a:
x-=1
y = 500
pygame.display.flip()
pygame.display.update()
gameDisplay.blit(player, (x,y))
elif c == d:
x+=1
y = 500
pygame.display.update()
pygame.display.flip()
gameDisplay.blit(player, (x,y))
elif c == w:
y+=1
x = 300
pygame.display.flip()
pygame.display.update()
gameDisplay.blit(player, (x,y))
elif c == s:
y-=1
x = 300
pygame.display.flip()
pygame.display.update()
gameDisplay.blit(player, (x,y))
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
You have to move and draw the player in the application loop. And of course you have to update the display in the application loop. The main application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
Use pygame.key.get_pressed() to get the states of the keys:
import pygame
pygame.init()
display_width, display_height = 800, 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
background = pygame.image.load('C:/Users/H.S/Desktop/background.png')
player = pygame.image.load('C:/Users/H.S/Desktop/player.png')
file = pygame.image.load('C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg')
pygame.display.set_icon((file))
pygame.display.set_caption('a bit racey')
clock = pygame.time.Clock()
x, y = 300, 500
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
x -= 1
if keys[pygame.K_d]:
x += 1
if keys[pygame.K_w]:
y -= 1
if keys[pygame.K_s]:
y += 1
gameDisplay.blit(background, (0,0))
gameDisplay.blit(player, (x, y))
pygame.display.flip()
I am making a game in pygame. In this game, the background image is large. On the screen, player only sees about 1/20th of the background image. I want, when player presses the left, right, up or down arrow keys, the background image moves respectively, but, it stops moving when player reaches the end of the image. I have no idea how to do this.
My code up to this point :-
import pygame
FPS = 60
screen = pygame.display.set_mode((1000, 1000))
bg = pygame.image.load('map.png')
clock = pygame.time.Clock()
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
Thanks in Advance :-
Get the sice of the background and the screen by get_size():
screen_size = screen.get_size()
bg_size = bg.get_size()
Define the initial start of the background in range [0, bg_size[0]-screen_size[0]]. e.g. center of the background:
bg_x = (bg_size[0]-screen_size[0]) // 2
Get the list of the key states by pygame.key.get_pressed():
keys = pygame.key.get_pressed()
Change bg_x dependent on the state of left and right:
if keys[pygame.K_LEFT]:
bg_x -= 10
if keys[pygame.K_RIGHT]:
bg_x += 10
Clamp bg_x to the range [0, bg_size[0]-screen_size[0]]:
bg_x = max(0, min(bg_size[0]-screen_size[0], bg_x))
blit the background at -bg_x on the screen:
screen.blit(bg, (-bg_x, 0))
See the example:
import pygame
FPS = 60
screen = pygame.display.set_mode((1000, 1000))
bg = pygame.image.load('map.png')
screen_size = screen.get_size()
bg_size = bg.get_size()
bg_x = (bg_size[0]-screen_size[0]) // 2
bg_y = (bg_size[1]-screen_size[1]) // 2
clock = pygame.time.Clock()
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
bg_x -= 10
if keys[pygame.K_RIGHT]:
bg_x += 10
if keys[pygame.K_UP]:
bg_y -= 10
if keys[pygame.K_DOWN]:
bg_y += 10
bg_x = max(0, min(bg_size[0]-screen_size[0], bg_x))
bg_y = max(0, min(bg_size[1]-screen_size[1], bg_y))
screen.blit(bg, (-bg_x, -bg_y))
pygame.display.flip()
I am trying to create a flappy bird game with python but i cant get multiple walls to appear on the page and move across.
I was wondering if there was a way to make multiple walls (rectangles) and move them without defining them individually. Im using Pygame.
import pygame
pygame.init()
white = (255,255,255)
yellow = (255,200,0)
green = (0,255,0)
displayHeight = 600
displayWidth = 500
gameDisplay = pygame.display.set_mode((displayWidth, displayHeight))
clock = pygame.time.Clock()
crash = False
def bird(b_X, b_Y, b_R, b_Colour):
pygame.draw.circle(gameDisplay, b_Colour, (b_X, b_Y), b_R)
def game_loop():
bX = 50
bY = 300
bR = 20
bColour = yellow
velocity = 0
gravity = 0.6
lift = -15
while crash == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.QUIT
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
velocity += lift
gameDisplay.fill(white)
bird(bX, bY, bR, bColour)
velocity = velocity + gravity
bY += velocity
bY = round(bY)
if bY > displayHeight:
bY = displayHeight
velocity = 0
if bY < 0:
bY = 0
velocity = 0
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
Is there a way to make a big population of blocks and move them individually without defining them one by one.
I do not know how the code exactly works out, but you can store multiple wall 'objects' in a list, and looping over that list every time you update the screen. In pseudocode, it works like this
wall-list = []
for wanted_wall_amount in range():
wall = create_a_wall
wall-list.append(wall)
game loop:
for wall in wall-list:
wall.x_coordinate += movespeed
because of the for loop within the game loop, every wall object you stored will update it's position, or whatever you want to move simultanously. I hope you understand the weird pseudocode
I am struggling with moving a drawn rectangle on the screen in pygame, I am trying to create a Snake game. I am very new to Python and object oriented programming in general so it is probably a stupid mistake. Code below.
#X coordinate of snake
lead_x = 300
#sets window position on screen
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (320,240)
#imports pygame module
import sys, pygame
#initialises pygame module
pygame.init()
#changes background colour to green
background_colour = 155, 188, 15
blue =(0,0,255)
red = (100,40,20)
#sets screen size
screen = pygame.display.set_mode((640, 480))
#changes the background colour
screen.fill(background_colour)
#creates rectangle on the screen
pygame.draw.rect(screen, blue, [lead_x,lead_x,10,10])
#updates display to show new background colour
pygame.display.update()
#Sets the window title to 'Python'
pygame.display.set_caption('Python')
#closes the window when user presses X
running = True
#if cross is clicked set running = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#Controls
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x -= 10
if event.key == pygame.K_RIGHT:
lead_x += 10
#if running = False close pygame window
if running == False:
pygame.quit()
You need to put pygame.draw.rect(screen, blue, [lead_x, lead_y, 10, 10]) in your game loop. As of right now you're drawing the rect on the screen only once, and that's in the beginning of the program. You want to continuously draw the rect at different lead_x and lead_y positions in order for a rect to move on the screen.
You should also put a screen.fill(background_colour) (to clear the previous drawing) and pygame.display.update() (to update the changes) in your loop as well.
EDIT: I noticed something: you probably want to create a variable lead_y and use pygame.draw.rect(screen, blue, [lead_x, lead_y, 10, 10]) so you don't move diagonally every time.
I have to move the rectangular object straight through the pygame window. I have tried some code with pygame. The code is
import pygame
from itertools import cycle
pygame.init()
screen = pygame.display.set_mode((300, 300))
s_r = screen.get_rect()
player = pygame.Rect((100, 100, 50, 50))
timer = pygame.time.Clock()
movement = "straight"
x = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise
if movement == 'straight':
x += 50
screen.fill(pygame.color.Color('Black'))
pygame.draw.rect(screen, pygame.color.Color('Grey'), player)
pygame.display.flip()
timer.tick(25)
Here the image didnt moves. What I need is that the image must be moved in a straight way.
x is adding, but that does not affect player, which actually affects the drawing of the rectangle.
import pygame
from itertools import cycle
pygame.init()
screen = pygame.display.set_mode((300, 300))
s_r = screen.get_rect()
timer = pygame.time.Clock()
movement = "straight"
x = 0
player = pygame.Rect((x, 100, 50, 50))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise
if movement == 'straight':
x += 10
player = pygame.Rect((x, 100, 50, 50))
if x >= 300:
x = 0
screen.fill(pygame.color.Color('Black'))
pygame.draw.rect(screen, pygame.color.Color('Grey'), player)
pygame.display.flip()
timer.tick(25)
You need to adjust the player rectangle each time you change x. From http://www.pygame.org/docs/ref/rect.html, you can see that the first two arguments are "left" and "top". So, if you want to the rectangle to move from left to right, you'll want something like this:
player = pygame.Rect((100 + x, 100, 50, 50))
pygame.draw.rect(screen, pygame.color.Color('Grey'), player)
import pygame
BLACK = pygame.color.Color('Black')
GREY = pygame.color.Color('Grey')
pygame.init()
screen = pygame.display.set_mode((300, 300))
screen_rect = screen.get_rect()
timer = pygame.time.Clock()
movement = "straight"
player = pygame.Rect(0, 100, 50, 50) # four aguments in place of tuple (,,,)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if movement == 'straight':
player.x += 10
if player.x >= 300: # check only when `player.x` was changed
player.x = 0
screen.fill(BLACK)
pygame.draw.rect(screen, GREY, player)
pygame.display.flip()
timer.tick(25)
BTW:
don't use raise to exit program.
use readable variable - not s_r but screen_rect
you don't need x - you have player.x
you can create rectangle only once
remove repeated empty lines when you add code to question