How to make a character move while key is held down - python

I'm trying to create a "player" - black square - which moves when you hold down WASD. I tried looking around here, on google and on youtube on how to make this work, but every solution I've tried has the same problem: instead of moving it while I hold down the key, I have to tap the key constantly to make it move in small bits. I've no clue what I'm doing wrong. Here's the code (using python 3.3 - pygame 1.9):
import pygame
from pygame.locals import *
from pygame.time import *
import sys
pygame.init()
velX = 0
velY = 0
running = True
clock = pygame.time.Clock()
def draw():
global velX
global velY
playerx = 20
playery = 20
screen = pygame.display.set_mode((700,300))
pygame.display.set_caption('something')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,255))
screen.blit(background, (0,0))
playerx = playerx + velX
playery = playery + velY
player_filename = 'player.png'
player = pygame.image.load(player_filename)
screen.blit(player, (playerx,playery))
pygame.display.flip()
def main():
global velX
global velY
global running
while running:
keys_down = pygame.key.get_pressed()
pygame.key.set_repeat(1, 50)
time = 50/1000
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running=False
if keys_down[K_d]:
velX += 50*time
if keys_down[K_w]:
velY -= 50*time
if keys_down[K_s]:
velY += 50*time
if keys_down[K_a]:
velX -= 50*time
clock.tick(50)
draw()
if __name__ == '__main__':
main()
I already tried the set repeat command, but it didn't seem to do much anything. I also tried directly copying from a few solutions I found here on stackoverflow, but none of them worked. I suppose there is something else wrong in the code.

There are two problems with your code. First to your question. The reason the player never moves more than one step is that you reset the player position in every call to draw() when you do
playerx = 20
playery = 20
Instead, you should put that code above the draw()function and add
global playerx
global playery
at the top of draw(). Now, the player position is not reset every frame.
The second problem is that you create a new screen in every call to draw() when you do
screen = pygame.display.set_mode((700,300))
pygame.display.set_caption('something')
instead you should move those to lines above draw() and just use the same screen for every draw.
Also, like elyase points out, what you probably want is to set the velocities to fixed values, not increase them. Like so
velX = 0
velY = 0
if keys_down[K_d]:
velX = 10
if keys_down[K_w]:
velY = -10
if keys_down[K_s]:
velY = 10
if keys_down[K_a]:
velX = -10
This way the player will move around with a constant speed in the direction you steer it.
Hope that clear some stuff up. :)

You are resetting the position inside draw(). Also you should put the code that changes the direction, inside the event.type == pygame.KEYDOWN condition. Something like this:
if event.type == KEYDOWN:
if event.key == K_d:
velX += 50*time
elif event.key == K_w:
velY -= 50*time
...
if event.type == KEYUP:
# set velocity to 0

Related

How do I make my character move more smoothly/make the background synced to my character? [duplicate]

I am making a game in pygame 1.9.2.
It's a faily simple game in which a ship moves between five columns of bad guys who attack by moving slowly downward. I am attempting to make it so that the ship moves left and right with the left and right arrow keys. Here is my code:
keys=pygame.key.get_pressed()
if keys[K_LEFT]:
location-=1
if location==-1:
location=0
if keys[K_RIGHT]:
location+=1
if location==5:
location=4
It works too well. The ship moves too fast. It is near impossible to have it move only one location, left or right. How can i make it so the ship only moves once every time the key is pressed?
You can get the events from pygame and then watch out for the KEYDOWN event, instead of looking at the keys returned by get_pressed()(which gives you keys that are currently pressed down, whereas the KEYDOWN event shows you which keys were pressed down on that frame).
What's happening with your code right now is that if your game is rendering at 30fps, and you hold down the left arrow key for half a second, you're updating the location 15 times.
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
location -= 1
if event.key == pygame.K_RIGHT:
location += 1
To support continuous movement while a key is being held down, you would have to establish some sort of limitation, either based on a forced maximum frame rate of the game loop or by a counter which only allows you to move every so many ticks of the loop.
move_ticker = 0
keys=pygame.key.get_pressed()
if keys[K_LEFT]:
if move_ticker == 0:
move_ticker = 10
location -= 1
if location == -1:
location = 0
if keys[K_RIGHT]:
if move_ticker == 0:
move_ticker = 10
location+=1
if location == 5:
location = 4
Then somewhere during the game loop you would do something like this:
if move_ticker > 0:
move_ticker -= 1
This would only let you move once every 10 frames (so if you move, the ticker gets set to 10, and after 10 frames it will allow you to move again)
pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is 1, otherwise 0. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement:
while True:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= speed
if keys[pygame.K_RIGHT]:
x += speed
if keys[pygame.K_UP]:
y -= speed
if keys[pygame.K_DOWN]:
y += speed
This code can be simplified by subtracting "left" from "right" and "up" from "down":
while True:
keys = pygame.key.get_pressed()
x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * speed
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or movement:
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x -= speed
if event.key == pygame.K_RIGHT:
x += speed
if event.key == pygame.K_UP:
y -= speed
if event.key == pygame.K_DOWN:
y += speed
See also Key and Keyboard event
Minimal example of continuous movement: replit.com/#Rabbid76/PyGame-ContinuousMovement
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
rect = pygame.Rect(0, 0, 20, 20)
rect.center = window.get_rect().center
vel = 5
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))
keys = pygame.key.get_pressed()
rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel
rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel
rect.centerx = rect.centerx % window.get_width()
rect.centery = rect.centery % window.get_height()
window.fill(0)
pygame.draw.rect(window, (255, 0, 0), rect)
pygame.display.flip()
pygame.quit()
exit()
Minimal example for a single action: replit.com/#Rabbid76/PyGame-ShootBullet
import pygame
pygame.init()
window = pygame.display.set_mode((500, 200))
clock = pygame.time.Clock()
tank_surf = pygame.Surface((60, 40), pygame.SRCALPHA)
pygame.draw.rect(tank_surf, (0, 96, 0), (0, 00, 50, 40))
pygame.draw.rect(tank_surf, (0, 128, 0), (10, 10, 30, 20))
pygame.draw.rect(tank_surf, (32, 32, 96), (20, 16, 40, 8))
tank_rect = tank_surf.get_rect(midleft = (20, window.get_height() // 2))
bullet_surf = pygame.Surface((10, 10), pygame.SRCALPHA)
pygame.draw.circle(bullet_surf, (64, 64, 62), bullet_surf.get_rect().center, bullet_surf.get_width() // 2)
bullet_list = []
run = True
while run:
clock.tick(60)
current_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
bullet_list.insert(0, tank_rect.midright)
for i, bullet_pos in enumerate(bullet_list):
bullet_list[i] = bullet_pos[0] + 5, bullet_pos[1]
if bullet_surf.get_rect(center = bullet_pos).left > window.get_width():
del bullet_list[i:]
break
window.fill((224, 192, 160))
window.blit(tank_surf, tank_rect)
for bullet_pos in bullet_list:
window.blit(bullet_surf, bullet_surf.get_rect(center = bullet_pos))
pygame.display.flip()
pygame.quit()
exit()
import pygame
pygame.init()
pygame.display.set_mode()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); #sys.exit() if sys is imported
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_0:
print("Hey, you pressed the key, '0'!")
if event.key == pygame.K_1:
print("Doing whatever")
In note that K_0 and K_1 aren't the only keys, to see all of them, see pygame documentation, otherwise, hit tab after typing in
pygame.
(note the . after pygame) into an idle program. Note that the K must be capital. Also note that if you don't give pygame a display size (pass no args), then it will auto-use the size of the computer screen/monitor. Happy coding!
I think you can use:
pygame.time.delay(delayTime)
in which delayTime is in milliseconds.
Put it before events.
Try this:
keys=pygame.key.get_pressed()
if keys[K_LEFT]:
if count == 10:
location-=1
count=0
else:
count +=1
if location==-1:
location=0
if keys[K_RIGHT]:
if count == 10:
location+=1
count=0
else:
count +=1
if location==5:
location=4
This will mean you only move 1/10 of the time. If it still moves to fast you could try increasing the value you set "count" too.
The reason behind this is that the pygame window operates at 60 fps (frames per second) and when you press the key for just like 1 sec it updates 60 frames as per the loop of the event block.
clock = pygame.time.Clock()
flag = true
while flag :
clock.tick(60)
Note that if you have animation in your project then the number of images will define the number of values in tick(). Let's say you have a character and it requires 20 sets images for walking and jumping then you have to make tick(20) to move the character the right way.
Just fyi, if you're trying to ensure the ship doesn't go off of the screen with
location-=1
if location==-1:
location=0
you can probably better use
location -= 1
location = max(0, location)
This way if it skips -1 your program doesn't break
make something like this, but based on time delay. i call my function first time immediately and then lunch timer, and while button is pressed i call it every button_press_delta seconds
from time import time
before main loop:
button_press_delta = 0.2
right_button_pressed = 0
while not done:
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
if not right_button_pressed:
call_my_function()
right_button_pressed = 1
right_button_pressed_time_start = time()
if right_button_pressed:
right_button_pressed_time = (
time() - right_button_pressed_time_start)
if right_button_pressed_time > button_press_delta:
call_my_function()
right_button_pressed_time_start = time()
else:
right_button_pressed = 0
You should use clock.tick(10) as stated in the docs.
all of the answers above are too complexicated i would just change the variables by 0.1 instead of 1
this makes the ship 10 times slower
if that is still too fast change the variables by 0.01
this makes the ship 100 times slower
try this
keys=pygame.key.get_pressed()
if keys[K_LEFT]:
location -= 0.1 #or 0.01
if location==-1:
location=0
if keys[K_RIGHT]:
location += 0.1 #or 0.01
if location==5:
location=4
To slow down your game, use pygame.clock.tick(10)

How to move object smoothly [duplicate]

This question already has answers here:
Framerate affect the speed of the game
(1 answer)
Pygame snake velocity too high when the fps above 15 [duplicate]
(1 answer)
Closed 2 years ago.
I've been searching for a solution but still can't get it to work. I don't know what's wrong. I want to move my object smoothly just like this:
https://youtu.be/vc1pJ8XdZa0?t=153
but mine is always "teleporting" when moving. Here's part of my code
#Player
playerImg = pygame.image.load('yin.png')
playerX = 0
playerY = 0
playerX_move = playerY_move = 0
playerMoveUnit = 5
def player(x,y):
screen.blit(playerImg,(x,y))
#Game Loop
running =True
while running:
screen.blit(background,(-100,-80))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_move-=playerMoveUnit
if event.key == pygame.K_RIGHT:
playerX_move+=playerMoveUnit
if event.key == pygame.K_DOWN:
playerY_move+=playerMoveUnit
if event.key == pygame.K_UP:
playerY_move-=playerMoveUnit
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
playerX_move+=playerMoveUnit
if event.key == pygame.K_RIGHT:
playerX_move-=playerMoveUnit
if event.key == pygame.K_DOWN:
playerY_move-=playerMoveUnit
if event.key == pygame.K_UP:
playerY_move+=playerMoveUnit
playerX+=playerX_move
playerY+=playerY_move
#Check boundary
if playerX <=-10:
playerX = -10
elif playerX >=(length-70):
playerX = length-70
if playerY <= -20:
playerY = -20
elif playerY >=(width-105):
playerY = width-105
player(playerX,playerY)
pygame.display.update()
just reduce your "playerMoveUnit"
Use pygame.time.Clock() and .tick() to control the flops per second. The framerate argument the .tick() will delay to keep the game running slower than the given ticks per second. For instance:
clock = pygame.time.Clock()
FPS = 60
running =True
while running:
clock.tick(FPS)
# [...]
Furthermore I recommend to use pygame.key.get_pressed() rather than the keyboard events for the movement of the player. pygame.key.get_pressed() returns a sequence of boolean values representing the state of every key.
Get and evaluate the states of the keys in every frame and move the player accordingly:
clock = pygame.time.Clock()
FPS = 60
running =True
while running:
clock.tick(FPS)
screen.blit(background,(-100,-80))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
playerX -= playerMoveUnit
if keys[pygame.K_RIGHT]:
playerX += playerMoveUnit
if keys[pygame.K_UP]:
playerY -= playerMoveUnit
if keys[pygame.K_DOWN]:
playerY += playerMoveUnit
#Check boundary
if playerX <=-10:
playerX = -10
elif playerX >=(length-70):
playerX = length-70
if playerY <= -20:
playerY = -20
elif playerY >=(width-105):
playerY = width-105
player(playerX,playerY)
pygame.display.update()
I notice that you do not have a clock.tick(...) in your code. That is needed to limit the frame rate.
You need to put it into your main while loop.
Somewhere at the top of the code you want to set a constant that controls your framerate. You'll also need a clock to call tick() on People commonly use a frame rate of 60, so you would want something like this near the top of your code:
FPS = 60
clock = pygame.time.Clock()
Then at the end of your while loop you need a clock.tick(FPS). I tend to put it just before the screen update like this:
while running:
....
clock.tick(FPS)
pygame.display.update()
FPS stands for Frames Per Second. The tick() call remembers the time that you last called it and does not return until an amount of time equal to 1/FPS has passed since the previous call. Putting it after the computation and just before the update() in the while loop ensures the screen updates occur as close as possible to those even 1/FPS time periods. You can find the docs here.
If your game logic per pass does not take very much time, without this limit your game will run very fast (as you discovered). A different issue but just as bad is, if the amount of computing taken each frame varies, then without imposing a constant frame rate the animation would not be smooth since each frame would display for different amounts of time.
Something like this was likely in the tutorial you were working with, but you may have missed it or not realized what it was for and left it out.
Edit:
After you said you had tried this and it still was not working, I looked at it a bit more. There are two other issues I see, though one may be intentional, I am not sure.
You have playerX_move, playerY_move and playerMoveUnit which together appear to be intended to control your speed. It appears that playerMoveUnit is supposed to be the absolute amount and that playerX_move and playerY_move are intended to be that applied to the X and Y directions. That would mean that you would assign playerMoveUnit to either playerX_move or playerY_move depending on the key press, like this:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_move = playerMoveUnit
However you are using += or -= instead. This means that each pass you will be increasing your speed by playerMoveUnit. That makes playerMoveUnit an acceleration not a speed and will result in your object moving very fast very quickly. This is likely the cause of the teleporting behaviour, it is just moving VERY fast.
The other issue I want to point out is that on the KEYDOWN you start the object moving in the desired direction, and on KEYUP you don't stop it by setting the speed to 0. Instead you start the speed going the other way. That is likely not the behaviour that you want. Your code will cause it to move one way when the key is pressed and then start moving in reverse when it is released and not stop till it hits the edge or another key is pressed. You likely want something like this instead:
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
playerX_move = 0
By the way this would be cleaner if you structured the as a class so you could group all the player related information together.
I just noticed that #JohnnyMopp had a comment on your question that pointed out the += -= thing. Though I had not noticed it, he added that before I edited my answer to include that fix.
You are not using sprites, but single image. You need to load multiple images in order to get smooth movement, in this case for player, if you are following this tutorial, you need 9 images for "left" and 9 for "right" movement. Loading can be done e.g. this way:
go_right = []
go_left = []
for i in range(1,10):
sprite_r = pg.image.load("R{}.png".format(i))
sprite_l = pg.image.load("L{}.png".format(i))
go_right.append(sprite_r)
go_left.append(sprite_l)
Then, in Player class define methods for left and right:
def go_left(self):
self.left = True
self.right = False
self.x -= self.v
def go_right(self):
self.right = True
self.left = False
self.x += self.v
Now, in redraw() or whatever you called it, set FPS to 27 for example(for divisibility reason):
clock.tick(27)
if player.right:
window.blit(go_right[player.walk_count//3], (player.x, player.y))
player.walk_count += 1
elif player.left:
window.blit(go_left[player.walk_count//3], (player.x, player.y))
player.walk_count += 1
else:
player.stand()
window.blit(char, (player.x, player.y))
This if's are responsible for displaying appropriate images from the list (loaded at the beginning)
And at the end, in the main_loop you need to handle the events (left and right arrows e.g.):
if keys[pg.K_RIGHT]:
if player.x < win_width - get_player_width: # or constant number
player.go_right()
elif keys[pg.K_LEFT]:
if player.x > 0:
player.go_left()
You will probably need to adjust some of the code but I made it following the given tutorial. I hope this will work for you
EDIT:
After additional info from the comments I managed to run your code, as others already suggested putting clock.tick(FPS) in main_loop solves the issue. Do not forget clock = pygame.time.Clock() line before the loop. Additionally, if you want smoother and animated movement apply the code I provided above

Making an image move in 4 directions in pygame

Struggling to make my 'cowboy' image move across the screen in all 4 directions. Nothing happens whenever I press the keys. Help please. This is based off the exercise questions in Python Crash Course.
import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("Move the Cowboy")
cowboy = pygame.image.load('images/cowboy.bmp')
cowboy = pygame.transform.scale(cowboy, (50, 50))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((240,237,207))
cowboy_rect = cowboy.get_rect()
screen_rect = screen.get_rect()
screen_center = screen_rect.center
cowboy_rect.center = screen_rect.center
cowboy_rect.x = cowboy_rect.width
cowboy_rect.y = cowboy_rect.height
cowboy_x = float(cowboy_rect.x)
cowboy_y = float(cowboy_rect.y)
screen.blit(cowboy, screen_center)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
cowboy_x -= 5
elif keys[pygame.K_RIGHT]:
cowboy_x += 5
elif keys[pygame.K_UP]:
cowboy_y -= 5
elif keys[pygame.K_DOWN]:
cowboy_y += 5
pygame.display.update()
pygame.quit()
You are putting the image at the middle of the screen with screen.blit(cowboy, screen_center). Change screen_center to cowboy_rect as this stores the position of the image.
Also take the cowboy_rect = cowboy.get_rect() out of the loop as this resets it back to 0.
You are then putting the position of cowboy_rect to the center of the screen by doing cowboy_rect.center = screen_rect.center, take that out of the loop so it only happens once, not every frame. Then its getting changed to 50,50 by cowboy_rect.x = cowboy_rect.width and cowboy_rect.y = cowboy_rect.height, so take that out.
Then at the keys, change cowboy_x -= 5 to cowboy_rect.x -= 5 and do the same for the others.
By putting this in: screen.blit(cowboy, screen_center), you are saying that this image will appear at the center of the screen. You want it to appear at (cowboy_x, cowboy_y), as these are the positionment variables you defined. Here is a working version of your code:
import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("Move the Cowboy")
cowboy = pygame.image.load('images/cowboy.bmp')
cowboy = pygame.transform.scale(cowboy, (50, 50))
cowboy_x, cowboy_y = 0, 0
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((240,237,207))
screen.blit(cowboy, (cowboy_x, cowboy_y))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
cowboy_x -= 1
elif keys[pygame.K_RIGHT]:
cowboy_x += 1
elif keys[pygame.K_UP]:
cowboy_y -= 1
elif keys[pygame.K_DOWN]:
cowboy_y += 1
pygame.display.update()
pygame.quit()
I took the liberty of taking out some completely useless lines of code, and of bringing the stuff you didn't need to re-iterate out of the loop, to save processing space.

How do I easily change the functionality of my code?

I want to make my game "go back" to the main menu and I was wondering If I can use the gamewindow function "inside" its own so that it would create a "reset". Basically, If my sprite were to collide and it says "game over" I would like to press the "back" button and If I were to press play again it would show a "reseted" game where the "timer", the "asteroid", the "ship", goes back to the beginning as If I were playing a new game again.
I was also wondering if any of the errors given to me mean anything to my problem now??
The code/game is mostly working. It just doesn't have that "reset" purpose.
Here's the errors:
"C:\Users\Myke Sustento\AppData\Local\Programs\Python\Python38-32\python.exe" "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py"
B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py:261: SyntaxWarning: "is" with a literal. Did you mean "=="?
if bullet_state is "fire":
B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py:322: SyntaxWarning: "is" with a literal. Did you mean "=="?
if bullet_state is "ready":
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py:171: DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
display.blit(ammoboximg,(x,y))
Traceback (most recent call last):
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 374, in <module>
gamewindow()
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 315, in gamewindow
gamewindow()
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 315, in gamewindow
gamewindow()
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 315, in gamewindow
gamewindow()
File "B:/PROGRAMMING RELATED/PYTHON PROJECTS/FINAL.py", line 330, in gamewindow
keys = pygame.key.get_pressed()
pygame.error: video system not initialized
Process finished with exit code 1
Here's the whole code:
# initialization of pygame
import pygame
import random
import math
pygame.init()
# creating the display
display = pygame.display.set_mode((500, 500))
# title & icon
spaceship = pygame.image.load("Space Blitz Sprites/spaceship.png")
pygame.display.set_icon(spaceship)
pygame.display.set_caption("SpaceBlitz")
# main menu sprites
spaceblitz = pygame.image.load("Space Blitz Sprites/spaceblitz.png")
play = pygame.image.load("Space Blitz Sprites/play.png")
howtoplay = pygame.image.load("Space Blitz Sprites/howtoplay.png")
about = pygame.image.load("Space Blitz Sprites/about.png")
quit = pygame.image.load("Space Blitz Sprites/quit.png")
# inside main menu
instruction = pygame.image.load("Space Blitz Sprites/instruction.png")
back = pygame.image.load("Space Blitz Sprites/back.png")
aboutdev = pygame.image.load("Space Blitz Sprites/aboutdev.png")
# main menu music
music = pygame.mixer.music.load("Space Blitz Sprites/mountaintrails.mp3")
# PlayerSpriteMovement
playerimg = pygame.image.load("Space Blitz Sprites/spaceship.png")
playerX = 250
playerY = 400
velocity = 3
clock = pygame.time.Clock()
# Bullet
bulletimg = pygame.image.load("Space Blitz Sprites/bullet.png")
bulletX = 0
bulletY = playerY
bulletx_change = 0
bulletY_change = 8
bullet_state = "ready"
bullet_ammo = 5
bulletfont = pygame.font.Font('freesansbold.ttf', 16)
# Ammo Box
ammoboximg = pygame.image.load('Space Blitz Sprites/ammo.png')
ammoboxX = random.randint(0,468)
ammoboxY = random.randint(-1000,-800)
ammoboxY_change = -1.5
# Asteroid
asteroidimg = []
asteroidX = []
asteroidY = []
asteroidX_change = []
asteroidY_change = []
no_of_enemies = 40
def mainmenu():
global menuselect
global spaceblitz
menu = True
pygame.mixer.music.play(50)
pygame.mixer.music.set_volume(0.2)
while menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if start.collidepoint(pos):
menu = False
menuselect = 1
pygame.mixer.music.stop()
if controls.collidepoint(pos):
menu = False
menuselect = 2
pygame.mixer.music.stop()
if developer.collidepoint(pos):
menu = False
menuselect = 3
pygame.mixer.music.stop()
if exit.collidepoint(pos):
menu = False
menuselect = 4
display.fill((0, 0, 0))
display.blit(spaceblitz, (170,150))
start = display.blit(play, (170,250))
controls = display.blit(howtoplay, (170,300))
developer = display.blit(about, (170,350))
exit = display.blit(quit, (170,400))
pygame.display.flip()
pygame.display.update()
def controls():
global menuselect
global menu
controls = True
while controls:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if balik.collidepoint(pos):
controls = False
menu = True
menuselect = 0
balik = display.blit(back, (0,450))
display.blit(instruction, (0,0))
pygame.display.flip()
pygame.display.update()
display.fill((0, 0, 0))
def developers():
global menuselect
global menu
dev = True
while dev:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if balik.collidepoint(pos):
dev = False
menu = True
menuselect = 0
balik = display.blit(back, (0, 450))
display.blit(aboutdev, (0, 0))
pygame.display.flip()
pygame.display.update()
display.fill((0, 0, 0))
# Asteroid
for r in range(no_of_enemies):
asteroidimg.append(pygame.image.load("Space Blitz Sprites/asteroid.png"))
asteroidX.append(random.randint(-100, 500))
asteroidY.append(random.randint(-300, -30))
asteroidX_change.append(0)
asteroidY_change.append(2)
# Game Over Text
overfont = pygame.font.Font('freesansbold.ttf',32)
# Sprite image
def player(x, y):
display.blit(playerimg, (x, y))
def fire_bullet(x, y):
global bullet_state
if bullet_ammo > -1:
bullet_state = "fire"
display.blit(bulletimg, (x + 9, y + -7))
else:
bullet_state = "ready"
def ammobox(x,y):
display.blit(ammoboximg,(x,y))
def AmBoxCollision(ammoboxX,ammoboxY,playerX,playerY):
ammoboxdistance = math.sqrt((math.pow(ammoboxX - playerX, 2)) + (math.pow(ammoboxY - playerY, 2)))
if ammoboxdistance < 27:
return True
else:
return False
def ammo():
global bullet_ammo
global ammo_decrease
ammo_decrease = 1
bullet_ammo -= ammo_decrease
def asteroid(x, y, r):
display.blit(asteroidimg[r], (x, y))
def BulCollision(asteroidX, asteroidY, bulletX, bulletY):
buldistance = math.sqrt((math.pow(bulletX - asteroidX, 2)) + (math.pow(bulletY - asteroidY, 2)))
if buldistance < 27:
return True
else:
return False
def PlayCollision(asteroidX, asteroidY, playerX, playerY):
playdistance = math.sqrt((math.pow(playerX - asteroidX, 2)) + (math.pow(playerY - asteroidY, 2)))
if playdistance < 27:
return True
else:
return False
def gameover_screen():
overtext = overfont.render("GAME OVER",True,(255,255,255))
display.blit(overtext, (150,250))
# mainloop
def gamewindow():
global menuselect
global playerX
global playerY
global velocity
global clock
global bulletX
global bulletY
global bulletY_change
global bullet_state
global asteroidX
global asteroidY
global asteroidY_change
global no_of_enemies
global ammoboxX
global ammoboxY
global ammoboxY_change
global dev
global menu
global bullet_ammo
global ammo_decrease
global passed_time
font = pygame.font.Font(None, 54)
font_color = pygame.Color('white')
start_time = pygame.time.get_ticks()
run_timer = True
running = True
while running:
clock.tick(60)
display.fill((0, 0, 0))
AmmoBoxCollision = AmBoxCollision(ammoboxX,ammoboxY,playerX,playerY)
if AmmoBoxCollision:
ammoboxY = random.randint(-1000, -800)
ammoboxX = random.randint(0, 468)
if bullet_ammo <= 4 and bullet_ammo > -1:
bullet_ammo += 1
if bullet_ammo == -1:
bullet_ammo +=2
if bullet_ammo == -1 or bullet_ammo == 0:
bullet_text = bulletfont.render("Ammo:%d" % bullet_ammo, True, (0, 0, 0))
display.blit(bullet_text, (10, 468))
noammo = bulletfont.render("NO AMMO",True,(255,255,255))
display.blit(noammo,(10,468))
else:
bullet_text = bulletfont.render("Ammo:%d" % bullet_ammo, True, (255, 255, 255))
display.blit(bullet_text, (10, 468))
if bullet_state is "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
if bulletY <= 0:
bulletY = playerY
bullet_state = "ready"
for r in range(no_of_enemies):
asteroid(asteroidX[r], asteroidY[r], r)
asteroidY[r] += asteroidY_change[r]
if asteroidY[r] >= 500:
asteroidY[r] = random.randint(-300, -30)
asteroidX[r] = random.randint(-100, 500)
Bulletcollision = BulCollision(asteroidX[r], asteroidY[r], bulletX, bulletY)
if Bulletcollision:
bulletY = playerY
bullet_state = "ready"
asteroidX[r] = random.randint(-100, 500)
asteroidY[r] = random.randint(-300, -30)
# Game over
PlayerCollision = PlayCollision(asteroidX[r], asteroidY[r], playerX, playerY)
if PlayerCollision:
for j in range(no_of_enemies):
asteroidY_change[j] = 0
asteroidY[j] = random.randint(-300, -30)
asteroidX[j] = random.randint(-100, 500)
asteroidY_change[j] = 2
velocity = 0
bulletY_change = 0
bulletY = 600
ammoboxY_change = 0
run_timer = False
gameover_screen()
playerX = 250
playerY = 400
velocity = 3
# movement
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if balik.collidepoint(pos):
running = False
menu = True
menuselect = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if bullet_ammo <= 5 and bullet_ammo > -1:
if bullet_state is "ready":
bulletX = playerX
bulletY = playerY
fire_bullet(bulletX, bulletY)
ammo()
# player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
playerX -= velocity
if keys[pygame.K_RIGHT]:
playerX += velocity
if keys[pygame.K_DOWN]:
playerY += velocity
if keys[pygame.K_UP]:
playerY -= velocity
# Border
if playerX <= 0:
playerX = 0
elif playerX >= 468:
playerX = 468
if playerY <= 0:
playerY = 0
elif playerY >= 468:
playerY = 468
if ammoboxY > 500:
ammoboxY = random.randint(-1000,-800)
ammoboxX = random.randint(0,468)
ammobox(ammoboxX,ammoboxY)
ammoboxY -= ammoboxY_change
if run_timer:
current_time = pygame.time.get_ticks()
passed_time = current_time - start_time
text = font.render(str(passed_time / 1000), True, font_color)
display.blit(text, (50, 50))
balik = display.blit(back, (350, 450))
player(playerX, playerY)
pygame.display.update()
mainmenu()
while True:
if menuselect == 0:
mainmenu()
elif menuselect == 1:
gamewindow()
elif menuselect == 2:
controls()
elif menuselect == 3:
developers()
elif menuselect == 4:
pygame.quit()
Here's where I would like to put my reset button (in either the PlayerCollision or balik.collidepoint(pos))
if PlayerCollision:
for j in range(no_of_enemies):
asteroidY_change[j] = 0
asteroidY[j] = random.randint(-300, -30)
asteroidX[j] = random.randint(-100, 500)
asteroidY_change[j] = 2
velocity = 0
bulletY_change = 0
bulletY = 600
ammoboxY_change = 0
run_timer = False
gameover_screen()
playerX = 250
playerY = 400
velocity = 3
# movement
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
if balik.collidepoint(pos):
running = False
menu = True
menuselect = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if bullet_ammo <= 5 and bullet_ammo > -1:
if bullet_state is "ready":
bulletX = playerX
bulletY = playerY
fire_bullet(bulletX, bulletY)
ammo()
I'm sorry if I'm asking a lot of question. This is my first ever code in python (or programming in general) so I don't really know how most of the stuff works. Plus, the code is a mixture of my code and my group-mate's code so half of the code in this program is unfamiliar to me. Tips for future coding practice is highly appreciated but I just want to finish my game right now so I would like to finish the current (and last) problem that I encountered.
As you say, you've just started and are learning. Very good! You've just run into a problem that you can learn a lot from: adding new features. Generally, this question would be better suited on the code review stack exchange but I think it's also suited here.
So what's the problem exactly? You say you want to have your game "go back" to the main menu. You suggest an implementation that (I find) difficult to follow and follow up with a bunch of code that I'm sure that works but is difficult (for me) to read. I honestly find it very impressive how much you've been able to write, especially since it's your first time coding!
In my opinion, your real problem isn't how to "go back", rather, it is how to make your code fixeable/updateable. A very good lesson to learn. There's a couple reasons why your current code is difficult to update:
All your code is in one file, making it hard to navigate
You have global variables, which can be hard to track
You use many hard coded values which are hard to change/keep track of
Why is this a problem? If you want to change something you cannot find where to change what because you cannot find anything because there's so much code everywhere. Then, once you find it, you cannot really know what everything is because of the global variables. Lastly, the hard coded values make it impossible to know if you've fixed everything, or forgot a number causing a bug (for instance, you want to make the screen twice as big, change all the picture sizes, but forget to update the speed).
So how do you change this?
Well, first try splitting up your code so it's easier to find things. I like to put things in folders like this:
my_project/
main.py
.gitignore
core/
game_mechanics/
player.py
bullet.py
asteroid.py
game_setup/
main_menu.py
run_game.py
config/
config.yaml
utils/
read_config.py
tests/
test1.py
test2.py
This may seem daunting, so let's run past what everything is:
Main.py: your main file which you run. It looks something like this:
from example_file import example_function
def main(config):
while True:
settings = main_menu()
play_game(settings)
if __name__ == '__main__':
config = load_config()
main(config)
When you run main.py it will load the config, and then run the main components. This file should be very short so it's easy to understand what is happening. Here for instance, you get the settings from running main_menu() and then play the game using those settings. When the game quits, the main menu is shown again.
Next is the .gitignore file that you'll need if you use git. If you don't know what this is/does you should google it, it will make your life much easier.
In your core folder you have all the main functions specific to the game. This is where you put most of your code. It is important to split it up into logical files so you can easily find what goes where.
In your utils folder you have utility functions you'll want to share between projects.
In your config folder you can store the settings. This should contain all the "number or letter values" you use, this way you don't have to go through your code manually changing number everywhere (and maybe forgetting one), but instead pass it along.
This solves your problem of having to find where what is. Everything is ordered nicely and you can easily find it. If you want to change large functionalities (like adding a new menu) you can do this in the main file which is easy to do.
The next problem is the use of global variables. You have quite a number of them and this makes it hard to track which value is what. It may be clear to you know, but imagine when you come back in a year and values change 'randomly', how do you find where what changes?
Instead, try to pass things to your functions like this:
def square_n(n):
return n * n
a = 5
a_squared = square_n(a)
This makes it much easier to read what happens where.
Lastly, as mentioned before the hard coded values make everything very difficult to update. Take the example with doubling the screen size. You make everything twice as large, but forget to double one speed component (because you overlooked it). This could be a "weird bug" that takes some time to fix. Instead try to save all these values in your config, and pass them along as shown above. This would look something like this:
def print_hello_world_n_times(n):
for i in range(n):
print("hello world")
config = load_config()
# config = {'a': 5, 'b': 10}
a = config['a']
print_hello_world_n_times(a)
I'm sorry I won't be giving you a copy-paste solution to your problem. I think it's much more useful for you to restructure your code yourself and then see how much easier it is to change its functionality.
Success!

Character only maintains movement while mouse is moving on screen?

My while loops only maintains the movement for the sprite while the cursor is moving inside of the screen. I've tried reorganizing some of the screen.blits and display.update() and display.flip(). I can't seem to figure out why the character stops after a change in one pixel instead of continuing like it I intended.
background_image = 'Terrain_Grass_First.png'
import pygame, sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption('Hans')
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height),0,32)
pygame.mouse.set_visible(False)
sprite = pygame.image.load('Hans_front_still.png').convert_alpha()
x,y = (0,0)
movex, movey = (0,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_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_w:
y = -1
elif event.key == K_a:
x = -1
elif event.key == K_s:
y = +1
elif event.key == K_d:
x = +1
elif event.type == KEYUP:
if event.key == K_w:
y = 0
elif event.key == K_a:
x = 0
elif event.key == K_s:
y = 0
elif event.key == K_d:
x = 0
movex += x
movey += y
screen.fill((0,0,0))
screen.blit(sprite,(movex,movey))
pygame.display.flip()
Your loop blocks on pygame.event.get.
You don't schedule any events of your own.
So, you do nothing until the OS has an event (mouse move, redraw, etc.) to give you.
And, even if you fixed that, you're calling movex += x once per event. So, when the OS is throwing a lot of events at you, your sprite will go zipping madly across the screen, but when the events are coming more slowly, it will crawl along. That's almost never what you want.
An easy fix for both problems is to just schedule your own events. For example, with pygame.time.set_timer(), you can make sure you get an event every, say, 250 milliseconds, and you can only move the sprite on those events. For example:
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, 250)
while True:
for event in pygame.event.get():
# ...
elif event.type == timer_event:
movex += x
movey += y
Another alternative is to design your game with a fixed framerate.
A trivial example looks like this:
FRAME_TIME = 50 # 50ms = 20fps
next_frame_time = pygame.time.get_ticks() + FRAMES
while True:
while True:
event = pygame.event.poll()
if event.type == pygame.NOEVENT:
break
elif # ...
pygame.display.flip()
now = pygame.time.get_ticks()
if now < next_frame_time:
pygame.time.wait(next_frame_time - now)
next_frame_time += FRAMES
Now you can just move every 5th frame.
A realistic example has to deal with missed frames, too many events in the queue, choose between wait and delay appropriately, etc.
But I'd go with the event-driven version with a timer instead of a fixed-framerate version in most cases, especially if you're already going down that line.
The only problem is your Indentation!
The fifth and sixth lines from the bottom have wrong indentation, and you need to delete them.
These two lines:
movex += x
movey += y
should be:
movex += x
movey += y
And it works

Categories