The cube is only moving once.
import time
import pygame
pygame.init()
class Window(object):
def __init__(self, width, height, bg):
self.width = width
self.height = height
self.bg = bg
def create(self):
return pygame.display.set_mode((self.width, self.height))
The problem is somewhere here
class Cube(object):
def __init__(self, surface, x, y, width, height, color):
self.surface = surface
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
def move(self, lead_x_change=0, lead_y_change=0):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -20
elif event.key == pygame.K_RIGHT:
lead_x_change = 20
elif event.key == pygame.K_UP:
lead_y_change = -20
elif event.key == pygame.K_DOWN:
lead_y_change = 20
self.x += lead_x_change
self.y += lead_y_change
def draw(self):
pygame.draw.rect(self.surface, self.color, (self.x, self.y, self.width, self.height))
window = Window(800, 600, (0, 0, 0))
surface = window.create()
head = Cube(surface, 400, 300, 20, 20, (255, 255, 255))
starting gameloop
gameloop = True
while gameloop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
head.move()
surface.fill((0, 0, 0))
head.draw()
pygame.display.update()
The Cube moves, but only one time after a keypress. it should continiously move in one direction though, after pressing the direction.
I dont know how to make it move continiously, even watched ton of videos about it and still dont understand.
Based on the comment that OP wants the movement to occur until another key is pressed, as opposed to whether a key is held down, there is an entirely separate solution involved. Instead, the code must be refactored to instead include a variable for the last direction that was pressed, and a timed loop to continuously move in that direction.
In the Cube's __init__ method, add:
self.lastDir = null
Make a new method in the Cube class for handling the key event:
def updateDir(self, event):
if event.type == pygame.KEYDOWN and event.key in [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN]:
self.lastDir = event.key
Modify the move method to instead be based on self.lastDir instead of event.key:
if self.lastDir != null:
if self.lastDir == pygame.K_LEFT:
lead_x_change = -20
...
And finally, modify the main loop to call the move method on a regular basis, as opposed to waiting for a new move:
gameloop = True
while gameloop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
head.updateDir(event)
head.move()
surface.fill((0, 0, 0))
head.draw()
pygame.display.update()
# Insert time.sleep(seconds) here if too fast
Maybe because when you are calling the method
head.move()
and have in the method parameters set lead changes to 0.
and when it is called it's always 0.
Try changing
def move(self, lead_x_change=0, lead_y_change=0):
to
def move(self, lead_x_change, lead_y_change):
I'm not 100% sure this will work.
Let me know!
In the main loop, when you call pygame.event.get(), the program is waiting until a new event occurs. In pygame, only a new key press qualifies as an "event"; holding a key down will not continuously fire an "event".
Instead, you will need to call a different function. As per this question, pygame.key.get_pressed() is probably more what you are looking for. You will want to modify your main loop so it runs even when an event is not present, like so:
gameloop = True
while gameloop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Note the next line is un-indented, so it executes anyway
head.move()
surface.fill((0, 0, 0))
head.draw()
pygame.display.update()
And also fix the move() method to be structured like:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
lead_x_changed = -1 # Will move much faster now
elif keys[pygame.K_RIGHT]:
...
Related
I'm facing a problem in pygame where the sprite is not moving regardless the inputs, i've checked it several times but have no clue what's wrong with it. There are 3 pages, first contains the main loop of the game, second contains the code of player sprite and third contains some game functions.
main---------page-01
import pygame
import sys
from player import Player
import game_functions as gf
def run_game():
# Intialise the game and start the screen
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("AmarCreep")
player = Player(screen)
# Main loop
while True:
# Check if user wants to quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Navy screen
screen.fill((0,0,128))
gf.responses(screen, player)
player.p_movements()
# Make the player appear
player.draw_player()
# Make the newly made screen visible
pygame.display.flip()
run_game()
Player--------------------page-02
import pygame
from pygame.sprite import Sprite
class Player(Sprite):
# Initialise the main player
def __init__(self, screen):
super(Player, self).__init__()
self.screen = screen
self.screen_rect = screen.get_rect()
# Specifying the position of the player at start
self.rect = pygame.Rect(0, 0, 30, 30)
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = 590
self.moving_left = False
self.moving_right = False
self.moving_up = False
def p_movements(self):
if self.moving_left:
self.rect.x -= 5
if self.moving_right:
self.rect.x += 5
if self.moving_up:
self.rect.y -= 30
def draw_player(self):
''' Draw the player on the screen'''
pygame.draw.rect(self.screen, (255,255,255), self.rect)
game_functions------------------page-03
import pygame
def responses(screen, player):
''' Check for responses'''
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player.moving_up == True
elif event.key == pygame.K_LEFT:
player.moving_left == True
elif event.key == pygame.K_RIGHT:
player.moving_right == True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
player.moving_up == False
elif event.key == pygame.K_LEFT:
player.moving_left == False
elif event.key == pygame.K_RIGHT:
player.moving_right == False
The first thing you appear to do in your main event loop is to totally clear the queue while looking for a quit event. That's not going to necessarily go down well with your second event loop in the game functions file.
In fact, it's quite messy to have two event loops, one of which throws away anything that's not a quit event - this may well lead to lost events.
I suspect a better way would be to provide a response game function which just handles one event (and doesn't touch the event queue at all):
def response(screen, player, evt):
if evt.type == pygame.KEYDOWN:
blah blah blah
and have the main (now the only) event loop call that for events it does not want to handle itself:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
gf.response(screen, player, event)
You may also want to consider what you would expect when both the up and down button are pressed (or the left and right, for that matter). At a minimum, you probably want to make that a test case :-)
This question already has an answer here:
Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?
(1 answer)
Closed 2 years ago.
So i am currently working on a simple game that until now only has the game loop and a class that is used to create the player. I want to keep my game loop clean so I decided that it would be a good idea to put the code used to move the character into a method 'move' which is called on the end of the game loop before the player is drawn. Currently when i run the code my character just sits on the spot and sometimes goes left/right if i spam the keys but it seems pretty random. This is the whole code, is the method not called correctly/every tick? I hope you guys can help me
import pygame
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screenWidth, screenHeight = pygame.display.get_surface().get_size()
fpsclock = pygame.time.Clock()
def screen_refresh():
screen.fill((0, 0, 0))
player.move()
player.draw()
pygame.display.update()
fpsclock.tick(60)
class player:
def __init__(self, vel):
self.img = pygame.image.load('player.png')
#self.walk = [pygame.image.load('player_1.png'), pygame.image.load('player_2.png')]
self.width, self.height = self.img.get_size()
self.x = (screenWidth - self.width) * 0.5
self.y = screenHeight - self.height
self.vel = vel
self.xchange = 0
def move(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
self.xchange += self.vel
if event.key == pygame.K_a:
self.xchange -= self.vel
if event.type == pygame.KEYUP:
if event.key == pygame.K_d:
self.xchange -= self.vel
if event.key == pygame.K_a:
self.xchange += self.vel
self.x += self.xchange
if self.x < 0:
self.x = 0
elif self.x > (screenWidth - self.width):
self.x = screenWidth - self.width
def draw(self):
screen.blit(self.img, (self.x, self.y))
player = player(5)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen_refresh()
Your code doesn't work, because you've multiple calls to pygame.event.get(). pygame.event.get() gets all event messages and remove them from the queue. Hence just one of the loops will receive the events, the other loop gets nothing. This causes that the events seems to be missed.
Get the list of events once in the main application loop and pass the list to the functions and methods where they are handled:
def screen_refresh(events): # events: list of events
screen.fill((0, 0, 0))
player.move(events) # pass events to player.move
player.draw()
pygame.display.update()
fpsclock.tick(60)
class player:
# [...]
def move(self, events): # events: list of events
for event in events:
# [...]
running = True
while running:
events = pygame.event.get() # get list of events
for event in events:
if event.type == pygame.QUIT:
running = False
screen_refresh(events) # pass events to screen_refresh
self.vel=vel
Where is other variable vel
And I would also like to suggest to draw the player first and then move it.
I'm trying to make it so when the game over screen shows the user can press space to get back into the game. Currently, when a game over happens, it displays the game over screen but accepts no input or at least doesn't do anything with the input. For some context, the game is basically about moving left and right to avoid obstacles. Currently, I only have one obstacle, but I just have not gotten to that yet. Thanks!
import pygame
import random
import math
pygame.init()
screenWidth = 700
screenHeight = 800
x = screenWidth / 2
y = (screenHeight / 4) * 3
width = 50
height = 50
win = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Test Game")
bg = pygame.image.load("background.png").convert()
gameover = pygame.image.load("gameover.png").convert()
bgx = (screenWidth / 6) * 2
bgy = 0
clock = pygame.time.Clock()
class enemy():
def __init__(self,c,y,width,height):
self.c = c
self.y = y
self.width = width
self.height = height
self.vel = 5
def draw(self, win):
if self.c == 1:
self.x = 250
#250
elif self.c == 2:
self.x = 350
#350
else:
self.x = 450
#450
self.y += self.vel
pygame.draw.rect(win, (0,0,255), (self.x,self.y,self.width,self.height))
evil = enemy(random.randint(1,3),0,50,50)
#def redrawGameWindow():
# evil.draw(win)
# pygame.display.update()
running = True
gameOver = False
while running:
clock.tick(60)
while gameOver:
win.blit(gameover, (0,0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if pygame.event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
gameOver = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
x+=100
if event.key == pygame.K_LEFT:
x-=100
win.fill((0,0,0))
win.blit(bg, (bgx, bgy))
evil.draw(win)
dist = math.hypot(evil.x - x, evil.y - y)
if dist <= 50:
print("Game Over!")
running = False
gameOver = True
pygame.draw.rect(win, (255,0,0), (x,y,width,height))
pygame.display.update()
#redrawGameWindow()
while gameOver:
win.blit(gameover, (0,0))
pygame.display.update()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
gameOver = False
pygame.quit()
The main problem is that your game over scene is the loop behind the main while running: loop and there's no way to go back to the first loop when you reach it. When you touch the evil object, you set running = False, leave the main and enter the while gameOver: loop.
In this loop you also need to call pygame.event.pump(), otherwise the pygame.key.get_pressed() function doesn't work correctly and the window freezes after a while because the events are not handled. If you want to restart the game, you should rather use only the nested while gameOver loop. Actually, I would recommend that you restructure the scenes even more and use a finite state machine instead (here's an answer in which I use functions as scenes but check out the link in the comment below as well).
Here's a working version of your code. I've changed a few things and added comments to explain the changes.
while running:
# -----The game over scene.-----
while gameOver:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# pygame.quit only uninitializes the pygame modules and
# doesn't quit the program.
pygame.quit()
# This will quit the whole program. You need to import sys.
sys.exit()
elif event.type == pygame.KEYUP: # event.type not pygame.event.type
if event.key == pygame.K_SPACE:
# Change it to False to break out of the loop.
gameOver = False
# Reset the game. You could reset the position of the
# `evil` object or instantiate a new one.
evil.x = 350
evil.y = 0
win.blit(gameover, (0,0))
pygame.display.update()
clock.tick(60) # You need to call tick in this loop as well.
# ------------------------------
# -----The main scene.-----
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
x += 100
elif event.key == pygame.K_LEFT:
x -= 100
win.fill((0,0,0))
win.blit(bg, (bgx, bgy))
evil.draw(win)
dist = math.hypot(evil.x - x, evil.y - y)
if dist <= 50:
print("Game Over!")
# running = False # This would stop the main loop.
gameOver = True
pygame.draw.rect(win, (255,0,0), (x,y,width,height))
pygame.display.update()
clock.tick(60)
# The other while loop was removed.
pygame.quit()
I am trying to create a game where "player_one.png" is controlled by the arrow keys to move around and dodge a bunch of randomly placed "player_two.png" that can move around on their own in random directions on a football field.
I have created a program that displays the field and the two images. I am having difficulty getting "player_two.png" display multiple copies on the field and cannot get them to move.
I also cannot get the "player_one.png" image to move based on the arrow key. I found a program using Sprites that moves a red block in the same way I want my player to move but cannot switch out the sprite with my image.
Here is my initial set-up which displays the field and the two images facing each other.
import pygame
import random
pygame.init()
#colors
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 100, 0)
red = (255, 0, 0)
size = [1000, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Tony's newest game attempt")
#boolean for while loop
done = False
#create clock
clock = pygame.time.Clock()
#declare font
font = pygame.font.Font(None, 25)
player_one = pygame.image.load("player_one.png").convert()
player_one.set_colorkey(white)
player_two = pygame.image.load("player_two.png").convert()
player_two.set_colorkey(white)
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(green)
for x in range(60,940,35):
pygame.draw.line(screen, white, [x, 0], [x, 500], 1)
score = 0
text = font.render("Score:" +str(score), True, black)
screen.blit(text, [0, 0])
screen.blit(player_one, [940, 240])
screen.blit(player_two, [60, 240])
pygame.display.flip()
clock.tick(20)
pygame.quit()
The sprite program that uses a class (something I could not get to work with images) moves the Player sprite using this code: (Please note this is not the entire program).
class Player(pygame.sprite.Sprite):
# Constructor function
def __init__(self, color, x, y):
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Set height, width
self.image = pygame.Surface([35, 35])
self.image.fill(color)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.x = 930
self.rect.y = 250
def reset_player(self):
self.rect.x = 930
self.rect.y = 250
# Find a new position for the player
def update(self):
self.rect.x += self.change_y
self.rect.y += self.change_x
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.rect.x -= player.rect.width
elif event.key == pygame.K_RIGHT:
player.rect.x += player.rect.width
elif event.key == pygame.K_UP:
player.rect.y -= player.rect.height
elif event.key == pygame.K_DOWN:
player.rect.y += player.rect.height
# -- Draw everything
# Clear screen
screen.fill(green)
for x in range(60,940,35):
pygame.draw.line(screen, white, [x, 0], [x, 500], 1)
# Draw sprites
all_sprites_list.draw(screen)
text = font.render("Score: "+str(score), True, black)
screen.blit(text, [10, 10])
# Flip screen
pygame.display.flip()
# Pause
clock.tick(20)
pygame.quit()
Can someone please help me figure out how to get my image to move the way the sprite does. Or help me display "player_two.png" all over the field and move in random directions. It would be best if the "player_two.png" moved every once every 3 times I hit an arrow key. If this is vague please email me and I can send my code. I need to finish this game in 2 weeks!
Multiple comments on the game -
I don't see where self.change_y is defined. Also, shouldn't the update function be like - self.rect.x += self.change_x instead of self.rect.x += self.change_y?
Also, the main loop should be something like this
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if player.rect.x > 0:
player.rect.x -= player.rect.width
elif event.key == pygame.K_RIGHT:
if player.rect.x < screen_width: # the screen/game width
player.rect.x += player.rect.width
# Something similar for the up & down keys
I would recommend you to look at this question.
I think you have the right idea but aren't executing it well.
Try going to your game loop and when displaying an image, display like so:
gameDisplay.blit(player1, (player1X,player1Y))
This way, you can use a if statement to change playerX and player1Y. Insert the following code under the code that quits pygame:
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_LEFT:
player1X -= 5
pygame.display.update()
elif e.key == pygame.K_RIGHT:
player1X += 5
pygame.display.update()
The reason this works is because the variable that is defining X and Y are not staic. You can also do the same thing for up and down using:
elif e.key == pygame.K_UP:
or
elif e.key == pygame.K_DOWN:
I'm faced with a logic error where the sprite simply doesn't move regardless of input.
My code will be below so no context is left out, it's fairly short.
Is there a better way of moving the sprite apart from the blit?
I've seen somewhere stuff about updating the sprite or some such, done quite differently to simply blitting it.
import pygame
pygame.init()
import random
import math
screen=pygame.display.set_mode([700,400])
black = ( 0, 0, 0)
white = ( 255, 255, 255)
red = ( 255, 0, 0)
player_x=350
player_y=200
player_x_vel=0
player_y_vel=0
rot_player=pygame.image
pi=math.pi
class Player(pygame.sprite.Sprite):
def __init__(self):
global player
pygame.sprite.Sprite.__init__(self)
self.pos=(350,200)
self.image=pygame.image.load("arrowtest.png").convert()
self.rect=self.image.get_rect()
screen=pygame.display.get_surface()
self.area=screen.get_rect()
self.speed=10
self.state="still"
self.reinit()
def reinit(self):
self.state="still"
self.movepos=[0,0]
def update(self):
newpos=self.rect.move(self.movepos)
if self.area.contains(newpos):
self.rect=newpos
pygame.event.pump()
def moveup(self):
self.movepos[1]-=(self.speed)
self.state="moveup"
def movedown(self):
self.movepos[1]+=(self.speed)
self.state="movedown"
def moveleft(self):
self.movepos[0]-=(self.speed)
self.state="moveleft"
def moveright(self):
self.movepos[0]+=(self.speed)
self.state="moveright"
def moveupright(self):
self.movepos[1]-=(self.speed)
self.movepos[0]+=(self.speed)
def moveupleft(self):
self.movepos[1]-=(self.speed)
self.movepos[0]-=(self.speed)
def movedownright(self):
self.movepos[1]+=(self.speed)
self.movepos[0]+=(self.speed)
def movedownleft(self):
self.movepos[1]+=(self.speed)
self.movepos[0]-=(self.speed)
def angleplayer(self):
mouse_pos=pygame.mouse.get_pos()
dx=mouse_pos[0]-player_x
dy=mouse_pos[1]-player_y
rads=math.atan2(-dy, dx)
rads %= 2*pi
angle = math.degrees(rads)
print angle
rot_player.image=pygame.transform.rotate(player.image, angle-90)
done=False
clock=pygame.time.Clock()
while done==False:
player = Player()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
if event.type == pygame.MOUSEBUTTONDOWN:
fired_pos=pygame.mouse.get_pos()
fired=True
if event.type == pygame.KEYDOWN:
player.angleplayer()
if event.key == pygame.K_w:
player.moveup()
if event.key == pygame.K_s:
player.movedown()
if event.key == pygame.K_a:
player.moveleft()
if event.key == pygame.K_d:
player.moveright()
print "co ords", player_x,player_y
print "x vel", player_x_vel
print "y vel", player_y_vel
if event.type == pygame.KEYUP:
player.movepos=[0,0]
player.state="still"
player.angleplayer()
screen.fill(white)
screen.blit(player.image, player.pos)
clock.tick(20)
pygame.display.flip()
pygame.quit()
Thanks in advance
First of all, you are creating a new player every iteration of your main loop:
...
while done == False:
player = Player()
...
You want to create the player once, so move the creation outside the loop:
...
player = Player()
while not done:
...
Second: To get the position of the player, you use player.pos:
...
screen.blit(player.image, player.pos)
...
but you never update player.pos (it's always (350,200)), you only change self.rect:
def update(self):
newpos=self.rect.move(self.movepos)
if self.area.contains(newpos):
self.rect=newpos
pygame.event.pump()
either use player.rect to get the player position, or update player.pos accordingly:
def update(self):
newpos=self.rect.move(self.movepos)
if self.area.contains(newpos):
self.rect = newpos
self.pos = self.rect.topleft
# don't call pygame.event.pump() here. Doesn't make any sense :-)
Third, you update the player position in the update function, but you never call it. Call it before drawing the player:
...
while not done:
...
player.update()
screen.fill(white)
screen.blit(player.image, player.pos)
...
You can simplify your code alot by setting movepos directly and removing the move... functions.
working example:
import pygame
pygame.init()
colors = pygame.color.THECOLORS
screen = pygame.display.set_mode([700,400])
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.surface.Surface((32,32))
self.image.fill(colors['green'])
self.rect = self.image.get_rect().move(350, 200)
self.speed = 4
def update(self):
dx, dy = self.movepos[0] * self.speed, self.movepos[1] * self.speed
self.rect = self.rect.move(dx, dy)
done=False
clock = pygame.time.Clock()
player = Player()
while not done:
if pygame.event.get(pygame.QUIT):
break
pressed = pygame.key.get_pressed()
l, r, u, d = [pressed[k] for k in pygame.K_a, pygame.K_d, pygame.K_w, pygame.K_s]
player.movepos = -l + r, -u + d
player.update()
screen.fill(colors['white'])
screen.blit(player.image, player.rect.topleft)
pygame.display.flip()
clock.tick(60)
You have a lot of move functions. Try combining them into a simple two function class like this example:
class Puck(pygame.sprite.Sprite):
def __init__(self, image_file, speed, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
The two main controllers are the self.rect.left and self.rect.top. By changing their values, you can move the sprites like in this example.
#Name of varible# = Puck('#Image File#', [#value of self.rect.left#, value of self.rect.top]
Then you can use events to change them like:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == K_UP:
random.rect.top =random.rect.top - 75
elif event.key == K_LEFT:
random.rect.left = random.rect.left - 75
elif event.key == K_DOWN:
random.rect.top = random.rect.top + 75
elif event.key == K_RIGHT:
randoml.rect.left = randoml.rect.left + 75
elif event.key == K_w:
random.rect.top = random.rect.top - 50
elif event.key == K_a:
random.rect.left = random.rect.left - 50
elif event.key == K_s:
random.rect.top = random.rect.top + 50
elif event.key == K_d:
random.rect.left = random.rect.left + 50
This should be able to move your sprite.