【发布时间】:2021-07-19 09:34:06
【问题描述】:
感谢您的帮助,我是一名学校主要工作的初学者程序员,并决定做一个结帐应用程序。这不是什么花哨的东西,大多数方面都是硬编码的,有些部分是从可编辑的 txt 文件中读取的。我目前正在制作应用程序的主系统,用户可以在其中选择添加到购物车的项目,并检查它是否已经在购物车中以修改购物车,因为我不想加倍在一件衣服上。
###Tasks 我卡住了
- 我无法让它写入应该接受 \n 的新行(大约 234),它不起作用,而是显示一个框,好像它不理解该字符。可能是我正在使用的字符系统,但我不太确定。 如果购物车中有东西并添加了另一个东西,则无法返回并修改购物车中已有的内容。 (当没有添加任何其他内容时,我能够做到这一点,因为它只是替换了文本)。 我的老师/项目主管还说要添加一个管理功能,他们可以通过 txt 文件修改商品、价格和图片(如果我添加的话)。
- 我还希望购物车位于滚动屏幕上,必要时会更新大小,但这是一项可选功能,除非有好心人能解释,否则我将寻找教程。 顶部的矩形是为了带到另一个页面,有更多的选项可供选择,我想不出如何编写这个位。 我也有点想在右下角打印日期和时间,但这是一个小片段 我知道很多,但我要到六月下旬才能完成所有事情。任何建议都值得赞赏,因为它仍在进行中,而且我只是该语言的新手。如果您需要任何其他相关信息,请告诉我。
谢谢大家!!!!!! #Edit : 按原样添加画面图片
# window ----------------------------------------
"""
This file is the executable file which calls all other files including the
full application.
"""
#The following imports are modules which have prewritten
#functions which I am able to use. They came predownloaded
#with python as well as downloading some from pygame.org
import pygame, sys
import tkinter as tk
from tkinter import Tk, Canvas, Frame, BOTH
from tkinter import ttk
from tkinter import *
from collections import Counter as count
from datetime import datetime
import time
import string
#import read
mainClock = pygame.time.Clock()
#setting window
from pygame.locals import *
#The following are Constants which are defined here for easier maintanence
click = bool
pygame.init()
pygame.display.set_caption('LeVOGUE')
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1200, 800), 0, 32)
windowSurface = pygame.display.set_mode((1200, 650), pygame.DOUBLEBUF)
run = 2
font = pygame.font.SysFont(None, 35)
font2 = pygame.font.SysFont(None, 70)
font3 = pygame.font.SysFont(None, 25)
base_font = pygame.font.Font(None, 50)
base_font2 = pygame.font.Font(None, 160)
Item = ''
Item_rect = pygame.Rect(780, 33, 400, 547)
Colour = pygame.Color(0,0,0)
found =False
#key_text_num = 2
s = " "
fh =open("user_info.txt", "r")
keys = pygame.key.get_pressed()
click = pygame.MOUSEBUTTONDOWN
Volume = True
active = False
#tick_active = False
#This is responsible fo importing the mixer module
from pygame import mixer
#Load music file
mixer.music.load("MainMenuMusic.mp3")
#play music file
mixer.music.play()
sc1_num = 0
sc1_price = 0
sc2_num = 0
sc2_price = 0
def Main_sales():
#this area of the code was reused from a previous file
#which is an example of the RAD approach.
#~~~~~~~~~~~~~~~~~Start Code applied from previous programs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global click
global Item
global active
global bags
global current_time
global found
global count
global s
global fh
global i_a
global key_text_num
global Item_rect
global sc1_num
global sc1_price
global sc2_num
global sc2_price
#This checkbox is imported from a module found within the LeVOGUE folder
#The checkbox file is imported above and called with the function checkbox
while True:
#Defining Quit
for event in pygame.event.get():
if event.type == QUIT:
print("Quit")
pygame.quit()
sys.exit()
#~~~~~~~~~~~~~~~~~~~~~~~~End Code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#if key escape key is pressed, Quit
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
#if click, click is true
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True
if click:
print(pygame.mouse.get_pos())
#Background colour
screen.fill((255, 255, 255))
text_surface = font3.render(Item, True, (0,0,0))
screen.blit(text_surface, (Item_rect.x+20 , Item_rect.y +100))
#mouse position
mx, my = pygame.mouse.get_pos()
#This section of code was applied from a tutorial which I followed
#It was used to allow me to create rectangular buttons although it was
#heavily modified to add functionality to different buttons
#~~~~~~~~~~~~~~Start code applied from tutorial ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
b_w = 180
b_h = 70
b2_x = 274
b2_y = 562
b2_w = 140
b2_h = 65
b3_x = 55
b3_y = 200
b4_x = 275
b4_y = 200
b5_x = 490
b5_y = 200
b6_x = 55
b6_y = 290
b7_x = 275
b7_y = 290
b8_x = 490
b8_y = 290
b9_x = 55
b9_y = 380
b10_x = 275
b10_y = 380
b11_x = 490
b11_y = 380
b12_x = 55
b12_y = 470
b13_x = 275
b13_y = 470
b14_x = 490
b14_y = 470
button_2 = pygame.Rect(b2_x, b2_y, b2_w, b2_h)
button_3 = pygame.Rect( b3_x, b3_y ,b_w, b_h)
button_4 = pygame.Rect( b4_x, b4_y ,b_w, b_h)
button_5 = pygame.Rect( b5_x, b5_y ,b_w, b_h)
button_6 = pygame.Rect( b6_x, b6_y ,b_w, b_h)
button_7 = pygame.Rect( b7_x, b7_y ,b_w, b_h)
button_8 = pygame.Rect( b8_x, b8_y ,b_w, b_h)
button_9 = pygame.Rect( b9_x, b9_y ,b_w, b_h)
button_10 = pygame.Rect( b10_x, b10_y ,b_w, b_h)
button_11 = pygame.Rect( b11_x, b11_y ,b_w, b_h)
button_12 = pygame.Rect( b12_x, b12_y ,b_w, b_h)
button_13 = pygame.Rect( b13_x, b13_y ,b_w, b_h)
button_14 = pygame.Rect( b14_x, b14_y ,b_w, b_h)
#What happens if the mouse click is over buttons listed above
if button_2.collidepoint(mx, my):
if click:
print("button2")
import MainMenu
if button_3.collidepoint(mx, my):
if click:
#while (sc_num < 10):
if (f"Scrunchie {sc1_num} ${sc1_price} ") in Item:
Item = Item[:-29]
sc1_num = sc1_num+1
sc1_price = sc1_price+15
Item = Item + (f"Scrunchie {sc1_num} ${sc1_price} \n")
print(" Button3")
if button_4.collidepoint(mx, my):
if click:
if (f"Scrunchie {sc2_num} ${sc2_price}") in Item:
Item = Item[:-39]
sc2_num = sc2_num+1
sc2_price = sc2_price+15
Item = Item + (f"Scrunchie2 {sc2_num} ${sc2_price}")
print(" Button4")
if button_5.collidepoint(mx, my):
if click:
print(" Button5")
if button_6.collidepoint(mx, my):
if click:
print(" Button6")
if button_7.collidepoint(mx, my):
if click:
print(" Button7")
if button_8.collidepoint(mx, my):
if click:
print(" Button8")
if button_9.collidepoint(mx, my):
if click:
print(" Button9")
if button_10.collidepoint(mx, my):
if click:
print(" Button10")
if button_11.collidepoint(mx, my):
if click:
print(" Button11")
if button_12.collidepoint(mx, my):
if click:
print(" Button12")
if button_13.collidepoint(mx, my):
if click:
print(" Button13")
if button_14.collidepoint(mx, my):
if click:
print(" Button14")
text_surface_Cart = font.render("Cart", True, (0,0,0))
text_surface_Item = font.render( "Item Qty Price", True, (0,0,0))
screen.blit(text_surface_Cart, (790,50))
screen.blit(text_surface_Item, (790, 100))
#for Button 2
text_surface_Enter = font2.render("Enter", True, (0,0,0))
screen.blit(text_surface_Enter, pygame.Rect((280, 565), (120, 70)))
#Cart
pygame.draw.rect(screen,Colour,pygame.Rect(780,30,400,550),2)
#button 2; Enter
pygame.draw.rect(screen, Colour , button_2, 2)
#Draw boxes for the number buttons
pygame.draw.rect(screen, Colour, button_3, 2)
pygame.draw.rect(screen, Colour, button_4, 2)
pygame.draw.rect(screen, Colour, button_5, 2)
pygame.draw.rect(screen, Colour, button_6, 2)
pygame.draw.rect(screen, Colour, button_7, 2)
pygame.draw.rect(screen, Colour, button_8, 2)
pygame.draw.rect(screen, Colour, button_9, 2)
pygame.draw.rect(screen, Colour, button_10, 2)
pygame.draw.rect(screen, Colour, button_11, 2)
pygame.draw.rect(screen, Colour, button_12, 2)
pygame.draw.rect(screen, Colour, button_13, 2)
pygame.draw.rect(screen, Colour, button_14, 2)
screen.blit((font3.render("Scrunchie1", True, (0,0,0))),(b3_x+20, b3_y+15))
screen.blit((font3.render("Scrunchie2", True, (0,0,0))),(b4_x+20, b4_y+15))
screen.blit((font3.render("Scrunchie3", True, (0,0,0))),(b5_x+20, b5_y+15))
screen.blit((font3.render("Scrunchie4", True, (0,0,0))),(b6_x+20, b6_y+15))
screen.blit((font3.render("Scrunchie5", True, (0,0,0))),(b7_x+20, b7_y+15))
screen.blit((font3.render("Shirt6", True, (0,0,0))),(b8_x+20, b8_y+15))
screen.blit((font3.render("Shirt7", True, (0,0,0))),(b9_x+20, b9_y+15))
screen.blit((font3.render("Shirt8", True, (0,0,0))),(b10_x+20, b10_y+15))
screen.blit((font3.render("Shirt9", True, (0,0,0))),(b11_x+20, b11_y+15))
screen.blit((font3.render("Pants10", True, (0,0,0))),(b12_x+20, b12_y+15))
screen.blit((font3.render("Pants11", True, (0,0,0))),(b13_x+20, b13_y+15))
screen.blit((font3.render("Pants11", True, (0,0,0))),(b14_x+20, b14_y+15))
#~~~~~~~~~~~~~~~~~~ End Code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ButtonBolt = pygame.image.load("LionsHEAD.png")
ButtonBolt = pygame.transform.scale(ButtonBolt, (120, 120))
screen.blit(ButtonBolt, (20,15))
text_surface_LeVOGUE = font2.render("LeVOGUE", True, (0,0,0))
screen.blit(text_surface_LeVOGUE, (140,55))
Key_Rect = pygame.Rect(260, 125, 230, 40)
pygame.draw.rect(screen, (0,0,0) , Key_Rect, 2)
Left_select_Rect = pygame.Rect(208, 125, 40, 40)
pygame.draw.rect(screen, (0,0,0), Left_select_Rect , 2)
text_surface_Left_select = font2.render("<", True, (0,0,0))
screen.blit(text_surface_Left_select, (215,118))
Right_select_Rect = pygame.Rect(502, 125, 40, 40)
pygame.draw.rect(screen, (0,0,0), Right_select_Rect , 2)
text_surface_Left_select = font2.render(">", True, (0,0,0))
screen.blit(text_surface_Left_select, (510,118))
#CART
pygame.draw.rect(screen,Colour,pygame.Rect(780,30,400,550),2)
click = False
#update screen
pygame.display.update()
pygame.display.flip()
mainClock.tick(60)
#The following code was derived from a tutorial which allows the application
#to quit without causing any error codes.
#~~~~~~~~~~~~Start code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def Quit():
running = True
while running:
pygame.quit()
quit()
#~~~~~~~~~~~ End Code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#run the main menu
Main_sales()
pygame.quit()
sys.exit()
【问题讨论】:
-
如果您简化示例,您可能会获得更多有用的帮助。没有多少人会阅读所有这些代码
标签: python python-3.x pygame