So I have a file in the same folder as the file i'm coding right now and when the code runs in pycharm, if I press left arrow the other file is opened, but if I open the file using python straight from the file location it just opens and closes. Here is my code:
import pygame
import sys
import random
import subprocess
pygame.init()
GUI = pygame.display.set_mode((800,600))
pygame.display.set_caption("The incredible guessing game")
x = 284
y = 250
width = 68
length = 250
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
if event.type == pygame.KEYDOWN:
command = "python AhjaiyGame.py"
subprocess.call(command)
pygame.draw.rect(GUI, (255,210,0), (x,y,length,width))
pygame.display.update()
pygame.quit()
Simply put, the program exists because it has completed running.
Python is tabulation based, and in the code you posted, the while loop is not actually doing anything.
You need to indent the code you need looped:
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
if event.type == pygame.KEYDOWN:
command = "python AhjaiyGame.py"
subprocess.call(command)
pygame.draw.rect(GUI, (255,210,0), (x,y,length,width))
pygame.display.update()
pygame.quit()
Notice the tabulation.
In this example, pygame.quit() will only be called when run becomes False and the while loop completes.
import pygame
import sys
import random
import subprocess
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("The incredible guessing game")
x = 40
y = 30
width = 10
height = 20
vel=0.1
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
keys=pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x-=vel
if keys[pygame.K_RIGHT]:
x+=vel
if keys[pygame.K_UP]:
y-=vel
if keys[pygame.K_DOWN]:
y+=vel
win.fill((0,0,0))
pygame.draw.rect(win, (255,0,0), (x,y,width,height))
pygame.display.update()
pygame.quit()
Related
I'm making a game for school project, and there's scrolling stuffs in my game. I need to reinit pygame to make sure every is reset. But somehow, scrolling stops working after reinit pygame.
I made a simple script to test if that really was the case, and it was.
import pygame
def main():
while True:
pygame.init()
screen = pygame.display.set_mode([1280, 720])
pygame.display.set_caption("PYGAME DOES NOT RECEIVE SCROLL EVENT AFTER RE-INIT?")
frame = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
print("left click")
elif event.button == 4:
print("scroll up")
elif event.button == 5:
print("scroll down")
if event.type == pygame.QUIT:
running = False
frame.tick(30)
pygame.quit()
if __name__ == "__main__":
main()
It gets left click event but not the scrolling ones.
Is there any way to fix this?
don't use pygame.MOUSEBUTTONDOWN for scroll.
I have found another way for you to map scrollling using pygame.MOUSEWHEEL.
Here is the revised code:
import pygame
def main():
while True:
pygame.init()
screen = pygame.display.set_mode([1280, 720])
pygame.display.set_caption("PYGAME DOES NOT RECEIVE SCROLL EVENT AFTER RE-INIT?")
frame = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
print("left click")
if event.button == 3:
print("right click")
if event.type == pygame.MOUSEWHEEL:
if event.y == 1:
print("scroll up")
if event.y == -1:
print("scroll down")
if event.type == pygame.QUIT:
running = False
frame.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
btw, happy new year and good luck mate~
I am working on a pygame space invaders game. When I try to blit the background image it doesn't work. I am fairly new to pygame so I don't know what to do. I tried downloading another picture and using that but the problem persists.
import pygame
import os
import time
import random
import math
import sys
pygame.init()
screen = pygame.display.set_mode((750,750))
pygame.display.set_caption("Space Invaders")
FPS = 60
clock = pygame.time.Clock()
#Ships
RED_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_red_small.png'))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_green_small.png'))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_blue_small.png'))
YELLOW_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_yellow.png'))
#Lasers
RED_LASER = pygame.image.load(os.path.join('assets','pixel_laser_red.png'))
BLUE_LASER = pygame.image.load(os.path.join('assets','pixel_laser_blue.png'))
YELLOW_LASER = pygame.image.load(os.path.join('assets','pixel_laser_yellow.png'))
GREEN_LASER = pygame.image.load(os.path.join('assets','pixel_laser_green.png'))
#Background
BG = pygame.transform.scale2x(pygame.image.load(os.path.join('assets','background-black.png')).convert_alpha())
def main():
run = True
def redraw_window():
screen.blit(BG, (0, 0))
pygame.display.update()
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(BG, (0,0))
pygame.display.update
clock.tick(120)
pygame.quit()
You need parenthesis when calling display update:
screen.blit(BG, (0,0))
pygame.display.update() # need parenthesis
clock.tick(120)
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_LEFT):
print('LEFT')
if (event.key == pygame.K_DOWN):
print('DOWN')
if (event.key == pygame.K_RIGHT):
print('RIGHT')
if (event.key == pygame.K_UP):
print('UP')
print('FRAME')
pygame.display.update()
clock.tick(1)
If you press arrow key just after 'FRAME' is printed, sometimes 'FRAME' is printed for second time before keypressed arrow is printed. Is as if pygame.event.get() is delayed. Example:
FRAME
FRAME
<---- UP pressed here
FRAME
UP
FRAME
FRAME
<---- UP pressed here
FRAME
UP
How could I correct this behaviour?
You are using clock.tick(1), so this is telling pygame that you want 1 fps. This will make everything slower, and pretty much pause your code for a tiny bit. Try changing the 1 to something like 60 or 30.
I'm trying to display an image using pygame, but I get this error:
Traceback (most recent call last):
File "H:/profile/desktop/untitled/venv/Scripts/AhjaiyGame.py", line 28, in
start = pygame.image.load(os.path.join(folder, "wecvguh.png"))
pygame.error: Couldn't open H:\profile\desktop\untitled\venv\Scripts\wecvguh.png
Code block:
import sys
import random
import os
import subprocess
import pygame
pygame.init()
GUI = pygame.display.set_mode((800,600))
pygame.display.set_caption("The incredible guessing game")
x = 284
y = 250
width = 68
length = 250
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
if event.type == pygame.KEYDOWN:
command = "python AhjaiyCPT.py"
subprocess.call(command)
pygame.display.update()
folder = os.path.dirname(os.path.realpath(__file__))
start = pygame.image.load(os.path.join(folder, "wecvguh.png"))
def img(x,y):
gameDisplay.blit(start, (x,y))
while run:
gameDisplay.fill(white)
start(x, y)
pygame.quit()
The code has two "run" loops, so it never gets to the second loop.
The code's indentation is confused - maybe from a paste into SO?. The overwhelming majority of programmers use 4 spaces to indent. This is probably a good custom to follow.
The code also loaded the "start" image every loop iteration, this is unnecessary (unless it changes on disk, in this case use os.stat() to check for changes before re-loading it).
Re-worked main loop:
folder = os.path.dirname(os.path.realpath(__file__))
start = pygame.image.load(os.path.join(folder, "wecvguh.png"))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
command = "python AhjaiyCPT.py"
subprocess.call(command)
gameDisplay.fill(white)
gameDisplay.blit(start, (x,y))
pygame.display.update()
pygame.quit()
For some reason, when I use the code below:
import pygame, sys
pygame.init()
def create_window():
global window, window_height, window_width, window_title
window_width, window_height = 1280, 720
window_title = "The Adventure of Nate"
pygame.display.set.caption(window_title)
window = pygame.display.set_mode((window_width, window_height, pygame.HWSURFACE|pygame.DOUBLEBUFF)
create_window()
isrunning = True
while isrunning == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
isRunning = False
window.fill(0, 0, 0)
pygame.display.update()
pygame.quit()
sys.exit()
I get the following error:
C:\Python3.6\python.exe "C:/Users/home/PycharmProjects/Basic RPG/Base
Game.py" File "C:/Users/home/PycharmProjects/Basic RPG/Base
Game.py", line 16
create_window()
^ SyntaxError: invalid syntax
Process finished with exit code 1
If anyone is experienced in the matter, can they help me correct my code.
(P.S: This is my first time coding without assistance, so sorry if my code is all over the place XD)
You're missing a ) in the line window = pygame.display.set_mode((window_width, window_height, pygame.HWSURFACE|pygame.DOUBLEBUFF)
It should be window = pygame.display.set_mode((window_width, window_height), pygame.HWSURFACE|pygame.DOUBLEBUFF)