Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
import pygame
from settings import *
from player import Player
import math
from map import *
pygame.init()
sc = pygame.display.set_mode((WIDTH , HEIGHT))
clock = pygame.time.Clock()
player = Player()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
player.movement()
sc.fill(BLACK)
pygame.draw.circle(sc , GREEN , (int(player.x) ,(int(player.y)), 12 )
pygame.draw.line(sc, GREEN, player.pos, (player.x + WIDTH * math.cos(player.angle),
player.y + WIDTH * math.sin(player.angle)))
for x,y in world_map:
pygame.draw.rect(sc , DARKGRAY , (x , y , TILE , TILE),2)
pygame.display.flip()
clock.tick(FPS)
File "C:/Users/ASUS/AppData/Roaming/JetBrains/PyCharmCE2020.2/scratches/game3d.py", line 20
pygame.draw.line(sc, GREEN, player.pos, (player.x + WIDTH * math.cos(player.angle),
^
SyntaxError: invalid syntax
You have an extra parentheses on line 19
pygame.draw.circle(sc , GREEN , (int(player.x) ,(int(player.y)), 12 )
should be
pygame.draw.circle(sc , GREEN , (int(player.x) , int(player.y)), 12 )
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I am trying to make a game that has a cookie in the middle and if you click that cookie, your points increase
My problem is that once i display the points and increase it, the numbers overlap
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((1000,500))
pygame.display.set_caption("Cookie Clicker")
clock = pygame.time.Clock()
screen.fill("White")
font1 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft. ttf", 25)
font2 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 30)
font3 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 35)
font4 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft. ttf", 40)
font5 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 45)
font6 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 50)
noc = 0
# Texts
nuofco = font4.render(str(noc), False, "Black")
nuofco_r = nuofco.get_rect(center = (237,80))
# Drawn Rects
cookie = pygame.draw.ellipse(screen, "Brown", (110,150,250,250))
pygame.draw.rect(screen, "Black", pygame.Rect(475,0,25,500))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if cookie.collidepoint(event.pos):
noc += 1
screen.blit(nuofco,nuofco_r)
pygame.display.update()
clock.tick(60)
After calculating the new number I think you need to redraw everything on the screen including the background. Because everything you draw on the screen is actually drawn on top of the previous one. And everything overlaps when the background is not drawn.
And also in the main loop, the usually recommended order is to take input, update variables, draw everything. It would be better for you if you first calculate everything that needs to be calculated and then draw what needs to be drawn.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
We're doing a "virus" now and I was told to make a delay in my code. Help me, please. Here's the code:
import pygame
from tkinter import *
from tkinter import messagebox
import time
import random
pygame.init()
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption("ЗАРАЖЕНИЕ АЧЕ)")
Tk().wm_withdraw()
font = pygame.font.SysFont("Lucida Console", 35)
label2 = font.render("DELETING ALL PASSWORDS. . . . .", 1, (12, 140, 0, 1))
a = 0
while True:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
time.sleep(0.5)
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption("ЗАРАЖЕНИЕ АЧЕ)")
messagebox.showerror("ERROR!", " Вы попытались закрыть окно! Начинаю удаление файлов")
warning = f"Процент удаления: {a}"
label3 = font.render(warning, 1, (12, 140, 0, 1))
a = a + 1
time.sleep(0.1)
if a >= 100:
break
screen.blit(label2, (50, 50))
screen.blit(label3, (50, 100))
pygame.display.update()
My question is, I don't know which command to use to delay and where to put it.
Thank you in advance!
With pygame, you don't use time.sleep, you need to replace your time.sleep code with pygame.time.wait, which is written in milliseconds, so your updated code would look like so:
import pygame
from tkinter import *
from tkinter import messagebox
import time
import random
pygame.init()
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption("ЗАРАЖЕНИЕ АЧЕ)")
Tk().wm_withdraw()
font = pygame.font.SysFont("Lucida Console", 35)
label2 = font.render("DELETING ALL PASSWORDS. . . . .", 1, (12, 140, 0, 1))
a = 0
while True:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.time.wait(500)
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption("ЗАРАЖЕНИЕ АЧЕ)")
messagebox.showerror("ERROR!", " Вы попытались закрыть окно! Начинаю удаление файлов")
warning = f"Процент удаления: {a}"
label3 = font.render(warning, 1, (12, 140, 0, 1))
a = a + 1
pygame.time.wait(100)
if a >= 100:
break
screen.blit(label2, (50, 50))
screen.blit(label3, (50, 100))
pygame.display.update()
Also with your code I replaced pygame.quit() with pygame.display.quit() so the code will continue after the so called "error".
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to make a game using python and pygame but for some reason or another pycharm isn't able to open my png file of a race car and I get this error-
Traceback (most recent call last):
File "C:/Users/Zack's PC/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/main1.py", line 14, in <module>
carImg = pygame.image.load(':/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
pygame.error: Couldn't open :/Users/Zack's PC/Pictures/gameimages/racecarimage.png
Here is the code
import pygame
pygame.init()
display_width= 900
display_height=600
black= (0,0,0)
white= (255,255,255)
red= (255,0,0)
screen= pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Race Car')
clock = pygame.time.Clock()
carImg = pygame.image.load(':/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
def car(x,y):
gameDisplay.blit(carImg,(x,y))
x = (display_height * 0.45)
y = (display_width * 0.8)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
car(x,y)
gameDisplay.fill(white)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
You forgot the drive letter, change:
Img = pygame.image.load(':/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
to:
Img = pygame.image.load('C:/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
No idea why the syntax is invalid, anyone have an idea? It says the two bottom lines of code in particular have invalid syntax. This is probably embarassingly simple to fix, so please have mercy on my soul.
I haven't tried anything because it's an extremely basic problem and I'm probably just bad at coding
import pygame
import sys
import time
pygame.init()
(width, height) = (1920, 1080)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('fat')
background_colour = pygame.Color('white')
color = pygame.Color('dodgerblue2')
font = pygame.font.Font(None, 100)
clock = pygame.time.Clock()
running = True
text = ''
while running:
# handle events and user-input
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if (event.key >= pygame.K_SPACE and event.key <= pygame.K_z):
# Append key-stroke's character
text += event.unicode
elif (event.key == pygame.K_BACKSPACE):
text = text[:-1]
elif (event.key == pygame.K_RETURN):
#print("interpret(text) - NOT IMPLEMENTED")
text = ""
if len(text) > 20:
text = text[:-1]
# repaint the screen
screen.fill(background_colour)
txt_surface = font.render(text, True, color)
screen.blit(txt_surface, (50, 100))
response = font.render(Room.defaultprompt, True, color)
screen.blit((response,(80, 150))
clock.tick_busy_loop(60) # limit FPS
display.flip()
The code did, but no longer, takes what the user types and presents it onto the screen. It stopped working after I tried to make pygame draw another line of text. (btw I know that Room.defaultprompt is undefined but that's because the rest of the code just isn't in the post)
Looking closely to it, so an error on this line:
screen.blit((response,(80, 150))
When it should be:
screen.blit(response,(80, 150))
#ErikXIII's answer is little different
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
My code looks like this, and it opens up the display alright and makes the background white, but it refuses to show my text. It dosnt give any errors either.
import pygame
import time
import random
pygame.init()
black = (0, 0, 0)
gameDisplay = pygame.display.set_mode((1250,700))
white = (255, 255, 255)
def text_objects(text, font, color):
textSurface = font.render(text, True, color,)
return textSurface, textSurface.get_rect()
def letter_input():
gameDisplay.fill(white)
smallText = pygame.font.Font("txt.ttf", 20)
textSurf, textRect = text_objects ("Please input something to say", smallText, black)
textRect.center = (11250/2), (700/2)
gameDisplay.blit(textSurf, textRect)
pygame.display.update()
letter_input()
pygame.display.set_caption("Sentence printer")
clock = pygame.time.Clock()
Quite frankly, I'm just confused at why it won't do this.
textRect.center = (11250/2), (700/2)
Here you set the topleft coordinates of the rect to (5625.0, 350.0) and that's outside of your gameDisplay. Try for example textRect.center = (500, 300) instead.