Why does this function in pygame not work - pygame

This code is supposed to print the domino piece [4,2], but it only fills the surface white and does not print anything.
I don't get an error and I can't find out what I did wrong.
import os
import string
import random
import pygame, sys
from pygame.locals import *
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
def main():
global DISPLAYSURF
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1300, 600), 0, 32)
pygame.display.set_caption('domino test')
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAYSURF.fill(WHITE)
printpiece([4,2],(400,200))
pygame.display.update()
def printpiece(piece, position):
def printdots(number, centre):
def printdot(dot, centre):
if dot == "AA":
pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]-15, centre[1]-15), 3, 3)
elif dot == "AB":
pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]-15, centre[1]), 3, 3)
elif dot == "AC":
pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]-15, centre[1]+15), 3, 3)
elif dot == "BB":
pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0], centre[1]), 3, 3)
elif dot == "CA":
pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]+15, centre[1]-15), 3, 3)
elif dot == "CB":
pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]+15, centre[1]), 3, 3)
elif dot == "CC":
pygame.draw.circle(DISPLAYSURF, BLACK, (centre[0]+15, centre[1]+15), 3, 3)
if number == 1:
printdot("BB", centre)
elif number == 2:
printdot("AC", centre)
printdot("CA", centre)
elif number == 3:
printdot("AC", centre)
printdot("BB", centre)
printdot("CA", centre)
elif number == 4:
printdot("AA", centre)
printdot("CA", centre)
printdot("AC", centre)
printdot("CC", centre)
elif number == 5:
printdot("AA", centre)
printdot("CA", centre)
printdot("BB", centre)
printdot("AC", centre)
printdot("CC", centre)
elif number == 6:
printdot("AA", centre)
printdot("AB", centre)
printdot("AC", centre)
printdot("CA", centre)
printdot("CB", centre)
printdot("CC", centre)
pygame.draw.rect(DISPLAYSURF, BLACK, (position[0]-5, position[1]-5, 110, 60))
pygame.draw.rect(DISPLAYSURF, WHITE, (position[0], position[1], 100, 50))
pygame.draw.line(DISPLAYSURF, BLACK, (position[0]+50, position[1]), (position[0]+50, position[1]+50), 2)
printdots(piece[0], (position[0]+25, position[1]+25))
printdots(piece[1], (position[0]+75, position[1]+25))
if __name__ == '__main__':
main()

Your indentation is wrong. printpiece(piece, position) doesn't execute any statements.
Delete one tab from the front of these lines and it will work:
pygame.draw.rect(DISPLAYSURF, BLACK, (position[0]-5, position[1]-5, 110, 60))
pygame.draw.rect(DISPLAYSURF, WHITE, (position[0], position[1], 100, 50))
pygame.draw.line(DISPLAYSURF, BLACK, (position[0]+50, position[1]), (position[0]+50, position[1]+50), 2)
printdots(piece[0], (position[0]+25, position[1]+25))
printdots(piece[1], (position[0]+75, position[1]+25))

Related

Text doesn't display on the title screen button

I've been having trouble trying to display the title menu text when making a game. I'm trying to display the font on the button so that when I hover my mouse over the button, the font stays on the screen.
import pygame
pygame.init()
background_colour = (33, 37, 49)
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('A Game')
screen.fill(background_colour)
game_font = pygame.font.Font('JetBrainsMono-Regular.ttf', 50)
game_name = game_font.render('a game', True, (255, 255, 255))
game_name = pygame.transform.scale2x(game_name)
game_name_rect = game_name.get_rect(center=(500, 300))
pygame.draw.rect(screen, (38, 44, 60), game_name_rect)
running = True
title_screen = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if title_screen is True:
screen.blit(game_name, game_name_rect)
if not game_name_rect.collidepoint(pygame.mouse.get_pos()):
pygame.draw.rect(screen, (38, 44, 60), game_name_rect)
if game_name_rect.collidepoint(pygame.mouse.get_pos()):
pygame.draw.rect(screen, (58, 64, 80), game_name_rect)
pygame.display.update()
You need to draw the text after the rectangle:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if title_screen is True:
color = (38, 44, 60)
if game_name_rect.collidepoint(pygame.mouse.get_pos()):
color = (58, 64, 80)
pygame.draw.rect(screen, color, game_name_rect)
screen.blit(game_name, game_name_rect)
pygame.display.update()

User input recorded from multiple text boxes and put into MySQL

I'm running into a brick wall and have spent multiple hours trying to research how to do this and I'm at the point I'm not sure if I'm asking the right question.
I have a page for recording user input in multiple boxes. I've made a separate file for my text box function which feeds into the main page. I want to be able to click next and record all the use input from the text boxes into a MySQL database.
The problem I'm running into seems to be that the main page doesn't know there's user input just that there is a text box, so I can't record anything, while if I change the text input file it defeats the purpose of creating one so that I can use text boxes anywhere.
Main page
import pygame
import Test
import button
import sys
import mysql.connector
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Main Page")
icon = pygame.image.load('icons8-robber-32.png')
pygame.display.set_icon(icon)
colour_active = pygame.Color('orange')
colour_inactive = pygame.Color('Yellow')
font = pygame.font.Font(None, 32)
start_image = pygame.image.load('play.png').convert_alpha()
exit_image = pygame.image.load('exit.png').convert_alpha()
menu_image = pygame.image.load('menu.png').convert_alpha()
back_image = pygame.image.load('back.png').convert_alpha()
next_image = pygame.image.load('next.png').convert_alpha()
def register():
input_box1 = Test.inputBox(210, 105, 140, 32, "First Name: ")
input_box2 = Test.inputBox(210, 160, 140, 32)
input_boxes = [input_box1, input_box2]
back_button = button.Button(100, 450, back_image, 0.2)
next_button = button.Button(450, 450, next_image, 0.2)
done = False
while not done:
for event in pygame.event.get():
for box in input_boxes:
box.handle_event(event)
for box in input_boxes:
box.update()
screen.fill((30, 30, 30))
for box in input_boxes:
box.draw(screen)
if back_button.draw(screen):
print("no")
if next_button.draw(screen):
sys.exit()
pygame.display.update()
start_button = button.Button(100, 150, start_image, 0.2)
exit_button = button.Button(250, 150, exit_image, 0.2)
menu_button = button.Button(400, 150, menu_image, 0.2)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((150, 255, 100))
if start_button.draw(screen):
print("no")
if exit_button.draw(screen):
sys.exit()
if menu_button.draw(screen):
register()
pygame.display.update()
pygame.QUIT()
sys.exit()
Text box file
import pygame
pygame.init()
colour_active = pygame.Color('orange')
colour_inactive = pygame.Color('Yellow')
font = pygame.font.Font(None, 32)
class inputBox():
def __init__(self, x, y, w, h, text=''):
self.rect = pygame.Rect(x,y,w,h)
self.color = colour_inactive
self.text = text
self.text_surface = font.render(text, True, self.color)
self.active = False
def handle_event(self,event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.active = not self.active
else:
self.active = False
self.color = colour_active if self.active else colour_inactive
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
print(self.text)
elif event.key == pygame.K_BACKSPACE:
self.text = self.text [:-1]
else:
self.text += event.unicode
self.text_surface = font.render(self.text, True, self.color)
def update(self):
self.rect.w = max(100, self.text_surface.get_width() + 10)
def draw(self, screen):
screen.blit(self.text_surface, (self.rect.x+5, self.rect.y+5))
pygame.draw.rect(screen, self.color, self.rect, 2)
You do not need the self.text file in the main code. So instead of writing input_box1.self.text, it was just input_box1.text.

I'm trying to make a wild west game with pygame, but I can't get the shooting functions to work

I've been trying to make a wild west game in Pygame in which the player duels an NPC, but I can't get the shooting functions to work. I've basically tried to keep it simple, and I have a series of functions to make the bullet go across the screen, but they aren't working. Here's the code:
import pygame, time
pygame.init()
display_width = 800
display_height = 600
win = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('The Old Wild West')
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
dead = False
avatar = pygame.image.load('cowboy.jfif')
avatar2 = pygame.image.load('enemy.jfif')
bullet = pygame.image.load('bullet.png')
bx2 = 330
bx3 = 440
bx4 = 550
bx = 220
by = 220
bspeed = 165
x = 50
ex = 550
ey = 225
y = 200
x_change = 0
speed = 0
def cowboy(x,y):
win.blit(avatar, (x,y))
def enemy(ex, ey):
win.blit(avatar2, (ex, ey))
def shootframe1():
win.blit(bullet, (bx, by))
def antiframe1():
pygame.draw.rect(win, (255, 255, 255), (bx, by, 50, 150))
def shootframe2():
win.blit(bullet, (bx2, by))
def antiframe2():
pygame.draw.rect(win, (255, 255, 255), (bx2, by, 50, 150))
def shootframe3():
win.blit(bullet, (bx3, by))
def antiframe3():
pygame.draw.rect(win, (255, 255, 255), (bx3, by, 50, 150))
def shootframe4():
win.blit(bullet, (bx4, by))
def antiframe4():
pygame.draw.rect(win, (255, 255, 255), (bx4, by, 50, 150))
while not dead:
for event in pygame.event.get():
if event.type == pygame.QUIT:
dead = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.type == pygame.K_SPACE:
if event.key == pygame.K_SPACE:
shootframe1()
time.sleep(0.5)
antiframe1()
shootframe2()
time.sleep(0.5)
antiframe2()
shootframe3()
time.sleep(0.5)
antiframe3()
shootframe4()
time.sleep(0.5)
antiframe4()
x += x_change
win.fill(white)
cowboy(x,y)
enemy(ex, ey)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
Here is cowboy.jfif, enemy.jfif and bullet.png:
bullet.png
cowboy.jfif
enemy.jfif
Please help me out with this confusion.
I updated your code drastically, I tried to comment everything pretty well so it's clear what is happening.
First, you were filling the screen white after drawing the bullet onto the screen so it never showed up.
Second, the space bar key didn't fire because you were checking if the space bar was an event type. So I moved it under the event.type == pygame.KEYDOWN
Third, using time.sleep(0.5) makes the game window freeze and stutter a lot so I would avoid doing that.
If you have any questions about the changes feel free to ask.
import pygame, time
pygame.init()
display_width = 800
display_height = 600
win = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('The Old Wild West')
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
dead = False
avatar = pygame.image.load('cowboy.jfif')
avatar2 = pygame.image.load('enemy.jfif')
bullet = pygame.image.load('bullet.png')
bx2 = 330
bx3 = 440
bx4 = 550
bx = 220
by = 220
bspeed = 165
x = 50
ex = 550
ey = 225
y = 200
x_change = 0
speed = 0
def cowboy(x,y):
win.blit(avatar, (x,y))
def enemy(ex, ey):
win.blit(avatar2, (ex, ey))
# A single shoot function that appends a new bullet to our bullet list below.
def shoot():
bullet_list.append(Bullet(bx, by))
# A bullet class that holds information about a single bullet
class Bullet:
def __init__(self, x, y):
# Initializing variables for the bullet
self.x = x
self.y = y
self.image = pygame.image.load('bullet.png')
self.bullet_speed = 165
# Moves the bullet accross the screen by updating the x variable after each draw()
def _move_bullet(self):
self.x += self.bullet_speed
def draw(self):
# Get the surface (window)
surface = pygame.display.get_surface()
# Blit the bullet to the screen
surface.blit(self.image, (self.x, self.y))
# Update its position with the function above
self._move_bullet()
bullet_list = [] # Since the user can have multiple bullets firing, we need to keep track of them
while not dead:
win.fill(white) #Move this to the top so you're filling the screen before drawing anything
for event in pygame.event.get():
if event.type == pygame.QUIT:
dead = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.key == pygame.K_SPACE: # Moved this up here
shoot() # Spawn the bullet
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
cowboy(x,y)
enemy(ex, ey)
# Draw each bullet onto the screen by iterating over the list
for bullet in bullet_list:
# If the bullet has exited the screen we remove it from the list so they are not kept in memory
if bullet.x > display_width:
bullet_list.remove(bullet)
# Draw the bullet (called from the Class)
bullet.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()

TypeError: Rect argument is invalid pygame

i am new to pygame and wanted to make a game i found a playlist online at https://www.youtube.com/playlistlist=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO t i followed it but i got a error like this
Traceback (most recent call last):
File "/home/pi/firstgamepyg.py", line 78, in <module>
game_loop()
File "/home/pi/firstgamepyg.py", line 65, in game_loop
things(thing_startx, thing_starty, thing_width, thing_height, black)
File "/home/pi/firstgamepyg.py", line 15, in things
pygame.draw.rect(gamedisplay, color,(thingx, thingy, thingw, thingh))
TypeError: Rect argument is invalid
this is my code
import pygame
import time
import random
pygame.init()
dw = 800
dh = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
cw = 96
gamedisplay = pygame.display.set_mode((dw,dh))
cimg = pygame.image.load('/home/pi/Downloads/car.jpeg')
def things(thingx, thingy, thingw, color, thingh):
#error on next line
pygame.draw.rect(gamedisplay, color,(thingx, thingy, thingw, thingh))
def car(x,y):
gamedisplay.blit(cimg,(x,y))
def text_objects(text, font):
textsurface = font.render(text, True, red)
return textsurface, textsurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((dw/2),(dh/2))
gamedisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('wasted')
def game_loop():
x = (dw * 0.45)
y = (dh * 0.65)
x_change = 0
thing_startx = random.randrange(0,800)
thing_starty = -600
thing_speed = 7
thing_width = 100
thing_height = 100
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
gameexit = False
while not gameexit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameexit = True
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key==pygame.K_RIGHT:
x_change = 0
x += x_change
gamedisplay.fill(white)
things(thing_startx, thing_starty, thing_width, thing_height, black)
thing_starty += thing_speed
car(x,y)
if x > dw - cw or x < 0:
crash()
gameexit = True
pygame.display.update()
clock.tick(100)
game_loop()
pygame.quit()
quit()
i was trying to make a game where you are a car and avoid blocks but i cant add the blocks because i get an error
your things function is formatted incorrectly. you have the parameter list as (thingx, thingy, thingw, color, thingh) but then you call it as (thingx, thingy, things, thingh, color). to make the errors go away all that you need to do is in the function definition switch color with thingh

How can I make the triangle in this program rotate?

Please be very specific , because it`s my first time working in pygame :D
#!/usr/bin/env python
import pygame
pygame.init()
#colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
PI = 3.141592653
size = (400, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Professor Craven's Cool Game")
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(WHITE)
pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Text", True, BLACK)
screen.blit(text, [250, 250])
pygame.display.flip()
Yes , if I have an error in this code also please tell me . I am open to learn new things about pygame .
clock.tick(60)
pygame.quit()
Try: pygame.transform.rotate() to rotate a pygame.Surface