Reputation: 79
I'm currently creating a game for university. Right now I need to display text and a timer on the screen. At the bottom of the code you can see that I used the .blit function to display the timer. But nothing happens. Also no error message shows up. It just does nothing. I also tried it with elements of a list. It would be amazing if someone could help me. Thank you really much in advance!
import pygame
from pygame.locals import *
import random
pygame.init()
# Color-Codes
ORANGE = (255, 140, 0)
RED = (255, 0, 0)
DARKBLUE = (0, 0, 139)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
DARKGREEN = (0, 98, 7)
LIMEGREEN = (50, 205, 50)
DARKGREY = (70, 70, 70)
BLUE = (0, 0, 255)
LIGHTBLUE = (173, 216, 230)
WOODY = (139,69,19)
LIST_OF_ALL_COLOR_NAMES = [ ORANGE, RED, DARKBLUE, WHITE, BLACK, YELLOW, GREEN, DARKGREEN, LIMEGREEN,
DARKGREY, BLUE, LIGHTBLUE ]
# Konstanten
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
PANEL_SIZE = 160
SQUARE_SIZE = 80
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
move_list = []
screen = pygame.display.set_mode((1200, 800)) # Fensterbreite und länge in pixeln, nur der Zeichenbereich
font = pygame.font.SysFont("Consolas", 30) # Importieren der Schrift
textRight = font.render("RECHTS", False, WHITE)
pygame.time.set_timer(pygame.USEREVENT, 1500)
def pxl (number_of_squares):
"number of squares -> number of pixels"
return SQUARE_SIZE * number_of_squares
SIDEPANEL = pygame.Rect(pxl(13), pxl(0), pxl(2), pxl(10))
counter, text = 15, "15".rjust(1080)
# functions
def drawGrid():
for x in range(0, SCREEN_WIDTH, SQUARE_SIZE):
for y in range(0, SCREEN_HEIGHT, SQUARE_SIZE):
rect = pygame.Rect(x, y, SQUARE_SIZE, SQUARE_SIZE)
pygame.draw.rect(screen, BLACK, rect, 1)
def drawRect():
pygame.draw.rect(screen, WHITE, SIDEPANEL,)
def write_move():
for event in pygame.event.get():
if event.type == KEYDOWN:
j = 0
i = 0
if event.key == K_UP:
screen.blit(move_list[i], pxl(13.5), pxl(0.5+j))
j += 0.5
i += 1
elif event.key == K_DOWN:
move_list.append(2)
elif event.key == K_RIGHT:
move_list.append(3)
elif event.key == K_LEFT:
move_list.append(4)
def showTimer():
screen.blit(font.render(text, True, BLACK), (1080,40))
# Classes
class Player(object):
def __init__(self):
self.rect = pygame.Rect(pxl(2.1), pxl(2.1), pxl(0.8), pxl(0.8))
def move(self, dx, dy):
# Move each axis separately. Note that this checks for collisions both times.
if dx != 0:
self.move_single_axis(dx, 0)
if dy != 0:
self.move_single_axis(0, dy)
def move_single_axis(self, dx, dy):
# Move the rect
self.rect.x += dx
self.rect.y += dy
# If you collide with a wall, move out based on velocity
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0: # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
if dx < 0: # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
if dy > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
if dy < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
class Wall(object):
def __init__(self, pos):
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], pxl(1), pxl(1))
# Variables
pygame.display.set_caption("PyGame for University") # Titel des Spiels
clock = pygame.time.Clock() # Schnelligkeit des Spiels festlegen, damit es überall gleich schnell läuft
ticks = 0
player = Player()
walls = []
level = [
"WWWWWWWWWWWWW",
"W W",
"W WWW",
"W WWWW W",
"W W W",
"W WWW WWWW W",
"W W W W",
"W W W W",
"W W",
"WWWWWWWWWWWWW",
]
x = y = 0
for row in level:
for col in row:
if col == "W":
Wall((x, y))
if col == "E":
end_rect = pygame.Rect(x, y, pxl(1), pxl(1))
x += pxl(1)
y += pxl(1)
x = 0
## Screen Logic
# Screen-Settings
# game main loop
running = True
while running:
j = 0
i = 0
#renderText = font.render(move_list[i], True, BLACK)
for event in pygame.event.get():
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
print("Spiel wird beendet!")
elif event.type == KEYDOWN:
if event.key == K_UP:
screen.blit(font.render("RECHTS", False, WHITE), [pxl(2), pxl(2)])
move_list.append(1)
elif event.key == K_DOWN:
move_list.append(2)
write_move()
elif event.key == K_RIGHT:
move_list.append(3)
write_move()
elif event.key == K_LEFT:
move_list.append(4)
write_move()
elif event.key == K_0:
print(move_list)
if event.type == pygame.USEREVENT:
counter -= 1
text = str(counter).rjust(pxl(1080)) if counter > 0 else "boom!"
# game logic ########################################################################
# draw frame ########################################################################
screen.fill(WOODY) # draw background
drawRect()
drawGrid()
for wall in walls:
pygame.draw.rect(screen, BLACK, wall.rect)
pygame.draw.rect(screen, BLUE, player.rect)
screen.blit(font.render(text, True, BLACK), (1080,40))
####################################################################################
pygame.display.flip() # update window
clock.tick(60)
Upvotes: 1
Views: 225
Reputation: 210909
Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).
1080 is way too many characters. Change the argument (e.g. 4):
text = str(counter).rjust(pxl(1080)) if counter > 0 else "boom!"
text = str(counter).rjust(4) if counter > 0 else "boom!"
Upvotes: 1